|
2 | 2 |
|
3 | 3 | # How to add pagination links
|
4 | 4 |
|
5 |
| -### JSON API adapter |
| 5 | +## JSON API adapter |
6 | 6 |
|
7 |
| -Pagination links will be included in your response automatically as long as |
8 |
| -the resource is paginated and if you are using the ```JsonApi``` adapter. |
| 7 | +When using the `JsonApi` adapter, pagination links will be automatically included if you use [Kaminari](https://github.com/amatsuda/kaminari) |
| 8 | +or [WillPaginate](https://github.com/mislav/will_paginate) within a Rails controller: |
9 | 9 |
|
10 |
| -If you want pagination links in your response, use [Kaminari](https://github.com/amatsuda/kaminari) |
11 |
| -or [WillPaginate](https://github.com/mislav/will_paginate). |
12 |
| - |
13 |
| -Although the others adapters does not have this feature, it is possible to |
14 |
| -implement pagination links to `JSON` adapter. For more information about it, |
15 |
| -please see in our docs |
16 |
| - |
17 |
| -###### Kaminari examples |
| 10 | +* Using Kaminari: |
18 | 11 |
|
19 | 12 | ```ruby
|
20 |
| -#array |
21 |
| -@posts = Kaminari.paginate_array([1, 2, 3]).page(3).per(1) |
22 |
| -render json: @posts |
23 |
| - |
24 |
| -#active_record |
25 |
| -@posts = Post.page(3).per(1) |
26 |
| -render json: @posts |
| 13 | +class PostsController < ApplicationController |
| 14 | + def index |
| 15 | + posts = Post.page(params[:page]).per(params[:per_page]) |
| 16 | + render json: posts |
| 17 | + end |
| 18 | +end |
27 | 19 | ```
|
28 | 20 |
|
29 |
| -###### WillPaginate examples |
30 |
| - |
31 |
| -```ruby |
32 |
| -#array |
33 |
| -@posts = [1,2,3].paginate(page: 3, per_page: 1) |
34 |
| -render json: @posts |
35 |
| - |
36 |
| -#active_record |
37 |
| -@posts = Post.page(3).per_page(1) |
38 |
| -render json: @posts |
39 |
| -``` |
| 21 | +* Using WillPaginate: |
40 | 22 |
|
41 | 23 | ```ruby
|
42 |
| -ActiveModelSerializers.config.adapter = :json_api |
| 24 | +class PostsController < ApplicationController |
| 25 | + def index |
| 26 | + posts = Post.page(params[:page]).per_page(params[:per_page]) |
| 27 | + render json: posts |
| 28 | + end |
| 29 | +end |
43 | 30 | ```
|
44 | 31 |
|
45 |
| -ex: |
| 32 | +The response might look like: |
46 | 33 | ```json
|
47 | 34 | {
|
48 | 35 | "data": [
|
|
67 | 54 | }
|
68 | 55 | ```
|
69 | 56 |
|
70 |
| -ActiveModelSerializers pagination relies on a paginated collection with the methods `current_page`, `total_pages`, and `size`, such as are supported by both [Kaminari](https://github.com/amatsuda/kaminari) or [WillPaginate](https://github.com/mislav/will_paginate). |
71 |
| - |
| 57 | +ActiveModelSerializers pagination relies on paginated collections which define the methods `#current_page`, `#total_pages`, and `#size`. |
| 58 | +Such methods are supported by both [Kaminari](https://github.com/amatsuda/kaminari) or [WillPaginate](https://github.com/mislav/will_paginate), |
| 59 | +but you can also roll out your own paginated collection by defining these methods. |
72 | 60 |
|
73 |
| -### JSON adapter |
| 61 | +If you do not want pagination links to be automatically rendered, you may disable it by setting the `ActiveModelSerializers.config.collection_serializer` config to |
| 62 | +`ActiveModel::Serializer::NonPaginatedCollectionSerializer`. |
74 | 63 |
|
75 |
| -If you are using `JSON` adapter, pagination links will not be included automatically, but it is possible to do so using `meta` key. |
| 64 | +If you want to disable pagination links for a specific controller, you may set the `serializer` option to `ActiveModel::Serializer::NonPaginatedCollectionSerializer`: |
76 | 65 |
|
77 |
| -Add this method to your base API controller. |
78 |
| - |
79 |
| -```ruby |
80 |
| -def pagination_dict(object) |
81 |
| - { |
82 |
| - current_page: object.current_page, |
83 |
| - next_page: object.next_page, |
84 |
| - prev_page: object.prev_page, |
85 |
| - total_pages: object.total_pages, |
86 |
| - total_count: object.total_count |
87 |
| - } |
| 66 | +``` ruby |
| 67 | +class PostsController < ApplicationController |
| 68 | + def index |
| 69 | + posts = Post.page(params[:page]).per_page(params[:per_page]) |
| 70 | + render json: posts, serializer: ActiveModel::Serializer::NonPaginatedCollectionSerializer |
| 71 | + end |
88 | 72 | end
|
89 | 73 | ```
|
90 | 74 |
|
91 |
| -Then, use it on your render method. |
| 75 | +### Json adapter |
| 76 | + |
| 77 | +If you are using the `Json` adapter, pagination links will not be included automatically, but it is possible to handle pagination using the `meta` option: |
92 | 78 |
|
93 | 79 | ```ruby
|
94 |
| -render json: posts, meta: pagination_dict(posts) |
| 80 | +class PostsController < ApplicationController |
| 81 | + def index |
| 82 | + posts = Post.page(params[:page]).per_page(params[:per_page]) |
| 83 | + render json: posts, meta: pagination_dict(posts) |
| 84 | + end |
| 85 | + |
| 86 | + private |
| 87 | + |
| 88 | + def pagination_dict(object) |
| 89 | + { |
| 90 | + current_page: object.current_page, |
| 91 | + next_page: object.next_page, |
| 92 | + prev_page: object.prev_page, |
| 93 | + total_pages: object.total_pages, |
| 94 | + total_count: object.total_count |
| 95 | + } |
| 96 | + end |
| 97 | +end |
95 | 98 | ```
|
96 | 99 |
|
97 |
| -ex. |
| 100 | +The response might look like: |
98 | 101 | ```json
|
99 | 102 | {
|
100 | 103 | "posts": [
|
|
113 | 116 | }
|
114 | 117 | }
|
115 | 118 | ```
|
116 |
| - |
117 |
| -You can also achieve the same result if you have a helper method that adds the pagination info in the meta tag. For instance, in your action specify a custom serializer. |
118 |
| - |
119 |
| -```ruby |
120 |
| -render json: @posts, each_serializer: PostPreviewSerializer, meta: meta_attributes(@post) |
121 |
| -``` |
122 |
| - |
123 |
| -```ruby |
124 |
| -#expects pagination! |
125 |
| -def meta_attributes(resource, extra_meta = {}) |
126 |
| - { |
127 |
| - current_page: resource.current_page, |
128 |
| - next_page: resource.next_page, |
129 |
| - prev_page: resource.prev_page, |
130 |
| - total_pages: resource.total_pages, |
131 |
| - total_count: resource.total_count |
132 |
| - }.merge(extra_meta) |
133 |
| -end |
134 |
| -``` |
135 |
| - |
136 |
| - |
137 |
| -### Attributes adapter |
138 |
| - |
139 |
| -This adapter does not allow us to use `meta` key, due to that it is not possible to add pagination links. |
0 commit comments