Skip to content

Commit

Permalink
Fix HelperProxy keyword argument passing for Ruby 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Bühlmann committed Jan 26, 2021
1 parent da6cd97 commit cbf7893
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/draper/helper_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ def initialize(view_context)
end

# Sends helper methods to the view context.
def method_missing(method, *args, &block)
def method_missing(method, *args, **kwargs, &block)
self.class.define_proxy method
send(method, *args, &block)
send(method, *args, **kwargs, &block)
end

# Checks if the context responds to an instance method, or is able to
Expand All @@ -28,8 +28,8 @@ def respond_to_missing?(method, include_private = false)
private

def self.define_proxy(name)
define_method name do |*args, &block|
view_context.send(name, *args, &block)
define_method name do |*args, **kwargs, &block|
view_context.send(name, *args, **kwargs, &block)
end
end
end
Expand Down
14 changes: 14 additions & 0 deletions spec/draper/helper_proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ module Draper
expect(helper_proxy.foo(:passed)).to be :passed
end

it "proxies methods with keyword arguments to the view context" do
view_context = double

# It seems we can't stub a method using `allow` with keyword
# arguments, so we just define it directly
def view_context.foo(bar:)
bar
end

helper_proxy = HelperProxy.new(view_context)

expect(helper_proxy.foo(bar: "baz")).to eq "baz"
end

it "passes blocks" do
view_context = double
helper_proxy = HelperProxy.new(view_context)
Expand Down

0 comments on commit cbf7893

Please sign in to comment.