Skip to content

Commit 553ebc0

Browse files
committed
Add per_page option to #paginate in Rails
Signed-off-by: David Celis <[email protected]>
1 parent b5a7fa7 commit 553ebc0

File tree

4 files changed

+34
-35
lines changed

4 files changed

+34
-35
lines changed

README.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,17 @@ class MoviesController < ApplicationController
4040
actors = Movie.find(params[:id]).actors
4141

4242
# Override how many Actors get returned. The default is 10.
43-
params[:per_page] = 25
44-
45-
paginate json: actors
43+
paginate json: actors, per_page: 25
4644
end
4745
end
4846
```
4947

5048
`paginate` will:
5149

52-
* Pull your collection from `json:` or `xml:`
53-
* Use `params[:page]` and `params[:per_page]` to paginate your collection for you
54-
* Use the paginated collection to render `Link` headers
55-
* Call `ActionController::Base#render` with whatever you passed to `paginate`.
50+
1. Pull your collection from `json:` or `xml:`
51+
2. Use `params[:page]` and `params[:per_page]` to paginate your collection for you
52+
3. Use the paginated collection to render `Link` headers
53+
4. Call `ActionController::Base#render` with whatever you passed to `paginate`.
5654

5755
The collection sent to `paginate` _must_ respond to your paginator's methods. For Kaminari, `Kaminari.paginate_array` will be called for you behind-the-scenes. For WillPaginate, you're out of luck unless you somewhere `require 'will_paginate/array'`. Because this pollutes `Array`, it won't be done for you automatically.
5856

lib/api-pagination.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ module ApiPagination
55
class << self
66
attr_reader :paginator
77

8-
def paginate(collection, options = {}, &block)
8+
def paginate(collection, options = {})
99
options[:page] ||= 1
1010
options[:per_page] ||= 10
1111

1212
case ApiPagination.paginator
1313
when :kaminari
1414
collection = Kaminari.paginate_array(collection) if collection.is_a?(Array)
15-
collection.page(options[:page]).per(options[:per_page]).tap(&block)
15+
collection.page(options[:page]).per(options[:per_page])
1616
when :will_paginate
17-
collection.paginate(:page => options[:page], :per_page => options[:per_page]).tap(&block)
17+
collection.paginate(:page => options[:page], :per_page => options[:per_page])
1818
end
1919
end
2020

lib/grape/pagination.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@ module Pagination
33
def self.included(base)
44
Grape::Endpoint.class_eval do
55
def paginate(collection)
6-
block = Proc.new do |collection|
7-
links = (header['Link'] || "").split(',').map(&:strip)
8-
url = request.url.sub(/\?.*$/, '')
9-
pages = ApiPagination.pages_from(collection)
6+
collection = ApiPagination.paginate(collection, params)
107

11-
pages.each do |k, v|
12-
old_params = Rack::Utils.parse_query(request.query_string)
13-
new_params = old_params.merge('page' => v)
14-
links << %(<#{url}?#{new_params.to_param}>; rel="#{k}")
15-
end
8+
links = (header['Link'] || "").split(',').map(&:strip)
9+
url = request.url.sub(/\?.*$/, '')
10+
pages = ApiPagination.pages_from(collection)
1611

17-
header 'Link', links.join(', ') unless links.empty?
18-
header 'Total', ApiPagination.total_from(collection)
12+
pages.each do |k, v|
13+
old_params = Rack::Utils.parse_query(request.query_string)
14+
new_params = old_params.merge('page' => v)
15+
links << %(<#{url}?#{new_params.to_param}>; rel="#{k}")
1916
end
2017

21-
ApiPagination.paginate(collection, params, &block)
18+
header 'Link', links.join(', ') unless links.empty?
19+
header 'Total', ApiPagination.total_from(collection)
20+
21+
return collection
2222
end
2323
end
2424

lib/rails/pagination.rb

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Pagination
55
def paginate(options)
66
collection = options[:json] || options[:xml]
77

8-
collection = _paginate_collection(collection)
8+
collection = _paginate_collection(collection, options)
99
options[:json] = collection if options[:json]
1010
options[:xml] = collection if options[:xml]
1111

@@ -18,22 +18,23 @@ def paginate_with(collection)
1818

1919
private
2020

21-
def _paginate_collection(collection)
22-
block = Proc.new do |collection|
23-
links = (headers['Link'] || "").split(',').map(&:strip)
24-
url = request.original_url.sub(/\?.*$/, '')
25-
pages = ApiPagination.pages_from(collection)
21+
def _paginate_collection(collection, options)
22+
params[:per_page] = options.delete(:per_page) if options[:per_page]
23+
collection = ApiPagination.paginate(collection, params)
2624

27-
pages.each do |k, v|
28-
new_params = request.query_parameters.merge(:page => v)
29-
links << %(<#{url}?#{new_params.to_param}>; rel="#{k}")
30-
end
25+
links = (headers['Link'] || "").split(',').map(&:strip)
26+
url = request.original_url.sub(/\?.*$/, '')
27+
pages = ApiPagination.pages_from(collection)
3128

32-
headers['Link'] = links.join(', ') unless links.empty?
33-
headers['Total'] = ApiPagination.total_from(collection)
29+
pages.each do |k, v|
30+
new_params = request.query_parameters.merge(:page => v)
31+
links << %(<#{url}?#{new_params.to_param}>; rel="#{k}")
3432
end
3533

36-
ApiPagination.paginate(collection, params, &block)
34+
headers['Link'] = links.join(', ') unless links.empty?
35+
headers['Total'] = ApiPagination.total_from(collection)
36+
37+
return collection
3738
end
3839
end
3940
end

0 commit comments

Comments
 (0)