|
1 |
| -# JsonapiCompliable |
| 1 | +[official documentation](https://bbgithub.dev.bloomberg.com/pages/InfrastructureExperience/jsonapi_compliable) |
2 | 2 |
|
3 |
| -Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/jsonapi_compliable`. To experiment with that code, run `bin/console` for an interactive prompt. |
| 3 | +### Getting Started |
| 4 | +Please read below documentation related to [jsonapi](http://jsonapi.org/format/#document-resource-objects) |
4 | 5 |
|
5 |
| -TODO: Delete this and the text above, and describe your gem |
6 | 6 |
|
7 |
| -## Installation |
8 |
| - |
9 |
| -Add this line to your application's Gemfile: |
10 |
| - |
11 |
| -```ruby |
12 |
| -gem 'jsonapi_compliable' |
| 7 | +### Adding JSONAPICompliable |
13 | 8 | ```
|
| 9 | +class ApplicationController < ActionController::Base |
| 10 | + include JSONAPICompliable |
| 11 | +end |
14 | 12 |
|
15 |
| -And then execute: |
| 13 | +``` |
| 14 | +### Define whitelist includes |
| 15 | +``` |
| 16 | +class AuthorsController < ApplicationController |
| 17 | + jsonapi do |
| 18 | + includes whitelist: { index: [{ books: :genre }, :state] } |
16 | 19 |
|
17 |
| - $ bundle |
| 20 | + allow_filter :last_name |
18 | 21 |
|
19 |
| -Or install it yourself as: |
| 22 | + allow_filter :first_name, aliases: [:title], if: :can_filter_first_name? |
20 | 23 |
|
21 |
| - $ gem install jsonapi_compliable |
| 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 |
22 | 33 |
|
23 |
| -## Usage |
| 34 | +### Defining filter field |
24 | 35 |
|
25 |
| -TODO: Write usage instructions here |
| 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 |
26 | 45 |
|
27 |
| -## Development |
| 46 | +### Defining filter field alias |
28 | 47 |
|
29 |
| -After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. |
| 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 |
30 | 57 |
|
31 |
| -To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). |
| 58 | +### Adding guard to filter |
32 | 59 |
|
33 |
| -## Contributing |
| 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 | +``` |
34 | 71 |
|
35 |
| -Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/jsonapi_compliable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. |
| 72 | +### Defining default filter |
36 | 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' |
37 | 84 |
|
38 |
| -## License |
| 85 | +>/authors |
39 | 86 |
|
40 |
| -The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). |
| 87 | +### Deserialize requests params which are coming jsonapi format |
41 | 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 | + } |
| 124 | +``` |
0 commit comments