Releases: davidcelis/api-pagination
Releases · davidcelis/api-pagination
3.0.2
3.0.1
- Fix an issue when using
will_paginate
withsequel
- Fix an issue with a default
per_page
not being set in Grape
3.0.0
The interface of Rails has changed to avoid using after_filter
, as that approach didn't really work:
class MoviesController < ApplicationController
# GET /movies
def index
movies = Movie.all # Movie.scoped if using ActiveRecord 3.x
paginate json: movies
end
# GET /movies/:id/cast
def cast
actors = Movie.find(params[:id]).actors
# Override how many Actors get returned. If unspecified,
# params[:per_page] (which defaults to 25) will be used.
paginate json: actors, per_page: 10
end
end
The interface for Grape has also changed a bit to be more consistent with the
new Rails interface. Namely, any per_page
option specified to the route-level
paginate
call overrides params[:per_page]:
class MoviesAPI < Grape::API
format :json
desc 'Return a paginated set of movies'
paginate
get do
# This method must take an ActiveRecord::Relation
# or some equivalent pageable set.
paginate Movie.all
end
route_param :id do
desc "Return one movie's cast, paginated"
# Override how many Actors get returned. If unspecified,
# params[:per_page] (which defaults to 25) will be used.
paginate per_page: 10
get :cast do
paginate Movie.find(params[:id]).actors
end
end
end
Miscellaneous fixes:
- The default
per_page
is now 25. - Fix issue with the Rails response body not automatically paging
2.1.0
- Populate a
Total
header based on the total number of entries in a collection
v2.0.0
2.0.0
- Add support for Grape pagination
- It is no longer necessary to explicitly call
paginate
,page
, orper
on your collection. Instead, the calls topaginate
will do it for you based on your:per_page
setting:
# Rails
class MoviesController
after_filter only: [:index] { paginate(:movies) }
def index
params[:per_page] = 25
@movies = Movie.all
render json: @movies
end
end
# Grape
class MoviesAPI < Grape::API
format :json
desc 'Return a paginated set of movies'
paginate per_page: 25
get :numbers do
movies = Movie.all
paginate movies
end
end
- The default setting for
:per_page
is 10 and can be overridden such as above.