提交 778c82be 编写于 作者: J Jon Leighton

Generate attribute method unless it's already in the module.

There's no harm in generating a method name that's already defined on
the host class, since we're generating the attribute methods in a module
that gets included. In fact, this is desirable as it allows the host
class to call super.
上级 55da28dd
......@@ -329,7 +329,7 @@ def generated_attribute_methods #:nodoc:
protected
def instance_method_already_implemented?(method_name)
method_defined?(method_name)
generated_attribute_methods.method_defined?(method_name)
end
private
......
......@@ -89,6 +89,29 @@ class AttributeMethodsTest < ActiveModel::TestCase
assert_equal "value of foo", ModelWithAttributes.new.foo
end
test '#define_attribute_method does not generate attribute method if already defined in attribute module' do
klass = Class.new(ModelWithAttributes)
klass.generated_attribute_methods.module_eval do
def foo
'<3'
end
end
klass.define_attribute_method(:foo)
assert_equal '<3', klass.new.foo
end
test '#define_attribute_method generates a method that is already defined on the host' do
klass = Class.new(ModelWithAttributes) do
def foo
super
end
end
klass.define_attribute_method(:foo)
assert_equal 'value of foo', klass.new.foo
end
test '#define_attribute_method generates attribute method with invalid identifier characters' do
ModelWithWeirdNamesAttributes.define_attribute_method(:'a?b')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册