Provides helpers for paginating collections in Grape endpoints. It works with with will_paginate and Kaminari.
Add this line to your application's Gemfile:
gem 'grape-pagination'
Tag your endpoint as supporting pagination params, then use the paginate
helper.
class API < Grape::API
desc 'Gets everything!'
paginate
get do
paginate collection
end
end
Which would result in a paginated collection and the following headers set:
Link: <http://localhost:3001/api/contacts?page=1&per_page=30>; rel="first", <http://localhost:3001/api/contacts?page=1&per_page=30>; rel="prev", <http://localhost:3001/api/contacts?page=3&per_page=30>; rel="next", <http://localhost:3001/api/contacts?page=105&per_page=30>; rel="last"
You can disable links by overriding values using Grape::Pagination.configure
method.
Grape::Pagination.configure do |config|
config.included_links = [:next, :prev] # show only next and previous links
end
By default will_paginate and kaminari are supported. However it is possible to use custom pagination using paginate\_with
.
Grape::Pagination.configure do |config|
config.paginate_with do |collection, params|
collection.page(params[:page]).per(params[:per_page]).padding(3)
end
end
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request