-
Notifications
You must be signed in to change notification settings - Fork 21.9k
Allow current_page?
to match against specific HTTP method(s) with a method:
option
#55286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
… `method:` option
method_matches = if method == :get | ||
request.get? || request.head? | ||
else | ||
request_method = request.method.downcase.to_sym | ||
if method.is_a?(Array) | ||
method << :head if method.include?(:get) | ||
method.include?(request_method) | ||
else | ||
method == request_method | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The extra nesting of this if statement could probably be removed?
method_matches = if method == :get | |
request.get? || request.head? | |
else | |
request_method = request.method.downcase.to_sym | |
if method.is_a?(Array) | |
method << :head if method.include?(:get) | |
method.include?(request_method) | |
else | |
method == request_method | |
end | |
end | |
method_matches = if method == :get | |
request.get? || request.head? | |
elsif method.is_a?(Array) | |
request_method = request.method.downcase.to_sym | |
method << :head if method.include?(:get) | |
method.include?(request_method) | |
else | |
method == request_method | |
end |
Or maybe even:
method_matches = if method == :get | |
request.get? || request.head? | |
else | |
request_method = request.method.downcase.to_sym | |
if method.is_a?(Array) | |
method << :head if method.include?(:get) | |
method.include?(request_method) | |
else | |
method == request_method | |
end | |
end | |
method_matches = case method | |
when :get | |
request.get? || request.head? | |
when Array | |
request_method = request.method.downcase.to_sym | |
method << :head if method.include?(:get) | |
method.include?(request_method) | |
else | |
method == request_method | |
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Argh, nevermind. request_method
is used in the last case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, I couldn't find the prettiest logic myself 😞 I was wanting to preserve the existing/default as a fast-path and the else
block is "new behavior".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... and I'm trying to avoid creating an array unnecessarily as I could coerce everything to Array(method)
but I think I've been warned off that before 😁
Maybe I'm dense, and this change is probably fine, but I don't understand in which case you'd need pagination helpers on a POST. |
@byroot |
@byroot The That could be pagination, but my targeted need is when the navigation includes "New Post" or "Edit Post" links that render a form that could be submitted and have validation errors. In those cases, there can still be a requirement for an "active" state on the navigation when rendering validation errors. That's not possible without a change like this PR. |
Yeah, sorry I had a bit of a brain fart. I skimmed over the historical issues, seems like that behavior was almost immediately regretted and people wanted it reverted but nothing happened. I need to find some clear headed time to see if there is an elegant solution to this. |
Motivation / Background
The
current_page?
helper has historically only matched onGET
andHEAD
requests. This creates challenges when rendering content around validation errors on POST/PUT/PATCH actions.This PR adds an optional
method:
option that allows passing an explicit http method (or methods) to thecurrent_page?
helper to match specific http verbs.This makes it easier to render simple navigations like this:
This is necessary because both the
PostsController#index
andPostsController#create
share the same URL path but are differentiated by their HTTP method.Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]