1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #38236 from HParker/fix-helper_method-kwargs

prevent helper_method from calling to_hash
This commit is contained in:
Aaron Patterson 2020-01-14 16:00:44 -08:00 committed by GitHub
commit ace7060685
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 10 deletions

View file

@ -66,13 +66,10 @@ module AbstractController
methods.each do |method|
_helpers.class_eval <<-ruby_eval, file, line
def #{method}(*args, **kwargs, &blk) # def current_user(*args, **kwargs, &blk)
if kwargs.empty? # if kwargs.empty?
controller.send(%(#{method}), *args, &blk) # controller.send(:current_user, *args, &blk)
else # else
controller.send(%(#{method}), *args, **kwargs, &blk) # controller.send(:current_user, *args, **kwargs, &blk)
end # end
end # end
def #{method}(*args, &blk) # def current_user(*args, &blk)
controller.send(%(#{method}), *args, &blk) # controller.send(:current_user, *args, &blk)
end # end
ruby2_keywords(%(#{method})) if respond_to?(:ruby2_keywords, true)
ruby_eval
end
end

View file

@ -104,6 +104,8 @@ class HelperTest < ActiveSupport::TestCase
class TestController < ActionController::Base
attr_accessor :delegate_attr
def delegate_method() end
def delegate_method_arg(arg); arg; end
def delegate_method_kwarg(hi:); hi; end
end
def setup
@ -111,9 +113,7 @@ class HelperTest < ActiveSupport::TestCase
@symbol = (@@counter ||= "A0").succ.dup
# Generate new controller class.
controller_class_name = "Helper#{@symbol}Controller"
eval("class #{controller_class_name} < TestController; end")
@controller_class = self.class.const_get(controller_class_name)
@controller_class = Class.new(TestController)
# Set default test helper.
self.test_helper = LocalAbcHelper
@ -130,6 +130,29 @@ class HelperTest < ActiveSupport::TestCase
assert_includes master_helper_methods, :delegate_method
end
def test_helper_method_arg
assert_nothing_raised { @controller_class.helper_method :delegate_method_arg }
assert_equal({ hi: :there }, @controller_class.new.helpers.delegate_method_arg({ hi: :there }))
end
def test_helper_method_arg_does_not_call_to_hash
assert_nothing_raised { @controller_class.helper_method :delegate_method_arg }
my_class = Class.new do
def to_hash
{ hi: :there }
end
end.new
assert_equal(my_class, @controller_class.new.helpers.delegate_method_arg(my_class))
end
def test_helper_method_kwarg
assert_nothing_raised { @controller_class.helper_method :delegate_method_kwarg }
assert_equal(:there, @controller_class.new.helpers.delegate_method_kwarg(hi: :there))
end
def test_helper_attr
assert_nothing_raised { @controller_class.helper_attr :delegate_attr }
assert_includes master_helper_methods, :delegate_attr