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
Copy file name to clipboardExpand all lines: README.md
+68-1Lines changed: 68 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -330,6 +330,70 @@ class AuthorResource < JSONAPI::Resource
330
330
end
331
331
```
332
332
333
+
#### Pagination
334
+
335
+
Pagination is performed using a `paginator`, which is a class responsible for parsing the `page` request parameters and applying the pagination logic to the results.
336
+
337
+
##### Paginators
338
+
339
+
`JSONAPI::Resource` supports several pagination methods by default, and allows you to implement a custom system if the defaults do not meet your needs.
340
+
341
+
###### Paged Paginator
342
+
343
+
The `paged``paginator` returns results based on pages of a fixed size. Valid `page` parameters are `number` and `size`. If `number` is omitted the first page is returned. If `size` is omitted the `default_page_size` from the configuration settings is used.
344
+
345
+
###### Offset Paginator
346
+
347
+
The `offset``paginator` returns results based on an offset from the beginning of the resultset. Valid `page` parameters are `offset` and `limit`. If `offset` is omitted a value of 0 will be used. If `limit` is omitted the `default_page_size` from the configuration settings is used.
348
+
349
+
###### Custom Paginators
350
+
351
+
Custom `paginators` can be used. These should derive from `Paginator`. The `apply` method takes a `relation` and is expected to return a `relation`. The `initialize` method receives the parameters from the `page` request parameters. It is up to the paginator author to parse and validate these parameters.
352
+
353
+
For example, here is a very simple single record at a time paginator:
354
+
355
+
```ruby
356
+
classSingleRecordPaginator < JSONAPI::Paginator
357
+
definitialize(params)
358
+
# param parsing and validation here
359
+
@page= params.to_i
360
+
end
361
+
362
+
defapply(relation)
363
+
relation.offset(@page).limit(1)
364
+
end
365
+
end
366
+
```
367
+
368
+
##### Paginator Configuration
369
+
370
+
The default paginator, which will be used for all resources, is set using `JSONAPI.configure`. For example:
371
+
372
+
```ruby
373
+
JSONAPI.configure do |config|
374
+
# built in paginators are :none, :offset, :cursor, :paged
375
+
self.default_paginator =:offset
376
+
377
+
self.default_page_size =10
378
+
self.maximum_page_size =20
379
+
end
380
+
```
381
+
382
+
If no `default_paginator` is configured, pagination will be disabled by default.
383
+
384
+
Paginators can also be set at the resource-level, which will override the default setting. This is done using the `paginator` method:
385
+
386
+
```ruby
387
+
classBookResource < JSONAPI::Resource
388
+
attribute :title
389
+
attribute :isbn
390
+
391
+
paginator :offset
392
+
end
393
+
```
394
+
395
+
To disable pagination in a resource, specify `:none` for `paginator`.
396
+
333
397
#### Callbacks
334
398
335
399
`ActiveSupport::Callbacks` is used to provide callback functionality, so the behavior is very similar to what you may be used to from `ActiveRecord`.
0 commit comments