Skip to content

Commit d0e4ba6

Browse files
committed
a few fixes and doc improvements [ci skip]
1 parent c1ddbd9 commit d0e4ba6

8 files changed

Lines changed: 65 additions & 57 deletions

File tree

β€Ždocs/api/frontend.mdβ€Ž

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ You need this section only if you are going to override a `pagy_nav*` helper or
9090

9191
**Important**: This method is not intended to be overridden, however you could just replace it in your overridden `pagy_nav*` helpers or templates with some generic helper like the rails `link_to`. If you intend to do so, be sure to have a very good reason, since using `pagy_link_proc` is a lot faster than the rails `link_to` (benchmarked at ~22x faster using ~18x less memory on a 20 links nav).
9292

93-
**Warning**: This is a peculiar way to create page links and it works only for that purpose. It is not intended to be used for generic links.
94-
9593
This method returns a specialized proc that you call to produce the page links. The reason it is a two steps process instead of a single method call is performance. Indeed the method calls the potentially expensive `pagy_url_for` only once and generates the proc, then calling the proc will just interpolates the strings passed to it.
9694

9795
Here is how you should use it: in your helper or template call the method to get the proc (just once):
@@ -190,7 +188,7 @@ Pagy::I18n.load(locale: 'de')
190188
Pagy::I18n.load(locale: 'de', filepath: 'path/to/pagy-de.yml')
191189
192190
# load the "de", "en" and "es" built-in locales:
193-
# the first :locale will be used also as the default_locale
191+
# the first :locale will be used also as the default locale
194192
Pagy::I18n.load({locale: 'de'},
195193
{locale: 'en'},
196194
{locale: 'es'})
@@ -222,7 +220,7 @@ That instance variable will be used by the [pagy_t](#pagy_tpath-vars) method inc
222220
223221
When Pagy uses its own i18n implementation, it has only access to the strings in its own files and not in other `I18n` files used by the rest of the app.
224222
225-
That means that if you use the `pagy_info` helper with the specific model names instead of the generic "items" string, you may need to add entries for the models in the pagy dictionary files. For example:
223+
That means that if you use the `pagy_info` or `pagy_items_selector` helpers with the specific model names instead of the generic "items" string, you may need to add entries for the models in the pagy dictionary files. For example:
226224
227225
```yaml
228226
en:

β€Ždocs/extras.mdβ€Ž

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,16 @@ If your app uses Webpacker, ensure that the webpacker `erb` loader is installed:
9292
bundle exec rails webpacker:install:erb
9393
```
9494

95-
Then create a `pagy.js.erb` to render the content of `pagy.js` and add the event listener into it:
95+
Then create a `pagy.js.erb` in order to render the content of `pagy.js` and add the event listener into it:
9696

97-
```
98-
// app/javascript/src/javascripts/pagy.js.erb
97+
```erb
9998
<%= Pagy.root.join('javascripts', 'pagy.js').read %>
10099
window.addEventListener("load", Pagy.init)
101100
```
102-
and import it:
101+
102+
and import it in `app/javascript/application.js`:
103+
103104
```js
104-
// app/javascript/application.js
105105
import '../src/javascripts/pagy.js.erb'
106106
```
107107

@@ -116,7 +116,9 @@ import '../src/javascripts/pagy.js.erb'
116116

117117
### In non-rails apps
118118

119-
Ensure the `pagy/extras/javascripts/pagy.js` script gets served with the page and add an event listener like:
119+
Ensure the `pagy/extras/javascripts/pagy.js` script gets served with the page.
120+
121+
Add an event listener like:
120122

121123
```js
122124
window.addEventListener('load', Pagy.init);

β€Ždocs/extras/foundation.mdβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ with a fast helper:
2020

2121
```erb
2222
<%== pagy_foundation_nav(@pagy) %>
23+
<%== pagy_foundation_nav_js(@pagy) %>
24+
<%== pagy_foundation_combo_nav_js(@pagy) %>
2325
```
2426

2527
or with a template:

β€Ždocs/extras/overflow.mdβ€Ž

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ Pagy::VARS[:overflow] = :exception
2828

2929
## Files
3030

31-
-[overflow.rb](https://github.com/ddnexus/pagy/blob/master/lib/pagy/extras/overflow.rb)
31+
- [overflow.rb](https://github.com/ddnexus/pagy/blob/master/lib/pagy/extras/overflow.rb)
3232

3333
## Variables
3434

35-
| Variable | Description | Default |
36-
|:------------|:---------------------------------------------------------|:--------------|
37-
| `:overflow` | one of `:last_page`, `:empty_page` or `:exception` modes | `:empty_page` |
35+
| Variable | Description | Default |
36+
|:------------|:------------------------------------------------------------------------------------|:--------------|
37+
| `:overflow` | the modes in case of overflowing page (`:last_page`, `:empty_page` or `:exception`) | `:empty_page` |
3838

3939
As usual, depending on the scope of the customization, you have a couple of options to set the variables:
4040

@@ -50,32 +50,15 @@ Pagy::VARS[:overflow] = :empty_page
5050

5151
These are the modes accepted by the `:overflow` variable:
5252

53-
### :last_page
54-
55-
**Notice**: Not available for `Pagy::Countless` instances.
56-
57-
It is useful in apps with an UI, in order to avoid to redirect to the last page.
58-
59-
Regardless the overflowing page requested, Pagy will set the page to the last page and paginate exactly as if the last page has been requested. For example:
60-
61-
```ruby
62-
# no exception passing an overflowing page (Default mode :last_page)
63-
pagy = Pagy.new(count: 100, page: 100)
64-
65-
pagy.overflow? #=> true
66-
pagy.vars[:page] #=> 100 (requested page)
67-
pagy.page #=> 5 (current/last page)
68-
pagy.last == pagy.page #=> true
69-
```
70-
7153
### :empty_page
7254

7355
This is the default mode; it will paginate the actual requested page, which - being overflowing - is empty. It is useful with APIs, where the client expects an empty set of results in order to stop requesting further pages.
7456

7557
Example for `Pagy` instance:
7658

7759
```ruby
78-
pagy = Pagy.new(count: 100, page: 100, overflow: :empty_page)
60+
# no exception passing an overflowing page
61+
pagy = Pagy.new(count: 100, page: 100)
7962

8063
pagy.overflow? #=> true
8164
pagy.vars[:page] #=> 100 (requested page)
@@ -96,7 +79,7 @@ Example for `Pagy::Countless` instance:
9679
require 'pagy/countless'
9780
require 'pagy/extras/overflow'
9881

99-
pagy = Pagy::Countless.new(count: 100, page: 100, overflow: :empty_page).finalize(0)
82+
pagy = Pagy::Countless.new(count: 100, page: 100).finalize(0)
10083

10184
pagy.overflow? #=> true
10285
pagy.vars[:page] #=> 100 (requested page)
@@ -111,9 +94,34 @@ pagy.to #=> 0
11194
pagy.series #=> [] (no pages)
11295
```
11396

97+
### :last_page
98+
99+
**Notice**: Not available for `Pagy::Countless` instances since for countless instances the last page is not known.
100+
101+
It is useful in apps with an UI, in order to avoid to redirect to the last page.
102+
103+
Regardless the overflowing page requested, Pagy will set the page to the last page and paginate exactly as if the last page has been requested. For example:
104+
105+
```ruby
106+
pagy = Pagy.new(count: 100, page: 100, overflow: :last_page)
107+
108+
pagy.overflow? #=> true
109+
pagy.vars[:page] #=> 100 (requested page)
110+
pagy.page #=> 5 (current/last page)
111+
pagy.last == pagy.page #=> true
112+
```
113+
114114
### :exception
115115

116-
This mode raises the `Pagy::OverflowError` as usual, so you can rescue from and do what is needed. It is useful when you need to use your own custom mode even in presence of this extra (which would not raise any error).
116+
This mode raises the `Pagy::OverflowError` as usual, so you can rescue from and implement your own custom mode even in presence of this extra.
117+
118+
```ruby
119+
begin
120+
pagy = Pagy.new(count: 100, page: 100, overflow: :exception)
121+
rescue Pagy::OverflowError => e
122+
...
123+
end
124+
```
117125

118126
## Methods
119127

β€Ždocs/extras/semantic.mdβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ with a fast helper:
2020

2121
```erb
2222
<%== pagy_semantic_nav(@pagy) %>
23-
<%== pagy_semantic_combo_nav_js(@pagy) %>
2423
<%== pagy_semantic_nav_js(@pagy) %>
24+
<%== pagy_semantic_combo_nav_js(@pagy) %>
2525
```
2626

2727
## Files

β€Ždocs/extras/support.mdβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ require 'pagy/extras/support'
1717

1818
## Support for alternative pagination types and features
1919

20-
Besides the classic navbar pagination, the `compact` and the `responsive` UI components, Pagy offers a few helpers to support a few alternative types of pagination and related features.
20+
Besides the classic `pagy*_nav` pagination, the `pagy*_nav_js` and the `pagy*_combo_nav_js` UI components, Pagy offers a few helpers to support a few alternative types of pagination and related features.
2121

2222
### Countless
2323

β€Ždocs/how-to.mdβ€Ž

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Pagy works out of the box assuming that:
8080
Pagy can also work in any other scenario assuming that:
8181

8282
- If your framework doesn't have a `params` method you may need to define the `params` method or override the `pagy_get_vars` (which uses the `params` method) in your controller
83-
- If the collection you are paginating doesn't respond to `offset` and `limit` you may need to override the `pagy_get_items` method in your controller (to get the items out of your specific collection) or use a specific extra if available (e.g. `array`, `searchkick`, ...)
83+
- If the collection you are paginating doesn't respond to `offset` and `limit` you may need to override the `pagy_get_items` method in your controller (to get the items out of your specific collection) or use a specific extra if available (e.g. `array`, `searchkick`, `elasticsearch_rais`)
8484
- If your framework doesn't have a `request` method you may need to override the `pagy_url_for` (which uses `Rack` and `request`) in your view
8585

8686
**Notice**: the total overriding you may need is usually just a handful of lines at worse, and it doesn't need monkey patching or writing any sub-class or module.
@@ -165,7 +165,7 @@ You can also override the `pagy_get_vars` if you need some special way to get th
165165
If you need to customize some HTML attribute of the page links, you may not need to override the `pagy_nav*` helper. It might be enough to pass some extra attribute string with the `:link_extra` variable. For example:
166166

167167
```ruby
168-
# for all the Pagy instance
168+
# for all the Pagy instances
169169
Pagy::VARS[:link_extra] = 'data-remote="true" class="my-class"'
170170

171171
# for a single Pagy instance (if you use the Pagy::Backend#pagy method)
@@ -189,13 +189,13 @@ def pagy_get_params(params)
189189
end
190190
```
191191

192-
You can also use the `:param` and : `:anchor` non core variables to add arbitrary params to the URLs of the pages. For example:
192+
You can also use the `:param` and : `:anchor` variables to add arbitrary params to the URLs of the pages. For example:
193193

194194
```ruby
195195
@pagy, @records = pagy(some_scope, params: {custom: 'param'}, anchor: '#your-anchor')
196196
```
197197

198-
**IMPORTANT**: For performance reasons the `:anchor` string must include the `#`.
198+
**IMPORTANT**: For performance reasons the `:anchor` string must include the `"#"`.
199199

200200
## Customizing the URL
201201

@@ -353,14 +353,14 @@ These helpers take the Pagy object and return the HTML string with the paginatio
353353
354354
**Notice**: the [extras](extras.md) add a few other helpers that you can use the same way, in order to get added features (e.g. bootstrap compatibility, responsiveness, compact layouts, etc.)
355355
356-
| Extra | Helpers |
357-
|:-------------------------------------|:-------------------------------------------------------------------------------------|
358-
| [bootstrap](extras/bootstrap.md) | `pagy_bootstrap_nav`, `pagy_bootstrap_combo_nav_js`, `pagy_bootstrap_nav_js` |
359-
| [bulma](extras/bulma.md) | `pagy_bulma_nav`, `pagy_bulma_combo_nav_js`, `pagy_bulma_nav_js` |
360-
| [foundation](extras/foundation.md) | `pagy_foundation_nav`, `pagy_foundation_combo_nav_js`, `pagy_foundation_nav_js` |
361-
| [materialize](extras/materialize.md) | `pagy_materialize_nav`, `pagy_materialize_combo_nav_js`, `pagy_materialize_nav_js` |
362-
| [navs](extras/navs.md) | `pagy_combo_nav_js`, `pagy_nav_js` |
363-
| [semantic](extras/semantic.md) | `pagy_semantic_nav`, `pagy_semantic_combo_nav_js`, `pagy_semantic_nav_js` |
356+
| Extra | Helpers |
357+
|:-------------------------------------|:-----------------------------------------------------------------------------------|
358+
| [bootstrap](extras/bootstrap.md) | `pagy_bootstrap_nav`, `pagy_bootstrap_nav_js`, `pagy_bootstrap_combo_nav_js` |
359+
| [bulma](extras/bulma.md) | `pagy_bulma_nav`, `pagy_bulma_nav_js`, `pagy_bulma_combo_nav_js` |
360+
| [foundation](extras/foundation.md) | `pagy_foundation_nav`, `pagy_foundation_nav_js`, `pagy_foundation_combo_nav_js` |
361+
| [materialize](extras/materialize.md) | `pagy_materialize_nav`, `pagy_materialize_nav_js`, `pagy_materialize_combo_nav_js` |
362+
| [navs](extras/navs.md) | `pagy_nav_js`, `pagy_combo_nav_js` |
363+
| [semantic](extras/semantic.md) | `pagy_semantic_nav`, `pagy_semantic_nav_js`, `pagy_semantic_combo_nav_js` |
364364
365365
Helpers are the preferred choice (over templates) for their performance. If you need to override a `pagy_nav*` helper you can copy and paste it in your helper and edit it there. It is a simple concatenation of strings with a very simple logic.
366366
@@ -496,7 +496,7 @@ Here are a few options for manually handling the error in apps:
496496
497497
private
498498
499-
def redirect_to_last_page(e)
500-
redirect_to url_for(page: e.pagy.last), notice: "Page ##{params[:page]} is overflowing. Showing page #{e.pagy.last} instead."
499+
def redirect_to_last_page(exception)
500+
redirect_to url_for(page: exception.pagy.last), notice: "Page ##{params[:page]} is overflowing. Showing page #{exception.pagy.last} instead."
501501
end
502502
```

β€Ždocs/migration-guide.mdβ€Ž

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Pagy::VARS[:items] = 10
6060
Pagy::VARS[:size] = [5,4,4,5]
6161
```
6262

63-
Remove all the old settings and uncomment and edit the new settings in the `pagy.rb` initializer _(see [Configuration](how-to.md#global-configuration))_.
63+
Remove all the legacy settings of the old gem(s) and uncomment and edit the new settings in the `pagy.rb` initializer _(see [Configuration](how-to.md#global-configuration))_.
6464

6565
#### Cleanup the Models
6666

@@ -70,7 +70,7 @@ The other gems are careless about adding methods, scopes, and even configuration
7070

7171
For example, you may want to search for keywords like `per_page`, `per` and such, which are actually configuration settings. They should either go into the `pagy.rb` initializer if they are global to the app, or into the specific `pagy` call in the controller if they are specific to an action.
7272

73-
If the app used the `page` scope in some of its methods or scopes, that should be removed (including removing the argument used to pass the page number to the scope), leaving the rest of the scope in place. Search where the app uses the already paginated scope in the controllers, and use the scope in a regular `pagy` statement. For example:
73+
If the app used the `page` scope in some of its methods or scopes in some model, that should be removed (including removing the argument used to pass the page number to the method/scope), leaving the rest of the scope in place. Search where the app uses the already paginated scope in the controllers, and use the scope in a regular `pagy` statement. For example:
7474

7575
```ruby
7676
#@records = Product.paginated_scope(params[:page])
@@ -125,10 +125,8 @@ Please take a look at the topics in the [how-to](how-to.md) documentation: that
125125

126126
### CSSs
127127

128-
The css styling that you may have applied to the pagination elements may need some minor change. However if the app uses the pagination from bootstrap (or some other framework), the same CSSs should work seamlessly with the pagy nave helpers or with any of the bootstrap templates.
128+
The css styling that you may have applied to the pagination elements may need some minor change. However if the app uses the pagination from bootstrap (or some other framework), the same CSSs should work seamlessly with the pagy nav helpers or with any of the bootstrap templates.
129129

130130
### I18n
131131

132-
If the app uses `I18n` you should follow the [I18n doc](api/frontend.md#i18n)
133-
134-
See also [I18n](api/frontend.md#i18n).
132+
If the app uses `I18n` you should follow the [I18n doc](api/frontend.md#i18n).

0 commit comments

Comments
 (0)