Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure target is an attribute for alias_attribute #48972

Merged
merged 1 commit into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion activemodel/lib/active_model/attribute_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +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
self.local_attribute_aliases = local_attribute_aliases.merge(new_name.to_s => old_name.to_s)
eagerly_generate_alias_attribute_methods(new_name, old_name)
end

Expand Down Expand Up @@ -370,6 +370,8 @@ def local_attribute_aliases # :nodoc:
end

private
attr_writer :local_attribute_aliases # :nodoc:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added to resolve these failures - https://buildkite.com/rails/rails/builds/98910#018a198e-03ae-4d10-820e-67f72d4b14c0. Also matches the signature for attribute_aliases, which might be better anyway.


def inherited(base) # :nodoc:
super
base.class_eval do
Expand Down
13 changes: 12 additions & 1 deletion activerecord/lib/active_record/attribute_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,18 @@ def alias_attribute_method_definition(code_generator, pattern, new_name, old_nam
!self.instance_method(target_name).owner.is_a?(GeneratedAttributeMethods)
reserved_method_name = ::ActiveRecord::AttributeMethods.dangerous_attribute_methods.include?(target_name)

if manually_defined && !reserved_method_name
if !abstract_class? && !has_attribute?(old_name)
# We only need to issue this deprecation warning once, so we issue it when defining the original reader method.
should_warn = target_name == old_name
if should_warn
ActiveRecord.deprecator.warn(
"#{self} model aliases `#{old_name}`, but #{old_name} is not an attribute. " \
"Starting in Rails 7.2 `, alias_attribute with non-attribute targets will raise. " \
"Use `alias_method :#{new_name}`, :#{old_name} or define the method manually."
)
end
super
elsif manually_defined && !reserved_method_name
aliased_method_redefined_as_well = method_defined_within?(method_name, self)
return if aliased_method_redefined_as_well

Expand Down
89 changes: 89 additions & 0 deletions activerecord/test/cases/attribute_methods_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require "models/minimalistic"
require "models/developer"
require "models/auto_id"
require "models/author"
require "models/boolean"
require "models/computer"
require "models/topic"
Expand Down Expand Up @@ -1316,6 +1317,94 @@ class ChildWithDeprecatedBehaviorResolved < ClassWithDeprecatedAliasAttributeBeh
assert_equal 123_456, object.id_value
end

ClassWithGeneratedAttributeMethodTarget = Class.new(ActiveRecord::Base) do
self.table_name = "topics"
alias_attribute :saved_title, :title_in_database
end

test "#alias_attribute with an _in_database method issues a deprecation warning" do
message = <<~MESSAGE.gsub("\n", " ")
AttributeMethodsTest::ClassWithGeneratedAttributeMethodTarget model aliases `title_in_database`, but title_in_database is not an attribute.
Starting in Rails 7.2 `, alias_attribute with non-attribute targets will raise. Use `alias_method :saved_title`, :title_in_database or define the method manually.
MESSAGE

obj = assert_deprecated(message, ActiveRecord.deprecator) do
ClassWithGeneratedAttributeMethodTarget.new
end
obj.title = "A river runs through it"
assert_nil obj.saved_title
obj.save
assert_equal "A river runs through it", obj.saved_title
end

ClassWithEnumMethodTarget = Class.new(ActiveRecord::Base) do
self.table_name = "books"

attribute :status, :string
enum status: {
pending: "0",
completed: "1",
}
alias_attribute :is_pending?, :pending?
end

test "#alias_attribute with enum method issues a deprecation warning" do
message = <<~MESSAGE.gsub("\n", " ")
AttributeMethodsTest::ClassWithEnumMethodTarget model aliases `pending?`, but pending? is not an attribute.
Starting in Rails 7.2 `, alias_attribute with non-attribute targets will raise.
Use `alias_method :is_pending?`, :pending? or define the method manually.
MESSAGE

obj = assert_deprecated(message, ActiveRecord.deprecator) do
ClassWithEnumMethodTarget.new
end
obj.status = "pending"
assert_predicate obj, :pending?
assert_predicate obj, :is_pending?
end

ClassWithAssociationTarget = Class.new(ActiveRecord::Base) do
self.table_name = "books"
belongs_to :author

alias_attribute :written_by, :author
end

test "#alias_attribute with an association method issues a deprecation warning" do
message = <<~MESSAGE.gsub("\n", " ")
AttributeMethodsTest::ClassWithAssociationTarget model aliases `author`, but author is not an attribute.
Starting in Rails 7.2 `, alias_attribute with non-attribute targets will raise.
Use `alias_method :written_by`, :author or define the method manually.
MESSAGE

obj = assert_deprecated(message, ActiveRecord.deprecator) do
ClassWithAssociationTarget.new
ipc103 marked this conversation as resolved.
Show resolved Hide resolved
end
obj.author = Author.new(name: "Octavia E. Butler")
assert_equal "Octavia E. Butler", obj.written_by.name
end

ClassWithAliasedManuallyDefinedMethod = Class.new(ActiveRecord::Base) do
self.table_name = "books"
alias_attribute :print, :publish

def publish
"Publishing!"
end
end

test "#alias_attribute with a manually defined method issues a deprecation warning" do
message = <<~MESSAGE.gsub("\n", " ")
AttributeMethodsTest::ClassWithAliasedManuallyDefinedMethod model aliases `publish`, but publish is not an attribute.
Starting in Rails 7.2 `, alias_attribute with non-attribute targets will raise.
Use `alias_method :print`, :publish or define the method manually.
MESSAGE

assert_deprecated(message, ActiveRecord.deprecator) do
ClassWithAliasedManuallyDefinedMethod.new
end
end

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