From cbf7893ae9ff947863227890fcf0eb95c52b958e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20B=C3=BChlmann?= Date: Tue, 12 Jan 2021 15:00:39 +0100 Subject: [PATCH] Fix HelperProxy keyword argument passing for Ruby 3 --- lib/draper/helper_proxy.rb | 8 ++++---- spec/draper/helper_proxy_spec.rb | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/draper/helper_proxy.rb b/lib/draper/helper_proxy.rb index 8645a138..6c1ceeaa 100644 --- a/lib/draper/helper_proxy.rb +++ b/lib/draper/helper_proxy.rb @@ -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 @@ -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 diff --git a/spec/draper/helper_proxy_spec.rb b/spec/draper/helper_proxy_spec.rb index de4d030d..e3404058 100644 --- a/spec/draper/helper_proxy_spec.rb +++ b/spec/draper/helper_proxy_spec.rb @@ -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)