Skip to content

Commit

Permalink
Fixed behavior of attribute_methods_generated? [#3220 state:resolved]
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information
spohlenz authored and josh committed Oct 7, 2009
1 parent f8e91bd commit 4df9633
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/attribute_methods.rb
Expand Up @@ -165,6 +165,7 @@ def #{matcher.method_name(attr_name)}(*args)
end
end
end
@attribute_methods_generated = true
end

def undefine_attribute_methods
Expand All @@ -176,7 +177,6 @@ def undefine_attribute_methods

def generated_attribute_methods #:nodoc:
@generated_attribute_methods ||= begin
@attribute_methods_generated = true
mod = Module.new
include mod
mod
Expand Down
26 changes: 26 additions & 0 deletions activemodel/test/cases/attribute_methods_test.rb
Expand Up @@ -4,6 +4,15 @@ class ModelWithAttributes
include ActiveModel::AttributeMethods

attribute_method_suffix ''

def attributes
{ :foo => 'value of foo' }
end

private
def attribute(name)
attributes[name.to_sym]
end
end

class ModelWithAttributes2
Expand All @@ -17,4 +26,21 @@ class AttributeMethodsTest < ActiveModel::TestCase
assert_not_equal ModelWithAttributes.send(:attribute_method_matchers),
ModelWithAttributes2.send(:attribute_method_matchers)
end

test '#define_attribute_methods generates attribute methods' do
ModelWithAttributes.define_attribute_methods([:foo])

assert ModelWithAttributes.attribute_methods_generated?
assert ModelWithAttributes.new.respond_to?(:foo)
assert_equal "value of foo", ModelWithAttributes.new.foo
end

test '#undefine_attribute_methods removes attribute methods' do
ModelWithAttributes.define_attribute_methods([:foo])
ModelWithAttributes.undefine_attribute_methods

assert !ModelWithAttributes.attribute_methods_generated?
assert !ModelWithAttributes.new.respond_to?(:foo)
assert_raises(NoMethodError) { ModelWithAttributes.new.foo }
end
end

0 comments on commit 4df9633

Please sign in to comment.