aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
authorPiotr Sarnacki <drogus@gmail.com>2012-01-15 12:06:50 +0100
committerPiotr Sarnacki <drogus@gmail.com>2012-01-15 15:54:59 +0100
commitb164e81c1159baf35af3e22a9c6b43875c1fcf49 (patch)
tree7231d72dcdcb6489b99a92fd36f956beed3d9405 /activemodel
parent85629c83af7d0d67d483174d836dc6daf1196e7b (diff)
downloadrails-b164e81c1159baf35af3e22a9c6b43875c1fcf49.tar.gz
rails-b164e81c1159baf35af3e22a9c6b43875c1fcf49.tar.bz2
rails-b164e81c1159baf35af3e22a9c6b43875c1fcf49.zip
Fix stack level too deep when model does not have attributes method.
Without that patch when using ActiveModel::AttributeMethods in a class that does not respond to `attributes` method, stack level too deep error will be raised on non existing method. While documentation is clear that you need to define `attributes` method in order to use AttributeMethods module, `stack level too deep` is rather obscure and hard to debug, therefore we should try to not break `method_missing` if someone forgets about defining `attributes`.
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/lib/active_model/attribute_methods.rb2
-rw-r--r--activemodel/test/cases/attribute_methods_test.rb8
2 files changed, 9 insertions, 1 deletions
diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb
index 71ab1501c8..432f3d4302 100644
--- a/activemodel/lib/active_model/attribute_methods.rb
+++ b/activemodel/lib/active_model/attribute_methods.rb
@@ -394,7 +394,7 @@ module ActiveModel
protected
def attribute_method?(attr_name)
- attributes.include?(attr_name)
+ respond_to_without_attributes?(:attributes) && attributes.include?(attr_name)
end
private
diff --git a/activemodel/test/cases/attribute_methods_test.rb b/activemodel/test/cases/attribute_methods_test.rb
index 0c6e49bee2..3f653e6620 100644
--- a/activemodel/test/cases/attribute_methods_test.rb
+++ b/activemodel/test/cases/attribute_methods_test.rb
@@ -76,7 +76,15 @@ private
end
end
+class ModelWithouAttributesMethod
+ include ActiveModel::AttributeMethods
+end
+
class AttributeMethodsTest < ActiveModel::TestCase
+ test 'method missing works correctly even if attributes method is not defined' do
+ assert_raises(NoMethodError) { ModelWithouAttributesMethod.new.foo }
+ end
+
test 'unrelated classes should not share attribute method matchers' do
assert_not_equal ModelWithAttributes.send(:attribute_method_matchers),
ModelWithAttributes2.send(:attribute_method_matchers)