Created
June 9, 2011 11:58
-
-
Save ream88/1016585 to your computer and use it in GitHub Desktop.
Changeable Concern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Changeable | |
extend ActiveSupport::Concern | |
included do | |
attr_accessible :type | |
end | |
module ClassMethods | |
def new(attributes = nil, options = {}, &block) | |
type = attributes['type'] if attributes.is_a?(Hash) | |
if type.blank? || (type = type.constantize) == self | |
super(attributes, options, &block) | |
else | |
super({}, options, &block).becomes(type).tap do |object| | |
object.attributes = attributes | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment