|
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 | | - |
7 | | -### Adding JSONAPICompliable |
8 | | -``` |
9 | | -class ApplicationController < ActionController::Base |
10 | | - include JSONAPICompliable |
11 | | -end |
12 | | -
|
13 | | -``` |
14 | | -### Define whitelist includes |
15 | | -``` |
16 | | -class AuthorsController < ApplicationController |
17 | | - jsonapi do |
18 | | - 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 |
27 | | - end |
28 | | -end |
29 | | -``` |
30 | | -Below url requesting state/foo as includes. |
31 | | -But foo include is ignored as it is not whitelist. |
32 | | ->authors?include=state,foo |
33 | | -
|
34 | | -### Defining filter field |
35 | | - |
36 | | -``` |
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 |
| 1 | +### JsonapiCompliable |
45 | 2 |
|
46 | | -### Defining filter field alias |
| 3 | +[official documentation](https://bbgithub.dev.bloomberg.com/pages/InfrastructureExperience/jsonapi_compliable) |
47 | 4 |
|
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 |
| 5 | +Supported Rails versions: >= 4.1 |
57 | 6 |
|
58 | | -### Adding guard to filter |
| 7 | +### Running tests |
59 | 8 |
|
60 | | -``` |
61 | | -class AuthorsController < ApplicationController |
62 | | - jsonapi do |
63 | | - allow_filter :last_name if: :can_filter? |
64 | | - end |
| 9 | +We support Rails >= 4.1. To do so, we use the [appraisal](https://github.com/thoughtbot/appraisal) gem. So, run: |
65 | 10 |
|
66 | | - def can_filter? |
67 | | - true |
68 | | - end |
69 | | -end |
| 11 | +```bash |
| 12 | +$ bin/appraisal rails-4 bin/rspec |
| 13 | +$ bin/appraisal rails-5 bin/rspec |
70 | 14 | ``` |
71 | 15 |
|
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' |
| 16 | +Or run tests for all versions: |
84 | 17 |
|
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 | | - } |
| 18 | +```bash |
| 19 | +$ bin/appraisal bin/rspec |
124 | 20 | ``` |
0 commit comments