#

In 

# pagy(:✳) Paginators


# The pagy Method

The pagy method provides a common interface to all paginators. Include it where you are going to paginate a collection (usually in ApplicationContoller):

include Pagy::Method

You can use it to paginate ANY collection, with ANY technique. For example:

@pagy, @records = pagy(:offset, collection, **options)
@pagy, @records = pagy(:keyset, set, **options)
@pagy, @records = pagy(...)
  • :offset, :keyset, etc. are symbols identifying the paginator. They implement the specific pagination.
  • @pagy is the pagination istance. It provides all the instance helper methods to use in your code.
  • @records are the records belonging to the requested page.

# Paginators

The paginators are internal constructor methods that provide different types of pagination for different types of collections, using a common API. They are passed to the pagy method by their symbolic name. (e.g., :offset, :keyset, countless, etc.)

:offset
:countless
:keynav_js
:keyset
:calendar
:elasticsearch_rails
:meilisearch
:searchkick

  • limit: 10
    • Specifies the number of items per page (default: 20)
  • max_pages: 500
    • Restricts pagination to only :max_pages. (Ignored by Pagy::Calendar::* unit instances)
  • page: 3
    • Set it only to force the current :page. (It is set automatically from the request query hash).
  • client_max_limit: 1_000
    • Set the maximum :limit that the client is allowed to request. Higher requested :limits are silently capped. (If falsey, the client cannot request the :limit)
  • 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',
                         query:    { 'param1' => 1234 }, # The string-keyed hash query from the request
                         cookie:   'xyz' }               # The 'pagy' cookie, only for keynav  
  • absolute: true
    • Makes the URL absolute.
  • fragment: '#...'
    • 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)
  • limit_key: 'custom_limit'
    • Set it to change the key string used for the :limit in URLs (default 'limit').
  • page_key: 'custom_page'
    • Set it to change the key string used for the :page in URLs (default 'page').
  • path: '/custom_path'
  • querify: tweak
    • Set it to a Lambda to directly edit the passed string-keyed query hash itself. Its result is ignored.
      tweak = ->(q) { q.except!('not_useful').merge!('custom' => 'useful') }
  • page
    • The current page
  • limit
    • The items per page
  • options
    • The options of the object
  • next
    • The next page
  • Pagy::OptionError
    • A subclass of ArgumentError that offers information to rescue invalid options passed to the constructor.
    • For example: rescue Pagy::OptionError => e
      • e.pagy the pagy object
      • e.option the offending option symbol (e.g. :page)
      • e.value the value of the offending option (e.g. -3)
# Records may repeat in different pages be missing