提交 b164e81c 编写于 作者: P Piotr Sarnacki

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`.
上级 85629c83
...@@ -394,7 +394,7 @@ def respond_to?(method, include_private_methods = false) ...@@ -394,7 +394,7 @@ def respond_to?(method, include_private_methods = false)
protected protected
def attribute_method?(attr_name) def attribute_method?(attr_name)
attributes.include?(attr_name) respond_to_without_attributes?(:attributes) && attributes.include?(attr_name)
end end
private private
......
...@@ -76,7 +76,15 @@ def attribute(name) ...@@ -76,7 +76,15 @@ def attribute(name)
end end
end end
class ModelWithouAttributesMethod
include ActiveModel::AttributeMethods
end
class AttributeMethodsTest < ActiveModel::TestCase 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 test 'unrelated classes should not share attribute method matchers' do
assert_not_equal ModelWithAttributes.send(:attribute_method_matchers), assert_not_equal ModelWithAttributes.send(:attribute_method_matchers),
ModelWithAttributes2.send(:attribute_method_matchers) ModelWithAttributes2.send(:attribute_method_matchers)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册