Skip to content

Commit a14417d

Browse files
committed
Updated documentation
1 parent cda2076 commit a14417d

File tree

1 file changed

+108
-3
lines changed

1 file changed

+108
-3
lines changed

README.md

Lines changed: 108 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,124 @@
1+
[official documentation](https://bbgithub.dev.bloomberg.com/pages/InfrastructureExperience/jsonapi_compliable)
2+
3+
### Getting Started
4+
Please read below documentation related to [jsonapi](http://jsonapi.org/format/#document-resource-objects)
5+
6+
17
### Adding JSONAPICompliable
28
```
39
class ApplicationController < ActionController::Base
410
include JSONAPICompliable
511
end
12+
613
```
7-
### Define whitelist includes/filters per controller.
14+
### Define whitelist includes
815
```
9-
class BooksController < ApplicationController
16+
class AuthorsController < ApplicationController
1017
jsonapi do
1118
includes whitelist: { index: [{ books: :genre }, :state] }
19+
20+
allow_filter :last_name
21+
22+
allow_filter :first_name, aliases: [:title], if: :can_filter_first_name?
23+
24+
allow_filter :first_name_prefix do |scope, filter|
25+
scope.where('first_name like ?', "#{filter}%")
26+
end
1227
end
1328
end
1429
```
1530
Below url requesting state/foo as includes.
1631
But foo include is ignored as it is not whitelist.
32+
>authors?include=state,foo
33+
34+
### Defining filter field
35+
1736
```
18-
/books?include=state%2Cfoo
37+
class AuthorsController < ApplicationController
38+
jsonapi do
39+
allow_filter :last_name
40+
end
41+
end
42+
```
43+
will allow us make request as below
44+
>/authors?filter[first_name]=john
45+
46+
### Defining filter field alias
47+
48+
```
49+
class AuthorsController < ApplicationController
50+
jsonapi do
51+
allow_filter :last_name, aliases: [:name]
52+
end
53+
end
54+
```
55+
will allow us make request as below
56+
>/authors?filter[name]=john
57+
58+
### Adding guard to filter
59+
60+
```
61+
class AuthorsController < ApplicationController
62+
jsonapi do
63+
allow_filter :last_name if: :can_filter?
64+
end
65+
66+
def can_filter?
67+
true
68+
end
69+
end
70+
```
71+
72+
### Defining default filter
73+
74+
```
75+
class AuthorsController < ApplicationController
76+
jsonapi do
77+
default_filter :first_name do |scope|
78+
scope.where(first_name: 'Willaim')
79+
end
80+
end
81+
end
82+
```
83+
if no filter provided below request will filter authors first_name='Willaim'
84+
85+
>/authors
86+
87+
### Deserialize requests params which are coming jsonapi format
88+
89+
```
90+
class AuthorsController < ApplicationController
91+
before_action :deserialize_jsonapi!, only: [:update, :create]
92+
end
93+
```
94+
incoming parameters
95+
```
96+
{
97+
data: {
98+
type: 'authors',
99+
attributes: {
100+
first_name: 'Stephen',
101+
last_name: 'King'
102+
},
103+
relationships: {
104+
books: {
105+
data: [
106+
{ type: 'books', attributes: { title: 'The Shining' } }
107+
]
108+
}
109+
}
110+
}
111+
}
112+
```
113+
will be deserialized to
114+
```
115+
{
116+
authors: {
117+
first_name: 'Stephen',
118+
last_name: 'King'
119+
books_attributes: [{
120+
title: 'The Shingin'
121+
}]
122+
}
123+
}
19124
```

0 commit comments

Comments
 (0)