theodik / grape-pagination

Pagination helpers for Grape.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Grape Pagination

Provides helpers for paginating collections in Grape endpoints. It works with with will_paginate and Kaminari.

Installation

Add this line to your application's Gemfile:

gem 'grape-pagination'

Usage

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"

Configuration

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   

Custom pagination

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   

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

About

Pagination helpers for Grape.

License:MIT License


Languages

Language:Ruby 100.0%