aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activemodel/lib/active_model/attribute_methods.rb2
-rw-r--r--activemodel/test/cases/attribute_methods_test.rb26
2 files changed, 27 insertions, 1 deletions
diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb
index 26ee9c4315..977a101277 100644
--- a/activemodel/lib/active_model/attribute_methods.rb
+++ b/activemodel/lib/active_model/attribute_methods.rb
@@ -165,6 +165,7 @@ module ActiveModel
end
end
end
+ @attribute_methods_generated = true
end
def undefine_attribute_methods
@@ -176,7 +177,6 @@ module ActiveModel
def generated_attribute_methods #:nodoc:
@generated_attribute_methods ||= begin
- @attribute_methods_generated = true
mod = Module.new
include mod
mod
diff --git a/activemodel/test/cases/attribute_methods_test.rb b/activemodel/test/cases/attribute_methods_test.rb
index 614c4a3645..5659dcbc48 100644
--- a/activemodel/test/cases/attribute_methods_test.rb
+++ b/activemodel/test/cases/attribute_methods_test.rb
@@ -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
@@ -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