Skip to content

Commit

Permalink
Use arguments forwarding in the delegate code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
kamipo committed Feb 7, 2021
1 parent 978308a commit f9cc3c8
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 14 deletions.
6 changes: 2 additions & 4 deletions actionpack/lib/action_dispatch/testing/integration.rb
Expand Up @@ -363,13 +363,11 @@ def remove! # :nodoc:
reset_html_document = "@html_document = nil"
end

definition = RUBY_VERSION >= "2.7" ? "..." : "*args"

module_eval <<~RUBY, __FILE__, __LINE__ + 1
def #{method}(#{definition})
def #{method}(...)
#{reset_html_document}
result = integration_session.#{method}(#{definition})
result = integration_session.#{method}(...)
copy_session_variables!
result
end
Expand Down
5 changes: 2 additions & 3 deletions activerecord/lib/active_record/relation/delegation.rb
Expand Up @@ -61,10 +61,9 @@ def generate_method(method)
return if method_defined?(method)

if /\A[a-zA-Z_]\w*[!?]?\z/.match?(method) && !DELEGATION_RESERVED_METHOD_NAMES.include?(method.to_s)
definition = RUBY_VERSION >= "2.7" ? "..." : "*args, &block"
module_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{method}(#{definition})
scoping { klass.#{method}(#{definition}) }
def #{method}(...)
scoping { klass.#{method}(...) }
end
RUBY
else
Expand Down
Expand Up @@ -199,13 +199,7 @@ def delegate(*methods, to: nil, prefix: nil, allow_nil: nil, private: nil)

# Attribute writer methods only accept one argument. Makes sure []=
# methods still accept two arguments.
definition = if /[^\]]=$/.match?(method)
"arg"
elsif RUBY_VERSION >= "2.7"
"..."
else
"*args, &block"
end
definition = /[^\]]=\z/.match?(method) ? "arg" : "..."

# The following generated method calls the target exactly once, storing
# the returned value in a dummy variable.
Expand Down

0 comments on commit f9cc3c8

Please sign in to comment.