Skip to content

Commit

Permalink
Merge pull request #48844 from Shopify/use-call-args-for-define-proxy…
Browse files Browse the repository at this point in the history
…-method-namespace

Use `call_args` in the `define_proxy_method` namespace.
  • Loading branch information
eileencodes committed Jul 31, 2023
2 parents ad790cb + 5dbc7b4 commit 05d93c7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
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

0 comments on commit 05d93c7

Please sign in to comment.