#
#
Upgrade to 43
Pagy version 43 is a complete redesign of the legacy code. Its improvements make pagination a lot simpler and powerful, but require a quite different way to use it.
Follow this guide to upgrade your app in just a few minutes.
Cherry-pick only what applies to your app: you can safely skip all the rest.
Just the steps without distractions.
This guide focuses on getting the job done quickly. If you want to learn more about the changes:
- Consult the docs and How To Guide
- Ask Pagy AI specific questions (Bottom-right button in this page)
- Ask in the Q&A discussion.
#
1. Replace the pagy.rb
config file
- Rename your
pagy.rb
initializer aspagy-old.rb
, and add the new, concise pagy.rb initializer in its place. - Search the
pagy-old.rb
for code-occurrences ofPagy::DEFAULT[...]
and move them to the newpagy.js
(remove them from thepagy-old.rb
) - Replace all the
Pagy::DEFAULT[...]
entries just added to the newpagy.rb
withPagy.options[...]
.
In the next steps we will use the pagy-old.rb
as the blueprint to guide most of the changes, and we will edit the new pagy.rb
as needed.
#
2. Replace your used extras
The new version doesn't use the extras anymore. They got integrated in the core code, and a few have been discontinued.
- Search any active
require 'pagy/extras/*
in thepagy-old.rb
file - When you find one, follow the specific section below to upgrade your code.
- As you proceed, remove each entry from the
pagy-old.rb
.
Extras...
array
arel
pagy
- All the old helpers are now
@pagy
instance methods with more explicit names.
boostrap
- All the old helpers are now
@pagy
instance methods with more explicit names.
- FYI: The redundant
pagy-bootstrap
class has been removed from theinput_nav_js
body.
bulma
- All the old helpers are now
@pagy
instance methods with more explicit names.
- FYI: The
is-centered
CSS class has been removed. - FYI: The previous/next links have been moved at the beginning and end of the pagination.
countless
calendar
- If your
pagy-old.rb
file contains any localization configuration, then uncomment and customize the following line in thepagy.rb
initializer:Pagy::Calendar.localize_with_rails_i18n_gem(*your_locales)
.- Note: In non-Rails applications, calendar localization requires adding
rails-i18n
to your Gemfile.
- Note: In non-Rails applications, calendar localization requires adding
- Remove any existing
Pagy::Calendar::*::DEFAULT
. Pass the options to each unit when you paginate.
elasticsearch_rails
- Active and passive modes are now handled by the same
pagy
method:
- Customization of the
pagy_search
method name has been discontinued:- Remove any existing
:elasticsearch_rails_pagy_search
variable from your code. - Replace your custom method name with the standard
pagy_search
method.
- Remove any existing
meilisearch
- Active and passive modes are now handled by the same
pagy
method:
- Customization of the
pagy_search
method name has been discontinued:- Remove any existing
:meilisearch_pagy_search
variable from your code. - Replace your custom method name with the standard
pagy_search
method.
- Remove any existing
searchkick
- Active and passive modes are now handled by the same
pagy
method:
- Customization of the
pagy_search
method name has been discontinued:- Remove any existing
:searchkick_pagy_search
variable from your code. - Replace your custom method name with the standard
pagy_search
method.
- Remove any existing
headers
jsonapi
- Notice that the
nil
links are now removed as theJSON:API
specifications require. - IMPORTANT: Enable the feature by explicitly setting the
jsonapi: true
option (in the initializer orpagy
method).
keyset
- Replace any existing
:jsonify_keyset_attributes
with:pre_serialize
.- The lambda receives the same
keyset_attributes
argument, but it must modify the specific values directly. The lambda's return value is ignored. For example:->(attrs) { attrs[:created_at] = attrs[:created_at].strftime('%F %T.%6N') }
.
- The lambda receives the same
limit
- Enable the feature by setting
client_max_limit: your_client_max_limit
option (in the initializer orpagy
method).
metadata
overflow
- The
Pagy::OverflowError
has been replaced by thePagy::RangeError
; however, it is no longer raised by default. - Pagy rescues the
Pagy::RangeError
and serves an empty page by default.- Now, Pagy behaves the same as it did before when requiring the overflow extra and using its default settings.
- The legacy
pagy.overflow?
is now thepagy.in_range?
method, which checks/returns the opposite state/boolean. - The
overflow: :last_page
behavior has been discontinued because it provides nearly no benefit:- Why there is little benefit in serving the last page?
- The navigation bar for an out-of-range request is rendered identically to that of the last page.
- The only difference is that there are no records/results to display.
- The "previous page" button points to the last page, so if users truly want to see the last page results (which they have likely already seen), they can simply click the link.
- Why there is little benefit in serving the last page?
- Summary for keeping the same behavior:
- The
:overflow
variable is not used anymore. - If you did not use the extra (i.e., Pagy raised errors), set
raise_range_error: true
. - If you used
overflow: :empty_page
or just required the overflow extra, simply remove it (this is now the default behavior). - If you used
overflow: :last_page
and still want this behavior despite the reasons above:- Set
raise_range_error: true
. - Use
rescue Pagy::RangeError => e
in your method. - Redirect to
@pagy.page_url(:last)
.
- Set
- The
standalone
Replace the
:url
variable with the:request
Common option hash. For example:request: { base_url: 'http://www.example.com', path: '/path', query: { 'param1' => 1234 }, # The string-keyed hash query from the request cookie: 'xyz' } # The 'pagy' cookie, only for keynav
i18n
- If absolutely necessary, uncomment or add this line to your initializer:
Pagy.translate_with_the_slower_i18n_gem!
.
gearbox
(discontinued)
- Due to extensive overwriting for minimal benefit, you can safely remove this feature from your app without noticeable impact.
Remove all
/gearbox/
related code.
size
(discontinued)
- Pagination bars similar to WillPaginate and Kaminari are not good for a lot of reasons. If still required, adapt the legacy file from a previous commit.
trim
(discontinued)
- It was mostly useless and half-baked, causing many complications in both the Ruby and JavaScript code for no significant benefit.
- Use an appropriate approach to address your requirement, such as utilizing URL rewriting at the HTTP server level.
#
3. Final steps
:params
variable...
- Use the
:querify
option, which is alambda
that can modify the string-keyed query hash at will. It is a bit more verbose, but it's more powerful and low-level. It solves an incompatibility with the old high-level:params
hash/lambda and improves performance. It is part of the Common URL Options group that gives you full and efficient control over the URL composition. - Example:
# Old symbol-keyed, high-level hash variable params: { a: 1, b: 2 } # New string-keyed, low-level, direct modification of the query hash querify: ->(q) { q.merge!('a' => 1, 'b' => 2) } # It also allows to do things like: querify = ->(q) { q.except!('not_useful').merge!('custom' => 'useful') }
*prev*
abbreviated naming
- Use
*previous*
in all the options, accessors, methods, etc.
#
4. Finalize the upgrade
- If your
pagy-old.rb
contains any JavaScript setup, it should still work, so you can move it to thepagy.rb
file, however, for apps with builders, consider using the new Pagy.sync_javascript.
- If your
pagy-old.rb
contains thePagy::I18n
setup, and the setup includes some custom dictionary file, then uncomment and set up the relevantPagy::I18n
lookup section in thepagy.rb
file. (See the I18n docs for details) - Update your custom dictionary files (if any) to the new dictionary structure, or they won't work correctly.
- Besides that, you don't need any line of the old setup, because all the locales are autoloaded when your app uses them.
- Remove all the I18n code from the
pagy-old.rb
.
- Overriding methods in controllers/helpers is not possible or discouraged.
- The cleanest approach for local overriding is via Ruby refinements or the initializer for global override.
- Check the How To Override Pagy Method.
- If your
pagy-old.rb
contains overridden methods, copy the methods over to thepagy.rb
initializer, however, consider that:- Internal Pagy protected methods have been extensively refactored, likely renamed, and occasionally removed.
- You should reconcile internal overrides by reviewing the updated Pagy codebase.
You may want also to check these internal renaming:
pagy-old.rb
file
- At this point, there should be no more code in the
pagy-old.rb
.
Please report any issue with this guide opening a new docs issue.