Created
March 28, 2012 15:07
-
-
Save tiegz/2226982 to your computer and use it in GitHub Desktop.
Pattern for class inheritable_attributes that was removed from rails 3.2
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
class SomeClass | |
self.instance_variable_set('@my_attribute', {:class => true}) | |
def self.my_attribute; @my_attribute; end | |
def self.inherited(klass) | |
val = self.instance_variable_get('@my_attribute') | |
klass.instance_variable_set('@my_attribute', val.duplicable? ? val.dup : val) | |
super | |
end | |
end | |
class AnotherClass < SomeClass; end | |
SomeClass.my_attribute # => {:class => true} | |
AnotherClass.my_attribute # => {:class => true} | |
AnotherClass[:subclass] = true | |
SomeClass.my_attribute # => {:class => true} | |
AnotherClass.my_attribute # => {:class => true, :subclass => true} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment