-
Notifications
You must be signed in to change notification settings - Fork 413
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
237 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
--- | ||
title: Jsonapi | ||
categories: | ||
- Feature | ||
- Extra | ||
--- | ||
|
||
# Jsonapi Extra | ||
|
||
Implements `JSON:API` specifications for pagination. | ||
|
||
When enabled, the query params used in the pagy URLs are nested under the `page` param, as specified by the [Query Parameter Family](https://jsonapi.org/format/#query-parameters-families) e.g. `https://example.com/products?page[page]=2&page[items]=30`. | ||
|
||
It works also with the [countless](countless.md), [searchkick](searchkick.md), [elasticsearch_rails](elasticsearch_rails.md) and [meilisearch](/docs/extras/meilisearch.md) extras. | ||
|
||
It does not make sense (and doesn't work) with the [Calendar](countless.md) extra. | ||
|
||
## Synopsis | ||
|
||
### Default usage | ||
|
||
||| pagy.rb (initializer) | ||
```ruby | ||
require 'pagy/extras/jsonapi' # works without further configuration | ||
``` | ||
||| | ||
|
||
||| Controller | ||
```ruby | ||
# enabled by default | ||
@pagy, @records = pagy(collection) | ||
# you can disable it explicitly for specific requests | ||
@pagy, @records = pagy(collection, jsonapi: false) | ||
``` | ||
||| | ||
|
||
### Custom usage | ||
|
||
||| pagy.rb (initializer) | ||
```ruby | ||
# optionally require other jsonapi-useful extras | ||
require 'pagy/extras/items' | ||
# jsonapi must be required AFTER other extras | ||
require 'pagy/extras/jsonapi' | ||
# optionally disable it by default (opt-in) | ||
Pagy::DEFAULT[:jsonapi] = false # default true | ||
``` | ||
||| | ||
|
||
||| Controller | ||
```ruby | ||
# disabled by default by the above Pagy::DEFAULT[:jsonapi] = false | ||
@pagy, @records = pagy(collection) | ||
# explicitly enable it for specific requests | ||
@pagy, @records = pagy(collection, jsonapi: true) | ||
# optional/custom setup | ||
@pagy, @records = pagy(collection, jsonapi: true, # enable the jsonapi specifications | ||
items_extra: true, # enable the items extra | ||
page_param: :number, # use page[number] param name instead of page[page] | ||
items_params: :size) # use page[size] param name instead of page[items] | ||
# get the links URL hash | ||
links_hash = pagy_jsonapi_links(@pagy) | ||
#=> {first: 'https://example.com/products?page[number]=1&page[size]=50&...', | ||
# last: 'https://example.com/products?page[number]=32&page[size]=50&...', | ||
# prev: 'https://example.com/products?page[number]=31&page[size]=50&...', | ||
# next: 'https://example.com/products?page[number]=33&page[size]=50&...'} | ||
``` | ||
||| | ||
|
||
## Interaction with extras | ||
|
||
If you want to allow your JSON:API app to allow the client to request a specific number of items per page and capping the request to a max number you can use the [items extra](/docs/extras/items.md). | ||
|
||
The [metadata extra]((/docs/extras/metadata.md)) implements also the `pagy_jsonapi_links` method returning the link hash (https://jsonapi.org/format/#fetching-pagination) | ||
|
||
An app that implements a JSON:API, if wants to allow the client to request a specific number of items per page, also capping the max number requested by the client. | ||
|
Oops, something went wrong.