File tree Expand file tree Collapse file tree 6 files changed +31
-15
lines changed Expand file tree Collapse file tree 6 files changed +31
-15
lines changed Original file line number Diff line number Diff line change 1
- Copyright © 2013 David Celis
1
+ Copyright © 2014 David Celis
2
2
3
3
MIT License
4
4
Original file line number Diff line number Diff line change @@ -39,8 +39,9 @@ class MoviesController < ApplicationController
39
39
def cast
40
40
actors = Movie .find(params[:id ]).actors
41
41
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
44
45
end
45
46
end
46
47
```
@@ -63,13 +64,21 @@ class MoviesAPI < Grape::API
63
64
format :json
64
65
65
66
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
70
69
# This method must take an ActiveRecord::Relation
71
70
# 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
73
82
end
74
83
end
75
84
```
Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ class << self
7
7
8
8
def paginate ( collection , options = { } )
9
9
options [ :page ] ||= 1
10
- options [ :per_page ] ||= 10
10
+ options [ :per_page ] ||= 25
11
11
12
12
case ApiPagination . paginator
13
13
when :kaminari
Original file line number Diff line number Diff line change @@ -3,7 +3,11 @@ module Pagination
3
3
def self . included ( base )
4
4
Grape ::Endpoint . class_eval do
5
5
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 )
7
11
8
12
links = ( header [ 'Link' ] || "" ) . split ( ',' ) . map ( &:strip )
9
13
url = request . url . sub ( /\? .*$/ , '' )
@@ -24,11 +28,11 @@ def paginate(collection)
24
28
25
29
base . class_eval do
26
30
def self . paginate ( options = { } )
27
- options . reverse_merge! ( :per_page => 10 )
31
+ set :per_page , options [ :per_page ]
28
32
params do
29
33
optional :page , :type => Integer , :default => 1 ,
30
34
:desc => 'Page of results to fetch.'
31
- optional :per_page , :type => Integer , :default => options [ :per_page ] ,
35
+ optional :per_page , :type => Integer ,
32
36
:desc => 'Number of results to return per page.'
33
37
end
34
38
end
Original file line number Diff line number Diff line change @@ -19,8 +19,11 @@ def paginate_with(collection)
19
19
private
20
20
21
21
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 )
24
27
25
28
links = ( headers [ 'Link' ] || "" ) . split ( ',' ) . map ( &:strip )
26
29
url = request . original_url . sub ( /\? .*$/ , '' )
Original file line number Diff line number Diff line change @@ -55,6 +55,6 @@ def index
55
55
headers [ 'Link' ] = %(<#{ numbers_url } ?#{ query . to_param } >; rel="without")
56
56
end
57
57
58
- paginate :json => ( 1 ..total ) . to_a
58
+ paginate :json => ( 1 ..total ) . to_a , :per_page => 10
59
59
end
60
60
end
You can’t perform that action at this time.
0 commit comments