Skip to content

3.0.0

Compare
Choose a tag to compare
@davidcelis davidcelis released this 17 Apr 19:27
· 162 commits to master since this release

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