|
| 1 | +# How to add pagination links |
| 2 | + |
| 3 | +### JSON-API adapter |
| 4 | + |
| 5 | +Pagination links will be included in your response automatically as long as the resource is paginated and if you are using a ```JSON-API``` adapter. |
| 6 | + |
| 7 | +If you want pagination links in your response, use [Kaminari](https://github.com/amatsuda/kaminari) or [WillPaginate](https://github.com/mislav/will_paginate). |
| 8 | + |
| 9 | +###### Kaminari examples |
| 10 | +```ruby |
| 11 | +#array |
| 12 | +@posts = Kaminari.paginate_array([1, 2, 3]).page(3).per(1) |
| 13 | +render json: @posts |
| 14 | + |
| 15 | +#active_record |
| 16 | +@posts = Post.page(3).per(1) |
| 17 | +render json: @posts |
| 18 | +``` |
| 19 | + |
| 20 | +###### WillPaginate examples |
| 21 | + |
| 22 | +```ruby |
| 23 | +#array |
| 24 | +@posts = [1,2,3].paginate(page: 3, per_page: 1) |
| 25 | +render json: @posts |
| 26 | + |
| 27 | +#active_record |
| 28 | +@posts = Post.page(3).per_page(1) |
| 29 | +render json: @posts |
| 30 | +``` |
| 31 | + |
| 32 | +```ruby |
| 33 | +ActiveModel::Serializer.config.adapter = :json_api |
| 34 | +``` |
| 35 | + |
| 36 | +ex: |
| 37 | +```json |
| 38 | +{ |
| 39 | + "data": [ |
| 40 | + { |
| 41 | + "type": "articles", |
| 42 | + "id": "3", |
| 43 | + "attributes": { |
| 44 | + "title": "JSON API paints my bikeshed!", |
| 45 | + "body": "The shortest article. Ever.", |
| 46 | + "created": "2015-05-22T14:56:29.000Z", |
| 47 | + "updated": "2015-05-22T14:56:28.000Z" |
| 48 | + } |
| 49 | + } |
| 50 | + ], |
| 51 | + "links": { |
| 52 | + "self": "http://example.com/articles?page[number]=3&page[size]=1", |
| 53 | + "first": "http://example.com/articles?page[number]=1&page[size]=1", |
| 54 | + "prev": "http://example.com/articles?page[number]=2&page[size]=1", |
| 55 | + "next": "http://example.com/articles?page[number]=4&page[size]=1", |
| 56 | + "last": "http://example.com/articles?page[number]=13&page[size]=1" |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +AMS 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). |
| 62 | + |
| 63 | + |
| 64 | +### JSON adapter |
| 65 | + |
| 66 | +If you are using `JSON` adapter, pagination links will not be included automatically, but it is possible to do so using `meta` key. |
| 67 | + |
| 68 | +In your action specify a custom serializer. |
| 69 | +```ruby |
| 70 | +render json: @posts, serializer: PaginatedSerializer, each_serializer: PostPreviewSerializer |
| 71 | +``` |
| 72 | + |
| 73 | +And then, you could do something like the following class. |
| 74 | +```ruby |
| 75 | +class PaginatedSerializer < ActiveModel::Serializer::ArraySerializer |
| 76 | + def initialize(object, options={}) |
| 77 | + meta_key = options[:meta_key] || :meta |
| 78 | + options[meta_key] ||= {} |
| 79 | + options[meta_key] = { |
| 80 | + current_page: object.current_page, |
| 81 | + next_page: object.next_page, |
| 82 | + prev_page: object.prev_page, |
| 83 | + total_pages: object.total_pages, |
| 84 | + total_count: object.total_count |
| 85 | + } |
| 86 | + super(object, options) |
| 87 | + end |
| 88 | +end |
| 89 | +``` |
| 90 | +ex. |
| 91 | +```json |
| 92 | +{ |
| 93 | + "articles": [ |
| 94 | + { |
| 95 | + "id": 2, |
| 96 | + "title": "JSON API paints my bikeshed!", |
| 97 | + "body": "The shortest article. Ever." |
| 98 | + } |
| 99 | + ], |
| 100 | + "meta": { |
| 101 | + "current_page": 3, |
| 102 | + "next_page": 4, |
| 103 | + "prev_page": 2, |
| 104 | + "total_pages": 10, |
| 105 | + "total_count": 10 |
| 106 | + } |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +### FlattenJSON adapter |
| 111 | + |
| 112 | +This adapter does not allow us to use `meta` key, due to that it is not possible to add pagination links. |
0 commit comments