Skip to content

Commit be2c632

Browse files
authored
Merge pull request rails#48996 from Shopify/check-for-parent-being-an-owner-of-the-target-method
Allow parent being the owner of an alias attribute method
2 parents 5544a81 + 0c99588 commit be2c632

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

activerecord/lib/active_record/attribute_methods.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ def alias_attribute_method_definition(code_generator, pattern, new_name, old_nam
8787
old_name = old_name.to_s
8888

8989
method_defined = method_defined?(target_name) || private_method_defined?(target_name)
90-
manually_defined = method_defined && self.instance_method(target_name).owner != generated_attribute_methods
90+
manually_defined = method_defined &&
91+
!self.instance_method(target_name).owner.is_a?(GeneratedAttributeMethods)
9192
reserved_method_name = ::ActiveRecord::AttributeMethods.dangerous_attribute_methods.include?(target_name)
9293

9394
if manually_defined && !reserved_method_name

activerecord/test/cases/attribute_methods_test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,6 +1266,28 @@ class ChildWithDeprecatedBehaviorResolved < ClassWithDeprecatedAliasAttributeBeh
12661266
assert_equal("overridden_subject_was", obj.subject_was)
12671267
end
12681268

1269+
ParentWithAlias = Class.new(ActiveRecord::Base) do
1270+
self.table_name = "topics"
1271+
alias_attribute :parents_subject, :title
1272+
end
1273+
1274+
AbstractClassInBetween = Class.new(ParentWithAlias) do
1275+
self.abstract_class = true
1276+
alias_attribute :parents_subject, :title
1277+
end
1278+
1279+
ChildWithAnAliasFromAbstractClass = Class.new(AbstractClassInBetween) do
1280+
end
1281+
1282+
test "#alias_attribute with the same alias as parent doesn't issue a deprecation" do
1283+
ParentWithAlias.new # eagerly generate parents alias methods
1284+
obj = assert_not_deprecated(ActiveRecord.deprecator) do
1285+
ChildWithAnAliasFromAbstractClass.new
1286+
end
1287+
obj.title = "hey"
1288+
assert_equal("hey", obj.parents_subject)
1289+
end
1290+
12691291
test "#alias_attribute method on an abstract class is available on subclasses" do
12701292
superclass = Class.new(ActiveRecord::Base) do
12711293
self.abstract_class = true

0 commit comments

Comments
 (0)