Skip to content

Instantly share code, notes, and snippets.

@tiegz
Created March 28, 2012 15:07
Show Gist options
  • Save tiegz/2226982 to your computer and use it in GitHub Desktop.
Save tiegz/2226982 to your computer and use it in GitHub Desktop.
Pattern for class inheritable_attributes that was removed from rails 3.2
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