|
| 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 | + |
1 | 7 | ### Adding JSONAPICompliable
|
2 | 8 | ```
|
3 | 9 | class ApplicationController < ActionController::Base
|
4 | 10 | include JSONAPICompliable
|
5 | 11 | end
|
| 12 | +
|
6 | 13 | ```
|
7 |
| -### Define whitelist includes/filters per controller. |
| 14 | +### Define whitelist includes |
8 | 15 | ```
|
9 |
| -class BooksController < ApplicationController |
| 16 | +class AuthorsController < ApplicationController |
10 | 17 | jsonapi do
|
11 | 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 |
12 | 27 | end
|
13 | 28 | end
|
14 | 29 | ```
|
15 | 30 | Below url requesting state/foo as includes.
|
16 | 31 | But foo include is ignored as it is not whitelist.
|
| 32 | +>authors?include=state,foo |
| 33 | +
|
| 34 | +### Defining filter field |
| 35 | + |
17 | 36 | ```
|
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 | + } |
19 | 124 | ```
|
0 commit comments