Skip to content

Commit f5754d1

Browse files
committed
Make Grape interface consistent with Rails'
Signed-off-by: David Celis <[email protected]>
1 parent 553ebc0 commit f5754d1

File tree

6 files changed

+31
-15
lines changed

6 files changed

+31
-15
lines changed

LICENSE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright © 2013 David Celis
1+
Copyright © 2014 David Celis
22

33
MIT License
44

README.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ class MoviesController < ApplicationController
3939
def cast
4040
actors = Movie.find(params[:id]).actors
4141

42-
# Override how many Actors get returned. The default is 10.
43-
paginate json: actors, per_page: 25
42+
# Override how many Actors get returned. If unspecified,
43+
# params[:per_page] (which defaults to 25) will be used.
44+
paginate json: actors, per_page: 10
4445
end
4546
end
4647
```
@@ -63,13 +64,21 @@ class MoviesAPI < Grape::API
6364
format :json
6465

6566
desc 'Return a paginated set of movies'
66-
paginate per_page: 25
67-
get :numbers do
68-
movies = Movie.all # Movie.scoped if using ActiveRecord 3.x
69-
67+
paginate
68+
get do
7069
# This method must take an ActiveRecord::Relation
7170
# or some equivalent pageable set.
72-
paginate movies
71+
paginate Movie.all
72+
end
73+
74+
route_param :id do
75+
desc "Return one movie's cast, paginated"
76+
# Override how many Actors get returned. If unspecified,
77+
# params[:per_page] (which defaults to 25) will be used.
78+
paginate per_page: 10
79+
get :cast do
80+
paginate Movie.find(params[:id]).actors
81+
end
7382
end
7483
end
7584
```

lib/api-pagination.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class << self
77

88
def paginate(collection, options = {})
99
options[:page] ||= 1
10-
options[:per_page] ||= 10
10+
options[:per_page] ||= 25
1111

1212
case ApiPagination.paginator
1313
when :kaminari

lib/grape/pagination.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ module Pagination
33
def self.included(base)
44
Grape::Endpoint.class_eval do
55
def paginate(collection)
6-
collection = ApiPagination.paginate(collection, params)
6+
options = {
7+
:page => params[:page],
8+
:per_page => (settings[:per_page] || params[:per_page])
9+
}
10+
collection = ApiPagination.paginate(collection, options)
711

812
links = (header['Link'] || "").split(',').map(&:strip)
913
url = request.url.sub(/\?.*$/, '')
@@ -24,11 +28,11 @@ def paginate(collection)
2428

2529
base.class_eval do
2630
def self.paginate(options = {})
27-
options.reverse_merge!(:per_page => 10)
31+
set :per_page, options[:per_page]
2832
params do
2933
optional :page, :type => Integer, :default => 1,
3034
:desc => 'Page of results to fetch.'
31-
optional :per_page, :type => Integer, :default => options[:per_page],
35+
optional :per_page, :type => Integer,
3236
:desc => 'Number of results to return per page.'
3337
end
3438
end

lib/rails/pagination.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ def paginate_with(collection)
1919
private
2020

2121
def _paginate_collection(collection, options)
22-
params[:per_page] = options.delete(:per_page) if options[:per_page]
23-
collection = ApiPagination.paginate(collection, params)
22+
options = {
23+
:page => params[:page],
24+
:per_page => (options.delete(:per_page) || params[:per_page])
25+
}
26+
collection = ApiPagination.paginate(collection, options)
2427

2528
links = (headers['Link'] || "").split(',').map(&:strip)
2629
url = request.original_url.sub(/\?.*$/, '')

spec/support/numbers_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ def index
5555
headers['Link'] = %(<#{numbers_url}?#{query.to_param}>; rel="without")
5656
end
5757

58-
paginate :json => (1..total).to_a
58+
paginate :json => (1..total).to_a, :per_page => 10
5959
end
6060
end

0 commit comments

Comments
 (0)