Created
June 14, 2011 05:52
-
-
Save tomafro/1024399 to your computer and use it in GitHub Desktop.
This file contains 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
class Event < Node | |
end |
This file contains 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
class Account < ActiveRecord::Base | |
has_many :nodes | |
end | |
Account.first.nodes.create! :type => 'event' |
This file contains 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
class Message < Node | |
end |
This file contains 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
class Node < ActiveRecord::Base | |
belongs_to :account | |
def new(attributes = {}, *args, &block) | |
clazz = class_for(attributes.delete(:type)) | |
if clazz == self | |
super(attributes, *args, &block) | |
else | |
clazz.new(attributes, *args, &block) | |
end | |
end | |
def class_for(t) | |
case t | |
when 'message' then Message | |
when 'event' then Event | |
else self | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment