Fix `with_options` to allow string key options

This commit is contained in:
Ryuta Kamizono 2020-02-05 17:22:25 +09:00
parent 8cdeeb5b26
commit a55620f3fa
2 changed files with 27 additions and 11 deletions

View File

@ -24,9 +24,20 @@ module ActiveSupport
options = @options
end
if options
@context.__send__(method, *arguments, **options, &block)
else
invoke_method(method, arguments, options, &block)
end
if RUBY_VERSION >= "2.7"
def invoke_method(method, arguments, options, &block)
if options
@context.__send__(method, *arguments, **options, &block)
else
@context.__send__(method, *arguments, &block)
end
end
else
def invoke_method(method, arguments, options, &block)
arguments << options if options
@context.__send__(method, *arguments, &block)
end
end

View File

@ -8,13 +8,21 @@ class OptionMergerTest < ActiveSupport::TestCase
@options = { hello: "world" }
end
def test_method_with_options_merges_string_options
local_options = { "cool" => true }
with_options(@options) do |o|
assert_equal local_options, method_with_options(local_options)
assert_equal @options.merge(local_options), o.method_with_options(local_options)
end
end
def test_method_with_options_merges_options_when_options_are_present
local_options = { cool: true }
with_options(@options) do |o|
assert_equal local_options, method_with_options(local_options)
assert_equal @options.merge(local_options),
o.method_with_options(local_options)
assert_equal @options.merge(local_options), o.method_with_options(local_options)
assert_equal @options.merge(local_options), o.method_with_kwargs(local_options)
assert_equal @options.merge(local_options), o.method_with_kwargs_only(local_options)
end
@ -35,13 +43,11 @@ class OptionMergerTest < ActiveSupport::TestCase
with_options(@options) do |o|
assert_equal local_options, method_with_options(local_options)
assert_equal @options.merge(local_options),
o.method_with_options(local_options)
assert_equal @options.merge(local_options), o.method_with_options(local_options)
assert_equal local_options, o.method_with_options(local_options)
end
with_options(local_options) do |o|
assert_equal local_options.merge(@options),
o.method_with_options(@options)
assert_equal local_options.merge(@options), o.method_with_options(@options)
end
end
@ -75,8 +81,7 @@ class OptionMergerTest < ActiveSupport::TestCase
def test_nested_method_with_options_using_lambda
local_lambda = lambda { { lambda: true } }
with_options(@options) do |o|
assert_equal @options.merge(local_lambda.call),
o.method_with_options(local_lambda).call
assert_equal @options.merge(local_lambda.call), o.method_with_options(local_lambda).call
end
end