Skip to content

Instantly share code, notes, and snippets.

@ream88
Created June 9, 2011 11:58
Show Gist options
  • Save ream88/1016585 to your computer and use it in GitHub Desktop.
Save ream88/1016585 to your computer and use it in GitHub Desktop.
Changeable Concern
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