Skip to content

Commit

Permalink
Fix with_options to allow string key options
Browse files Browse the repository at this point in the history
  • Loading branch information
kamipo committed Feb 5, 2020
1 parent 8cdeeb5 commit a55620f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
17 changes: 14 additions & 3 deletions activesupport/lib/active_support/option_merger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,20 @@ def method_missing(method, *arguments, &block)
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
Expand Down
21 changes: 13 additions & 8 deletions activesupport/test/option_merger_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,21 @@ def setup
@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
Expand All @@ -35,13 +43,11 @@ def test_method_with_options_allows_to_overwrite_options

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

Expand Down Expand Up @@ -75,8 +81,7 @@ def test_nested_method_with_options_containing_hashes_going_deep
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

Expand Down

0 comments on commit a55620f

Please sign in to comment.