You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Paginate in your headers, not in your response body.
6
4
This follows the proposed [RFC-5988](http://tools.ietf.org/html/rfc5988) standard for Web linking.
7
5
@@ -18,6 +16,7 @@ gem 'rails-api'
18
16
gem 'grape', '>= 0.10.0'
19
17
20
18
# Then choose your preferred paginator from the following:
19
+
gem 'pagy'
21
20
gem 'kaminari'
22
21
gem 'will_paginate'
23
22
@@ -27,11 +26,11 @@ gem 'api-pagination'
27
26
28
27
## Configuration (optional)
29
28
30
-
By default, api-pagination will detect whether you're using Kaminari or WillPaginate, and name headers appropriately. If you want to change any of the configurable settings, you may do so:
29
+
By default, api-pagination will detect whether you're using Pagy, Kaminari, or WillPaginate, and it will name headers appropriately. If you want to change any of the configurable settings, you may do so:
31
30
32
31
```ruby
33
32
ApiPagination.configure do |config|
34
-
# If you have both gems included, you can choose a paginator.
33
+
# If you have more than one gem included, you can choose a paginator.
35
34
config.paginator =:kaminari# or :will_paginate
36
35
37
36
# By default, this is set to 'Total'
@@ -59,6 +58,16 @@ ApiPagination.configure do |config|
59
58
end
60
59
```
61
60
61
+
### Pagy-specific configuration
62
+
63
+
Pagy does not have a built-in way to specify a maximum number of items per page, but `api-pagination` will check if you've set a `:max_per_page` variable. To configure this, you can use the following code somewhere in an initializer:
64
+
65
+
```ruby
66
+
Pagy::VARS[:max_per_page] =100
67
+
```
68
+
69
+
If left unconfigured, clients can request as many items per page as they wish, so it's highly recommended that you configure this.
70
+
62
71
## Rails
63
72
64
73
In your controller, provide a pageable collection to the `paginate` method. In its most convenient form, `paginate` simply mimics `render`:
@@ -103,7 +112,7 @@ class MoviesController < ApplicationController
103
112
end
104
113
```
105
114
106
-
Note that the collection sent to `paginate`_must_ respond to your paginator's methods. This is typically fine unless you're dealing with a stock Array. For Kaminari, `Kaminari.paginate_array` will be called for you behind-the-scenes. For WillPaginate, you're out of luck unless you call `require 'will_paginate/array'` somewhere. Because this pollutes `Array`, it won't be done for you automatically.
115
+
Note that the collection sent to `paginate`_must_ respond to your paginator's methods. This is typically fine unless you're dealing with a stock Array. For Kaminari, `Kaminari.paginate_array` will be called for you behind-the-scenes. For WillPaginate, you're out of luck unless you call `require 'will_paginate/array'` somewhere. Because this pollutes `Array`, it won't be done for you automatically. If you use Pagy, it doesn't matter, because Pagy doesn't care what you're paginating. It will just work, as long as the collection responds to `count`.
107
116
108
117
**NOTE:** In versions 4.4.0 and below, the `Rails::Pagination` module would end up included in `ActionController::Base` even if `ActionController::API` was defined. As of version 4.5.0, this is no longer the case. If for any reason your API controllers cannot easily changed be changed to inherit from `ActionController::API` instead, you can manually include the module:
0 commit comments