From d18546178a99835aa5e134d51d8a9bbc15760995 Mon Sep 17 00:00:00 2001
From: Domizio Demichelis
Date: Wed, 30 May 2018 22:53:32 +0700
Subject: [PATCH] added :params and :anchor variables to control the generation
of arbitrary URLs per pagy instance
---
docs/api/pagy.md | 2 ++
docs/how-to.md | 6 ++++++
lib/pagy.rb | 2 +-
lib/pagy/frontend.rb | 4 ++--
4 files changed, 11 insertions(+), 3 deletions(-)
diff --git a/docs/api/pagy.md b/docs/api/pagy.md
index e7539e624..cb8f50b98 100644
--- a/docs/api/pagy.md
+++ b/docs/api/pagy.md
@@ -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` |
diff --git a/docs/how-to.md b/docs/how-to.md
index befda7d09..cf863082f 100644
--- a/docs/how-to.md
+++ b/docs/how-to.md
@@ -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
diff --git a/lib/pagy.rb b/lib/pagy.rb
index fcbbca9ff..479b23e48 100644
--- a/lib/pagy.rb
+++ b/lib/pagy.rb
@@ -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} }
diff --git a/lib/pagy/frontend.rb b/lib/pagy/frontend.rb
index 425b7431e..f46d06c2b 100644
--- a/lib/pagy/frontend.rb
+++ b/lib/pagy/frontend.rb
@@ -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