Skip to content

Commit

Permalink
better forward improvements with pagy_url_for receiving the pagy inst…
Browse files Browse the repository at this point in the history
…ance instead of the single pagy_param
  • Loading branch information
ddnexus committed May 30, 2018
1 parent 40fc959 commit 62fd6c8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/api/frontend.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Displaying Products <b>476-500</b> of <b>1000</b> in total
See also [Using the pagy_info helper](../how-to.md#using-the-pagy_info-helper).


### pagy_url_for(n, page_param)
### pagy_url_for(page, page_param)

This method is called internally in order to produce the url of a page by passing it its number. For standard usage it works out of the box and you can just ignore it.

Expand Down
8 changes: 4 additions & 4 deletions docs/how-to.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ When you need somethig more radical with the URL than just massaging the params,
The following is a Rails-specific alternative that supports fancy-routes (e.g. `get 'your_route(/:page)' ...` that produce paths like `your_route/23` instead of `your_route?page=23`):

```ruby
def pagy_url_for(n, page_param)
params = request.query_parameters.merge(:only_path => true, page_param => n)
def pagy_url_for(page, pagy)
params = request.query_parameters.merge(:only_path => true, pagy.vars[:page_param] => page )
url_for(params)
end
```
Expand All @@ -225,8 +225,8 @@ Notice that this overridden method is quite slower than the original because it
You may need to POST a very complex search form that would generate an URL potentially too long to be handled by a browser, and your page links may need to use POST and not GET. In that case you can try this simple solution:

```ruby
def pagy_url_for(n, _)
n
def pagy_url_for(page, _)
page
end
```
That would produce links that look like e.g. `<a href="2">2</a>`. Then you can attach a javascript "click" event on the page links. When triggered, the `href` content (i.e. the page number) should get copied to a hidden `"page"` input and the form should be posted.
Expand Down
6 changes: 3 additions & 3 deletions lib/pagy/frontend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def pagy_info(pagy)


# this works with all Rack-based frameworks (Sinatra, Padrino, Rails, ...)
def pagy_url_for(page, page_param)
params = request.GET.merge(page_param => page)
def pagy_url_for(page, pagy)
params = request.GET.merge(pagy.vars[:page_param] => page)
"#{request.path}?#{Rack::Utils.build_nested_query(pagy_get_params(params))}"
end

Expand All @@ -50,7 +50,7 @@ def pagy_get_params(params) params end
# returns a specialized proc to generate the HTML links
def pagy_link_proc(pagy, lx=''.freeze) # "lx" means "link extra"
p_prev, p_next, p_lx = pagy.prev, pagy.next, pagy.vars[:link_extra]
a, b = %(<a href="#{pagy_url_for(MARKER, pagy.vars[:page_param])}"#{p_lx ? %( #{p_lx}) : ''.freeze}#{lx.empty? ? lx : %( #{lx})}).split(MARKER)
a, b = %(<a href="#{pagy_url_for(MARKER, pagy)}"#{p_lx ? %( #{p_lx}) : ''.freeze}#{lx.empty? ? lx : %( #{lx})}).split(MARKER)
-> (n, text=n, x=''.freeze) { "#{a}#{n}#{b}#{ if n == p_prev ; ' rel="prev"'.freeze
elsif n == p_next ; ' rel="next"'.freeze
else ''.freeze end }#{x.empty? ? x : %( #{x})}>#{text}</a>" }
Expand Down

0 comments on commit 62fd6c8

Please sign in to comment.