Allow easier debugging of Action Dispatch requests

Kernel#method was redefined so one couldn't do for instance.

method(:POST).source_location

Now when called without arguments it returns the method of the
request and when called with arguments it uses Kernel#method

Which makes debugging easier

Co-authored-by: Joé Dupuis <joe@dupuis.io>
This commit is contained in:
Dorian Marié 2022-05-18 12:55:28 -07:00
parent b5630e232b
commit 190d1424a4
2 changed files with 21 additions and 2 deletions

@ -195,9 +195,20 @@ def request_method_symbol
# Returns the original value of the environment's REQUEST_METHOD,
# even if it was overridden by middleware. See #request_method for
# more information.
def method
@method ||= check_method(get_header("rack.methodoverride.original_method") || get_header("REQUEST_METHOD"))
#
# For debugging purposes, when called with arguments this method will
# fallback to Object#method
def method(*args)
if args.empty?
@method ||= check_method(
get_header("rack.methodoverride.original_method") ||
get_header("REQUEST_METHOD")
)
else
super
end
end
ruby2_keywords(:method)
# Returns a symbol form of the #method.
def method_symbol

@ -783,6 +783,14 @@ class RequestMethod < BaseRequestTest
end
end
end
test "delegates to Object#method if an argument is passed" do
request = stub_request
assert_nothing_raised do
request.method(:POST)
end
end
end
class RequestFormat < BaseRequestTest