Skip to content

Commit

Permalink
Merge pull request #48913 from ipc103/fix-parent-deprecation-warning
Browse files Browse the repository at this point in the history
Allow parent to define alias method override
  • Loading branch information
eileencodes committed Aug 17, 2023
2 parents 8eb051a + a88f47d commit 8abfcd7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
5 changes: 5 additions & 0 deletions activemodel/lib/active_model/attribute_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ def attribute_method_affix(*affixes)
# person.nickname_short? # => true
def alias_attribute(new_name, old_name)
self.attribute_aliases = attribute_aliases.merge(new_name.to_s => old_name.to_s)
local_attribute_aliases[new_name.to_s] = old_name.to_s
eagerly_generate_alias_attribute_methods(new_name, old_name)
end

Expand Down Expand Up @@ -361,6 +362,10 @@ def undefine_attribute_methods
attribute_method_patterns_cache.clear
end

def local_attribute_aliases # :nodoc:
@local_attribute_aliases ||= {}
end

private
def inherited(base) # :nodoc:
super
Expand Down
3 changes: 2 additions & 1 deletion activerecord/lib/active_record/attribute_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ def eagerly_generate_alias_attribute_methods(_new_name, _old_name) # :nodoc:
end

def generate_alias_attributes # :nodoc:
superclass.generate_alias_attributes unless base_class?
return if @alias_attributes_mass_generated

generated_attribute_methods.synchronize do
return if @alias_attributes_mass_generated
ActiveSupport::CodeGenerator.batch(generated_attribute_methods, __FILE__, __LINE__) do |code_generator|
attribute_aliases.each do |new_name, old_name|
local_attribute_aliases.each do |new_name, old_name|
generate_alias_attribute_methods(code_generator, new_name, old_name)
end
end
Expand Down
12 changes: 12 additions & 0 deletions activerecord/test/cases/attribute_methods_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,9 @@ def subject_was
end
end

class ChildWithDeprecatedBehaviorResolved < ClassWithDeprecatedAliasAttributeBehaviorResolved
end

test "#alias_attribute with an overridden original method along with an overridden alias method doesn't issue a deprecation" do
obj = assert_not_deprecated(ActiveRecord.deprecator) do
ClassWithDeprecatedAliasAttributeBehaviorResolved.new
Expand All @@ -1234,6 +1237,15 @@ def subject_was
assert_equal("overridden_subject_was", obj.subject_was)
end

test "#alias_attribute with an overridden original method along with an overridden alias method in a parent class doesn't issue a deprecation" do
obj = assert_not_deprecated(ActiveRecord.deprecator) do
ChildWithDeprecatedBehaviorResolved.new
end
obj.title = "hey"
assert_equal("hey", obj.subject)
assert_equal("overridden_subject_was", obj.subject_was)
end

private
def new_topic_like_ar_class(&block)
klass = Class.new(ActiveRecord::Base) do
Expand Down

0 comments on commit 8abfcd7

Please sign in to comment.