Skip to content

Commit

Permalink
Fix class_attribute so that it works with singleton classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Carl Lerche committed Apr 2, 2010
1 parent f0364d8 commit ab281f5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
Expand Up @@ -50,7 +50,7 @@ def class_attribute(*attrs)
singleton_class.send(:define_method, attr) { value }
end

define_method(attr) { self.class.send(attr) }
define_method(attr) { self.singleton_class.send(attr) }
define_method(:"#{attr}?") { !!send(attr) }
define_method(:"#{attr}=") do |value|
singleton_class.remove_possible_method(attr)
Expand Down
6 changes: 6 additions & 0 deletions activesupport/test/core_ext/class/attribute_test.rb
Expand Up @@ -59,4 +59,10 @@ def setup
object = Class.new { class_attribute :setting, :instance_writer => false }.new
assert_raise(NoMethodError) { object.setting = 'boom' }
end

test 'works well with singleton classes' do
object = @klass.new
object.singleton_class.setting = 'foo'
assert_equal 'foo', object.setting
end
end

0 comments on commit ab281f5

Please sign in to comment.