1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fixes STI when 2+ levels deep.

PR #14052 Added a regression where it was only looking for methods in one
level up, So when the method was defined in a 2+ levels up the
inheritance chain, the method was not found as defined.
This commit is contained in:
Arthur Neves 2014-03-10 11:37:33 -04:00
parent 6e3ab3e15f
commit e5f15a8365
No known key found for this signature in database
GPG key ID: 04A390FB1E433E17
2 changed files with 16 additions and 3 deletions

View file

@ -105,9 +105,9 @@ module ActiveRecord
super
else
# If B < A and A defines its own attribute method, then we don't want to overwrite that.
defined = method_defined_within?(method_name, superclass) &&
superclass.instance_method(method_name).owner != superclass.generated_attribute_methods
defined || super
defined = method_defined_within?(method_name, superclass, superclass.generated_attribute_methods)
base_defined = Base.method_defined?(method_name) || Base.private_method_defined?(method_name)
defined && !base_defined || super
end
end

View file

@ -746,6 +746,19 @@ class AttributeMethodsTest < ActiveRecord::TestCase
assert "unknown attribute: hello", error.message
end
def test_methods_override_in_multi_level_subclass
klass = Class.new(Developer) do
def name
"dev:#{read_attribute(:name)}"
end
end
2.times { klass = Class.new klass }
dev = klass.new(name: 'arthurnn')
dev.save!
assert_equal 'dev:arthurnn', dev.reload.name
end
def test_global_methods_are_overwritten
klass = Class.new(ActiveRecord::Base) do
self.table_name = 'computers'