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

Use call_args in the define_proxy_method namespace. #48844

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
7 changes: 4 additions & 3 deletions activemodel/lib/active_model/attribute_methods.rb
Expand Up @@ -409,10 +409,11 @@ def define_proxy_call(code_generator, name, proxy_target, parameters, *call_args
mangled_name = "__temp__#{name.unpack1("h*")}"
end

code_generator.define_cached_method(name, as: mangled_name, namespace: :"#{namespace}_#{proxy_target}") do |batch|
call_args.map!(&:inspect)
call_args << parameters if parameters
call_args.map!(&:inspect)
call_args << parameters if parameters
namespace = :"#{namespace}_#{proxy_target}_#{call_args.join("_")}}"

code_generator.define_cached_method(name, as: mangled_name, namespace: namespace) do |batch|
body = if CALL_COMPILABLE_REGEXP.match?(proxy_target)
"self.#{proxy_target}(#{call_args.join(", ")})"
else
Expand Down
17 changes: 17 additions & 0 deletions activerecord/test/cases/attribute_methods_test.rb
Expand Up @@ -1144,6 +1144,23 @@ def some_method_that_is_not_on_super
assert_equal "abcd", model.read_attribute_before_type_cast("new_bank_balance")
end

ToBeLoadedFirst = Class.new(ActiveRecord::Base) do
self.table_name = "topics"
alias_attribute :subject, :author_name
end

ToBeLoadedSecond = Class.new(ActiveRecord::Base) do
self.table_name = "topics"
alias_attribute :subject, :title
end

test "aliases to the same attribute name do not conflict with each other" do
first_model_object = ToBeLoadedFirst.new(author_name: "author 1")
assert_equal("author 1", first_model_object.subject)
second_model_object = ToBeLoadedSecond.new(title: "foo")
assert_equal("foo", second_model_object.subject)
end

ClassWithDeprecatedAliasAttributeBehavior = Class.new(ActiveRecord::Base) do
self.table_name = "topics"
alias_attribute :subject, :title
Expand Down