Skip to content

Commit

Permalink
Preserve query parameters in pagination links from POST request
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed May 29, 2023
1 parent ae522de commit 61ffb68
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/will_paginate/view_helpers/action_view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,12 @@ def url(page)
end

def merge_get_params(url_params)
if @template.respond_to? :request and @template.request and @template.request.get?
symbolized_update(url_params, @template.params, GET_PARAMS_BLACKLIST)
if @template.respond_to?(:request) and @template.request
if @template.request.get?
symbolized_update(url_params, @template.params, GET_PARAMS_BLACKLIST)
elsif @template.request.respond_to?(:query_parameters)
symbolized_update(url_params, @template.request.query_parameters, GET_PARAMS_BLACKLIST)
end
end
url_params
end
Expand Down
16 changes: 15 additions & 1 deletion spec/view_helpers/action_view_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,13 @@ def renderer.gap() '<span class="my-gap">~~</span>' end
assert_no_links_match /p0wned/
end

it "should not preserve parameters on POST" do
it "should only preserve query parameters on POST" do
request.post
request.params :foo => 'bar'
request.query_parameters = { :hello => 'world' }
paginate
assert_no_links_match /foo=bar/
assert_links_match /hello=world/
end

it "should add additional parameters to links" do
Expand Down Expand Up @@ -465,6 +467,18 @@ def params(more = nil)
@params.update(more) if more
ActionController::Parameters.new(@params)
end

def query_parameters
if get?
params
else
ActionController::Parameters.new(@query_parameters)
end
end

def query_parameters=(more)
@query_parameters = more.with_indifferent_access
end

def host_with_port
'example.com'
Expand Down

0 comments on commit 61ffb68

Please sign in to comment.