Skip to content

Commit

Permalink
Fixes STI when 2+ levels deep.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
arthurnn committed Mar 10, 2014
1 parent 6e3ab3e commit e5f15a8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
6 changes: 3 additions & 3 deletions activerecord/lib/active_record/attribute_methods.rb
Expand Up @@ -105,9 +105,9 @@ def instance_method_already_implemented?(method_name)
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

Expand Down
13 changes: 13 additions & 0 deletions activerecord/test/cases/attribute_methods_test.rb
Expand Up @@ -746,6 +746,19 @@ def test_bulk_update_raise_unknown_attribute_errro
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'
Expand Down

0 comments on commit e5f15a8

Please sign in to comment.