Skip to content

Commit

Permalink
added :params and :anchor variables to control the generation of arbi…
Browse files Browse the repository at this point in the history
…trary URLs per pagy instance
  • Loading branch information
ddnexus committed May 30, 2018
1 parent 62fd6c8 commit d185461
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/api/pagy.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ These are the non-core variables: as for the core-variables they can be set glob
|--------------:|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:------------|
| `:size` | the size of the page links to show: array of initial pages, before current page, after current page, final pages. (see also [Control the page links](../how-to.md#controlling-the-page-links))_ | `[1,4,4,1]` |
| `:page_param` | the name of the page param name used in the url. (see [Customizing the page param](../how-to.md#customizing-the-page-param) | `:page` |
| `:params` | the arbitrary param hash to add to the url. (see [Customizing the params](../how-to.md#customizing-the-params) | `{}` |
| `:anchor` | the arbitrary anchor string (including the "#") to add to the url. (see [Customizing the page param](../how-to.md#customizing-the-params) | `nil` |
| `:link_extra` | the extra attributes string (formatted as a valid HTML attribute/value pairs) added to the page links | `nil` |
| `:item_path` | the dictionary path used by the `pagy_info` method to lookup the item/model name | `nil` |

Expand Down
6 changes: 6 additions & 0 deletions docs/how-to.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@ def pagy_get_params(params)
params.except(:anything, :not, :useful).merge!(something: 'more useful')
end
```
You can also use the `:param` and : `:anchor` non core variables to add arbitrary params to the URLs of the pages. For example:

```ruby
@pagy, @records = pagy(some_scope, params: {custom: 'param'}, anchor: '#your-anchor')
```
__IMPORTANT__: For performance reasons the `:anchor` string must include the `#`.

## Customizing the URL

Expand Down
2 changes: 1 addition & 1 deletion lib/pagy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class OutOfRangeError < StandardError; end
def self.root; Pathname.new(__FILE__).dirname end

# default core vars
VARS = { page:1, items:20, outset:0, size:[1,4,4,1], page_param: :page }
VARS = { page:1, items:20, outset:0, size:[1,4,4,1], page_param: :page, params: {} }

# default I18n vars
zero_one = [:zero, :one] ; I18N = { file: Pagy.root.join('locales', 'pagy.yml').to_s, plurals: -> (c) {(zero_one[c] || :other).to_s.freeze} }
Expand Down
4 changes: 2 additions & 2 deletions lib/pagy/frontend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def pagy_info(pagy)

# this works with all Rack-based frameworks (Sinatra, Padrino, Rails, ...)
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))}"
params = request.GET.merge(pagy.vars[:page_param] => page).merge!(pagy.vars[:params])
"#{request.path}?#{Rack::Utils.build_nested_query(pagy_get_params(params))}#{pagy.vars[:anchor]}"
end


Expand Down

0 comments on commit d185461

Please sign in to comment.