From f85027e631435879204645198726a3a4085835c1 Mon Sep 17 00:00:00 2001 From: Bruno Bacarini Date: Wed, 19 Aug 2015 11:09:47 -0300 Subject: [PATCH] add more documentation to pagination links --- README.md | 4 +-- docs/README.md | 2 +- docs/howto/add_pagination_links.md | 54 +++++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ef103ba1..24152c4f 100644 --- a/README.md +++ b/README.md @@ -274,9 +274,9 @@ The `url` declaration describes which named routes to use while generating URLs for your JSON. Not every adapter will require URLs. ## Pagination -Pagination links will be included in your response automatically as long as the resource is paginated using [Kaminari](https://github.com/amatsuda/kaminari) or [WillPaginate](https://github.com/mislav/will_paginate) and if you are using a ```JSON-API``` adapter. The others adapters does not have this feature. +Pagination links will be included in your response automatically as long as the resource is paginated using [Kaminari](https://github.com/amatsuda/kaminari) or [WillPaginate](https://github.com/mislav/will_paginate) and if you are using a ```JSON-API``` adapter. -For more information about it, please see in our docs [How to add pagination links](https://github.com/rails-api/active_model_serializers/blob/master/docs/howto/add_pagination_links.md) +Although the others adapters does not have this feature, it is possible to implement pagination links to `JSON` adapter. For more information about it, please see in our docs [How to add pagination links](https://github.com/rails-api/active_model_serializers/blob/master/docs/howto/add_pagination_links.md) ## Caching diff --git a/docs/README.md b/docs/README.md index 2398528b..5bd9c2e1 100644 --- a/docs/README.md +++ b/docs/README.md @@ -12,7 +12,7 @@ This is the documentation of AMS, it's focused on the **0.10.x version.** ## How to - [How to add root key](howto/add_root_key.md) -- [How to add pagination links](howto/add_pagination_links.md) (```JSON-API``` only) +- [How to add pagination links](howto/add_pagination_links.md) ## Getting Help diff --git a/docs/howto/add_pagination_links.md b/docs/howto/add_pagination_links.md index d49095b8..87fc887f 100644 --- a/docs/howto/add_pagination_links.md +++ b/docs/howto/add_pagination_links.md @@ -1,6 +1,8 @@ # How to add pagination links -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. The others adapters does not have this feature. +### JSON-API adapter + +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. If you want pagination links in your response, use [Kaminari](https://github.com/amatsuda/kaminari) or [WillPaginate](https://github.com/mislav/will_paginate). @@ -44,3 +46,53 @@ ex: ``` AMS relies on either [Kaminari](https://github.com/amatsuda/kaminari) or [WillPaginate](https://github.com/mislav/will_paginate). Please install either dependency by adding one of those to your Gemfile. + +### JSON adapter + +If you are using `JSON` adapter, pagination links will not be included automatically, but it is possible to do so using `meta` key. + +In your action specify a custom serializer. +```ruby +render json: @posts, serializer: PaginatedSerializer, each_serializer: PostPreviewSerializer +``` + +And then, you could do something like the following class. +```ruby +class PaginatedSerializer < ActiveModel::Serializer::ArraySerializer + def initialize(object, options={}) + meta_key = options[:meta_key] || :meta + options[meta_key] ||= {} + options[meta_key] = { + current_page: object.current_page, + next_page: object.next_page, + prev_page: object.prev_page, + total_pages: object.total_pages, + total_count: object.total_count + } + super(object, options) + end +end +``` +ex. +```json +{ + "articles": [ + { + "id": 2, + "title": "JSON API paints my bikeshed!", + "body": "The shortest article. Ever." + } + ], + "meta": { + "current_page": 3, + "next_page": 4, + "prev_page": 2, + "total_pages": 10, + "total_count": 10 + } +} +``` + +### FlattenJSON adapter + +This adapter does not allow us to use `meta` key, due to that it is not possible to add pagination links.