Skip to content

Commit 0a11793

Browse files
authored
Update README.md
1 parent 276d993 commit 0a11793

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

README.md

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ $ bundle
7676

7777
## Why JSONAPI::Utils?
7878

79-
One of the main motivations behind `JSONAPI::Utils` is to keep things explicit in controllers (no hidden actions) so that developers can easily understand and maintain their code.
79+
One of the main motivations behind `JSONAPI::Utils` is to keep things explicit in controllers (no hidden actions :-) so that developers can easily understand and maintain their code.
8080

81-
Unlike `JSONAPI::Resources` (JR), JU doesn't care about how you will operate your controller's actions. The gem deals only with the request validation and response rendering (via JR's objects) and provides a set of useful helpers along the way. Developers can then decide how to actually operate their actions: service objects, interactors etc.
81+
Unlike `JSONAPI::Resources` (JR), JU doesn't care about how you will operate your controller actions. The gem deals only with the request validation and response rendering (via JR's objects) and provides a set of helpers (renders, formatters etc) along the way. Thus developers can decide how to actually operate their actions: service objects, interactors etc.
8282

8383
## Usage
8484

@@ -93,7 +93,7 @@ JU brings two main renders to the game, working pretty much the same way as Rail
9393

9494
**jsonapi_render**
9595

96-
It takes the arguments and generates a JSON API-compliant response.
96+
It renders a JSON API-compliant response.
9797

9898
```ruby
9999
# app/controllers/users_controller.rb
@@ -138,7 +138,7 @@ jsonapi_render json: { data: [{ id: 1, first_name: 'Tiago' }, { id: 2, first_nam
138138

139139
**jsonapi_render_errors**
140140

141-
It takes arguments and generates a JSON API-compliant error response.
141+
It renders a JSON API-compliant error response.
142142

143143
```ruby
144144
# app/controllers/users_controller.rb
@@ -171,7 +171,7 @@ jsonapi_render_errors json: errors, status: :unprocessable_entity
171171

172172
#### Formatters
173173

174-
In the backstage those are the guys which actually parse ActiveRecord/Hash objects and build a new Hash compliant with JSON API. They can be called anywhere in controllers being very useful if you need to do some work with the response's body before rendering the response.
174+
In the backstage these are the guys which actually parse the ActiveRecord/Hash object to build a new Hash compliant with JSON API's specs. Formatters can be called anywhere in controllers being very useful if you need to do some work with the response's body before rendering the actual response.
175175

176176
> Note: the resulting Hash from those methods can not be passed as argument to `JSONAPI::Utils#jsonapi_render` or `JSONAPI::Utils#jsonapi_render_error`, instead it needs to be rendered by the usual `ActionController#render`.
177177
@@ -193,14 +193,35 @@ Arguments:
193193

194194
#### Paginators
195195

196-
If you are rendering a collection of hashes, you'll need to implement the `#pagination_range` method, which returns a range for your resources. For example:
196+
Pagination works out of the box on JU, you just need to decide which kind of paginator you'd like to use.
197+
198+
It's really easy to work with pagination on JU, actually it's just a matter of chosing the [paginator you wish](http://jsonapi-resources.com/v0.8/guide/configuration.html#Defaults) in your JR's config file:
199+
200+
```ruby
201+
# config/initializers/jsonapi_resources.rb
202+
JSONAPI.configure do |config|
203+
# :none, :offset, :paged, or a custom paginator name
204+
config.default_paginator = :paged
205+
206+
# Output pagination links at top level
207+
config.top_level_links_include_pagination = true
208+
209+
# Default sizes
210+
config.default_page_size = 70
211+
config.maximum_page_size = 100
212+
end
213+
```
214+
215+
As you may have noticed above, it's possible to use custom paginators. In order to create your own paginator your just need to define a class which inherits from `JSONAPI::Paginator` and implements the `#pagination_range` method which in turn must return the range to be applied over the resulting collection.
216+
217+
For example, if you would like to paginate over a collection of hashes, you may implement the `#pagination_range` method as below:
197218

198219
```ruby
199220
class CustomPaginator < JSONAPI::Paginator
200221
def pagination_range(page_params)
201222
offset = page_params['offset']
202223
limit = JSONAPI.configuration.default_page_size
203-
offset..offset + limit - 1
224+
offset..offset + limit - 1 # resulting range
204225
end
205226
```
206227

@@ -215,7 +236,7 @@ end
215236

216237
### Request
217238

218-
Before a controller's action gets executed, `JSONAPI::Utils` will validate the request against JSON API specifications as well as evaluating the eventual query string params to check if they match the resource's definition. If something goes wrong during the validation process, JU will render an error response like this examples below:
239+
Before a controller action gets executed, `JSONAPI::Utils` will validate the request against JSON API's specs as well as evaluating the eventual query string params to check if they match the resource's definition. If something goes wrong during the validation process, JU will render an error response like this examples below:
219240

220241
```json
221242
HTTP/1.1 400 Bad Request

0 commit comments

Comments
 (0)