Skip to content

Commit 9c6fe5b

Browse files
authored
Merge pull request rails#50138 from rails/rm-remove-duplication
Remove duplication between `alias_attribute_method_definition` and `define_proxy_call`
2 parents 5407776 + 139e806 commit 9c6fe5b

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

activemodel/lib/active_model/attribute_methods.rb

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -225,28 +225,13 @@ def alias_attribute_method_definition(code_generator, pattern, new_name, old_nam
225225
method_name = pattern.method_name(new_name).to_s
226226
target_name = pattern.method_name(old_name).to_s
227227
parameters = pattern.parameters
228-
mangled_name = target_name
229228

230-
unless NAME_COMPILABLE_REGEXP.match?(target_name)
231-
mangled_name = "__temp__#{target_name.unpack1("h*")}"
232-
end
229+
mangled_name = build_mangled_name(target_name)
233230

234-
code_generator.define_cached_method(method_name, as: mangled_name, namespace: :alias_attribute) do |batch|
235-
body = if CALL_COMPILABLE_REGEXP.match?(target_name)
236-
"self.#{target_name}(#{parameters || ''})"
237-
else
238-
call_args = [":'#{target_name}'"]
239-
call_args << parameters if parameters
240-
"send(#{call_args.join(", ")})"
241-
end
231+
call_args = []
232+
call_args << parameters if parameters
242233

243-
modifier = parameters == FORWARD_PARAMETERS ? "ruby2_keywords " : ""
244-
245-
batch <<
246-
"#{modifier}def #{mangled_name}(#{parameters || ''})" <<
247-
body <<
248-
"end"
249-
end
234+
define_call(code_generator, method_name, target_name, mangled_name, parameters, call_args, namespace: :alias_attribute)
250235
end
251236

252237
# Is +new_name+ an alias?
@@ -419,20 +404,31 @@ def attribute_method_patterns_matching(method_name)
419404
# using the given `extra` args. This falls back on `send`
420405
# if the called name cannot be compiled.
421406
def define_proxy_call(code_generator, name, proxy_target, parameters, *call_args, namespace:)
407+
mangled_name = build_mangled_name(name)
408+
409+
call_args.map!(&:inspect)
410+
call_args << parameters if parameters
411+
namespace = :"#{namespace}_#{proxy_target}_#{call_args.join("_")}}"
412+
413+
define_call(code_generator, name, proxy_target, mangled_name, parameters, call_args, namespace: namespace)
414+
end
415+
416+
def build_mangled_name(name)
422417
mangled_name = name
418+
423419
unless NAME_COMPILABLE_REGEXP.match?(name)
424420
mangled_name = "__temp__#{name.unpack1("h*")}"
425421
end
426422

427-
call_args.map!(&:inspect)
428-
call_args << parameters if parameters
429-
namespace = :"#{namespace}_#{proxy_target}_#{call_args.join("_")}}"
423+
mangled_name
424+
end
430425

426+
def define_call(code_generator, name, target_name, mangled_name, parameters, call_args, namespace:)
431427
code_generator.define_cached_method(name, as: mangled_name, namespace: namespace) do |batch|
432-
body = if CALL_COMPILABLE_REGEXP.match?(proxy_target)
433-
"self.#{proxy_target}(#{call_args.join(", ")})"
428+
body = if CALL_COMPILABLE_REGEXP.match?(target_name)
429+
"self.#{target_name}(#{call_args.join(", ")})"
434430
else
435-
call_args.unshift(":'#{proxy_target}'")
431+
call_args.unshift(":'#{target_name}'")
436432
"send(#{call_args.join(", ")})"
437433
end
438434

0 commit comments

Comments
 (0)