From a55620f3fa89d957817349e5170f686d505eeee4 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Wed, 5 Feb 2020 17:22:25 +0900 Subject: [PATCH] Fix `with_options` to allow string key options --- .../lib/active_support/option_merger.rb | 17 ++++++++++++--- activesupport/test/option_merger_test.rb | 21 ++++++++++++------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/activesupport/lib/active_support/option_merger.rb b/activesupport/lib/active_support/option_merger.rb index 7feb919aef..4993bf9fd9 100644 --- a/activesupport/lib/active_support/option_merger.rb +++ b/activesupport/lib/active_support/option_merger.rb @@ -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 diff --git a/activesupport/test/option_merger_test.rb b/activesupport/test/option_merger_test.rb index 668de7c81b..a017abb3b4 100644 --- a/activesupport/test/option_merger_test.rb +++ b/activesupport/test/option_merger_test.rb @@ -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