# Limit Extra

In 

Allow the client to request a custom page limit with an optional selector UI. It is useful with APIs or user-customizable UIs.

# Synopsis

# Default usage

pagy.rb (initializer)
require 'pagy/extras/limit' # works without further configuration
Controller
# enabled by default
@pagy, @records = pagy(collection)
# you can disable it explicitly for specific requests
@pagy, @records = pagy(collection, limit_extra: false)

# Custom usage

pagy.rb (initializer)
# optionally disable it by default
Pagy::DEFAULT[:limit_extra] = false # default true
# customize the defaults if you need to
Pagy::DEFAULT[:limit_param] = :custom_param # default :limit
Pagy::DEFAULT[:limit_max]   = 200 # default 100
Controller
# disabled by default by the above Pagy::DEFAULT[:limit_extras] = false
@pagy, @records = pagy(collection)
# explicitly enable it for specific requests
@pagy, @records = pagy(collection, limit_extra: true)

See Javascript (only if you use the pagy_limit_selector_js UI)

# Variables

Variable Description Default
:limit_extra Enable or disable the feature true
:limit_param The name of the "limit" param used in the url :limit
:limit_max The max limit allowed. Set it to nil for no limit 100

You can use the :limit_extra variable to opt-out of the feature even when the extra is required.

This extra uses the :limit_param variable to determine the param it should get the :limit from.

The :limit_max is used to cap the :limit to that max. It is set to 100 by default. Set it to nil for no limit.

You may want to customize the variables. Depending on the scope of the customization, you have a couple of options:

As a global default:

pagy.rb (initializer)
Pagy::DEFAULT[:limit_param] = :custom_param
Pagy::DEFAULT[:limit_max]   = 50

For a single instance (overriding the global default):

Controller
pagy(collection, limit_param: :custom_param, limit_max: 50)
Pagy.new(count: 100, limit_param: :custom_param, limit_max: 50)

# Methods

The limit extra adds the pagy_limit_selector_js helper to the Pagy::Frontend module.

This helper provides a limit selector UI, which allows the user to select any arbitrary limit per page (below the :limit_max number) in a numeric input field. It looks like:

Show items per page

It returns an empty string if the :limit_extra is false.

The method accepts also a few optional keyword arguments variables:

  • :id which adds the id HTML attribute to the nav tag
  • :item_name an already pluralized string that will be used in place of the default item/items
some_view.html.erb
<%== pagy_limit_selector_js(@pagy, item_name: 'Product'.pluralize(@pagy.count)) %>