:page_facing_up: Link header pagination for Rails and Grape APIs.
:page_facing_up: Link header pagination for Rails and Grape APIs.
Paginate in your headers, not in your response body. This follows the proposed RFC-8288 standard for Web linking.
In your Gemfile:
# Requires Rails (Rails-API is also supported), or Grape
# v0.10.0 or later. If you're on an earlier version of
# Grape, use api-pagination v3.0.2.
gem 'rails', '>= 3.0.0'
gem 'rails-api'
gem 'grape', '>= 0.10.0'
# Then choose your preferred paginator from the following:
gem 'pagy'
gem 'kaminari'
gem 'will_paginate'
# Finally...
gem 'api-pagination'By default, api-pagination will detect whether you're using Pagy, Kaminari, or WillPaginate, and it will name headers appropriately. If you want to change any of the configurable settings, you may do so:
…Pagy does not have a built-in way to specify a maximum number of items per page, but api-pagination will check if you've set a :max_per_page variable. To configure this, you can use the following code somewhere in an initializer:
Pagy::OPTIONS[:max_per_page] = 100If left unconfigured, clients can request as many items per page as they wish, so it's highly recommended that you configure this.
In your controller, provide a pageable collection to the paginate method. In its most convenient form, paginate simply mimics render:
class MoviesController ; rel="first",
; rel="last",
; rel="next",
; rel="prev"
Total: 4321
Per-Page: 10
# ...api-pagination requires either Kaminari or WillPaginate in order to function, but some users may find themselves in situations where their application includes both. For example, you may have included ActiveAdmin (which uses Kaminari for pagination) and WillPaginate to do your own pagination. While it's suggested that you remove one paginator gem or the other, if you're unable to do so, you must configure api-pagination explicitly:
ApiPagination.configure do |config|
config.paginator = :will_paginate
endIf you don't do this, an annoying warning will print once your app starts seeing traffic. You should also configure Kaminari to use a different name for its per_page method (see https://github.com/activeadmin/activeadmin/wiki/How-to-work-with-will_paginate):
Kaminari.configure do |config|
config.page_method_name = :per_page_kaminari
endNo open issues yet, or sync has not completed.