Skip to content

Commit

Permalink
Merge pull request #45116 from ghiculescu/helper_method_backtrace
Browse files Browse the repository at this point in the history
  • Loading branch information
byroot committed Jul 17, 2023
1 parent 704cac8 commit c05a88d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
11 changes: 7 additions & 4 deletions actionpack/lib/abstract_controller/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,13 @@ def helper_method(*methods)
file, line = location.path, location.lineno

methods.each do |method|
_helpers_for_modification.class_eval <<~ruby_eval, file, line
def #{method}(*args, &block) # def current_user(*args, &block)
controller.send(:'#{method}', *args, &block) # controller.send(:'current_user', *args, &block)
end # end
# def current_user(*args, &block)
# controller.send(:'current_user', *args, &block)
# end
_helpers_for_modification.class_eval <<~ruby_eval.lines.map(&:strip).join(";"), file, line
def #{method}(*args, &block)
controller.send(:'#{method}', *args, &block)
end
ruby2_keywords(:'#{method}')
ruby_eval
end
Expand Down
13 changes: 13 additions & 0 deletions actionpack/test/controller/helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ class TestController < ActionController::Base
def delegate_method() end
def delegate_method_arg(arg); arg; end
def delegate_method_kwarg(hi:); hi; end
def method_that_raises
raise "an error occurred"
end
end

def setup
Expand Down Expand Up @@ -149,6 +152,16 @@ def test_helper_method_kwarg
assert_equal(:there, @controller_class.new.helpers.delegate_method_kwarg(hi: :there))
end

def test_helper_method_with_error_has_correct_backgrace
@controller_class.helper_method :method_that_raises
expected_backtrace_pattern = "#{__FILE__}:#{__LINE__ - 1}"

error = assert_raises(RuntimeError) do
@controller_class.new.helpers.method_that_raises
end
assert_not_nil error.backtrace.find { |line| line.include?(expected_backtrace_pattern) }
end

def test_helper_attr
assert_nothing_raised { @controller_class.helper_attr :delegate_attr }
assert_includes master_helper_methods, :delegate_attr
Expand Down

0 comments on commit c05a88d

Please sign in to comment.