#
#
💚 Paginators
#
pagy method
The pagy method starts every pagination.
Include the Pagy::Method where you are going to use it (usually in ApplicationContoller):
include Pagy::Method
You can use it to paginate ANY collection, with ANY technique:
@pagy, @records = pagy(:offset, collection, **options)
@pagy, @records = pagy(:keyset, set, **options)
@pagy, @records = pagy(...)
:offset,:keyset, etc. are symbols identifying thepaginator to use, i.e. the internal method handling that type of pagination.@pagyis the pagination istance. It provides methods for every UI components and helpers to use in your code.@recordsare the records belonging to the requested page
#
Paginators
The paginators are internal methods that provide different type of pagination for different types of collections, with a common API. They are passed to the pagy method by their symbolic name. (e.g, :offset, :keyset, countless, etc.)
Avoid instantiating Pagy classes directly
Instantiate implementing classes only if the documentation explicitly suggests it.
Paginators and classes are autoloaded only if used!
Unused code consumes no memory.
:offset
:countless
:keynav_js
:keyset
:calendar
:elasticsearch_rails
:meilisearch
:searchkick
Common Options for Paginators
Individual paginators may offer additional options, which are documented with the paginator itself.
limit: 10- Specifies the number of items per page (default:
20)
- Specifies the number of items per page (default:
max_pages: 500- Restricts pagination to only
:max_pages. (Ignored byPagy::Calendar::*unit instances)
- Restricts pagination to only
page: 3- Set it only to force the current
:page. (It is set automatically from the request query hash).
- Set it only to force the current
requestable_limit: 1_000- Allow the client to set the
:limitin therequestquery, up to1_000in the example
- Allow the client to set the
request: custom_request- Set this hash only in non-rack environments or when instructed by the docs. (It is set automatically from the request). For example:
custom_request = { base_url: 'http://www.example.com', path: '/path', queried: { 'param1' => 1234 }, # The string-keyed hash queried from the request cookie: 'xyz' } # The 'pagy' cookie, only for keynav
- Set this hash only in non-rack environments or when instructed by the docs. (It is set automatically from the request). For example:
Common URL options for all paginators and @pagy methods
These options control give you full control over the URL composition.
absolute: true- Makes the URL absolute.
fragment: '#...'- URL fragment string. (It must include the leding
"#"!)
- URL fragment string. (It must include the leding
jsonapi: true- Enables JSON:API-compliant URLs, with nested query string (e.g.,
?page[number]=2&page[size]=100)
- Enables JSON:API-compliant URLs, with nested query string (e.g.,
limit_key: 'custom_limit'- Set it to change the key string used for the
:limitin URLs (default'limit').
- Set it to change the key string used for the
page_key: 'custom_page'- Set it to change the key string used for the
:pagein URLs (default'page').
- Set it to change the key string used for the
path: '/custom_path'- Overrides the request path in pagination URLs. Use the path only (not the absolute URL). (see Override the request path)
querify: tweak- Set it to a
Lambdato directly edit the passed string-keyed query hash itself. Its result is ignored.tweak = ->(q) { q.except!('not_useful').merge!('custom' => 'useful') }
- Set it to a
Common Readers for Paginators
Individual paginators may offer additional readers, which are documented with the paginator itself.
page- The current page
limit- The items per page
options- The options of the object
next- The next page
Common Exceptions for Paginators
Individual paginators may raise specific exceptions, which are documented with the paginator itself.
Pagy::OptionError- A subclass of
ArgumentErrorthat offers information to rescue invalid options passed to the constructor. - For example:
rescue Pagy::OptionError => ee.pagythe pagy objecte.optionthe offending option symbol (e.g.:page)e.valuethe value of the offending option (e.g.-3)
- A subclass of
#
Records may repeat in different pages (or sometimes be missing)
Don't Paginate Unordered PostgreSQL Collections!
@pagy, @records = pagy(:offset, unordered)
# behind the scenes, pagy selects the page of records with:
unordered.offset(pagy.offset).limit(pagy.limit)
Citation: PostgreSQL Documentation
When using LIMIT, always include an ORDER BY clause to constrain the result rows into a unique order. Otherwise, the subset of rows retrieved may be unpredictable.
Ensure the PostgreSQL collection is ordered!
# Results will be consistent and predictable with #order
ordered = unordered.order(:id)
@pagy, @records = pagy_offset(ordered)