mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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.
This commit is contained in:
parent
55da28dd2f
commit
778c82bea6
2 changed files with 24 additions and 1 deletions
|
@ -329,7 +329,7 @@ module ActiveModel
|
|||
|
||||
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')
|
||||
|
||||
|
|
Loading…
Reference in a new issue