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
+29-8Lines changed: 29 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -76,9 +76,9 @@ $ bundle
76
76
77
77
## Why JSONAPI::Utils?
78
78
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.
80
80
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.
82
82
83
83
## Usage
84
84
@@ -93,7 +93,7 @@ JU brings two main renders to the game, working pretty much the same way as Rail
93
93
94
94
**jsonapi_render**
95
95
96
-
It takes the arguments and generates a JSON API-compliant response.
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.
175
175
176
176
> 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`.
177
177
@@ -193,14 +193,35 @@ Arguments:
193
193
194
194
#### Paginators
195
195
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:
197
218
198
219
```ruby
199
220
classCustomPaginator < JSONAPI::Paginator
200
221
defpagination_range(page_params)
201
222
offset = page_params['offset']
202
223
limit =JSONAPI.configuration.default_page_size
203
-
offset..offset + limit -1
224
+
offset..offset + limit -1# resulting range
204
225
end
205
226
```
206
227
@@ -215,7 +236,7 @@ end
215
236
216
237
### Request
217
238
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:
0 commit comments