Skip to content

Commit f78b371

Browse files
committed
2 parents 4420e2a + 280c62e commit f78b371

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

README.md

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ JSONAPI::Utils (JU) was built on top of [JSONAPI::Resources](https://github.com/
1313
Add these lines to your application's Gemfile:
1414

1515
```ruby
16-
gem 'jsonapi-utils', '~> 0.4.3'
16+
gem 'jsonapi-utils', '~> 0.4.4'
1717
```
1818

1919
And then execute:
@@ -22,11 +22,23 @@ And then execute:
2222
$ bundle
2323
```
2424

25-
## Macros
25+
## Response Macros
2626

2727
### jsonapi_render
2828

29-
Validates the request and renders ActiveRecord/Hash objects into JSON API-compliant responses.
29+
Takes ActiveRecord/Hash objects and generates JSON API-compliant responses.
30+
31+
```ruby
32+
# GET /users
33+
def index
34+
jsonapi_render json: User.all
35+
end
36+
37+
# GET /users/:id
38+
def show
39+
jsonapi_render json: User.find(params[:id])
40+
end
41+
```
3042

3143
Arguments:
3244

@@ -37,13 +49,9 @@ Arguments:
3749
- `count`: explicitly points the total count of records for the request in order to build a proper pagination. By default, JU will count the total number of records.
3850
- `model`: sets the model reference in cases when `json` is a Hash or a collection of Hashes.
3951

40-
```ruby
41-
# Single resource rendering
42-
jsonapi_render json: User.find(params[:id])
43-
44-
# Collection rendering
45-
jsonapi_render json: User.all
52+
Examples:
4653

54+
```ruby
4755
# Specify a particular HTTP status code
4856
jsonapi_render json: new_user, status: :created
4957

@@ -62,15 +70,17 @@ jsonapi_render json: { data: [{ id: 1, first_name: 'Tiago' }, { id: 2, first_nam
6270

6371
### jsonapi_serialize
6472

65-
In the backstage that's the method that actually parsers ActiveRecord/Hash objects and builds a new Hash compliant with JSON API. It can be called anywhere in controllers, concerns etc.
73+
In the backstage this is the method that actually parses ActiveRecord/Hash objects and builds a new Hash compliant with JSON API. It can be called anywhere in your controllers being very useful whenever you need to work with a JSON API "serialized" version of your object before rendering it.
6674

6775
```ruby
68-
collection = jsonapi_serialize(User.all)
69-
render json: do_some_magic_with(collection)
76+
def index
77+
result = do_some_magic(jsonapi_serialize(User.all))
78+
render json: result
79+
end
7080
```
7181

7282
Arguments:
73-
- It receives a second optional argument with the same options for `jsonapi_render`.
83+
- It receives the same options as `jsonapi_render`.
7484

7585
## Usage
7686

0 commit comments

Comments
 (0)