jsonapi-utils 0.6.0.beta → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -11
- data/lib/jsonapi/utils/response/formatters.rb +1 -1
- data/lib/jsonapi/utils/version.rb +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0efb4509e06e84372f0c8eb578bda2ee0c1996d7
|
4
|
+
data.tar.gz: 0c6ed180ee94395c772150647a96caeb4067c08e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0aa6e06f2971a5ef29a353950c258ee883ddcf52d7398460edf5b3da622618c7cd7afd27d42c74df729cf03dedc56a0e0b7fbd6ed2a5a4ecad063c7a90007d67
|
7
|
+
data.tar.gz: 88f627109d057c93192792df420ddd6948e332a98ca4f98d97a6772bbbaa319a0c5b139cb1b3c9c706eda7023fa84f7f0171c41833a018ca3a4bc3117349c133
|
data/README.md
CHANGED
@@ -283,12 +283,12 @@ JU brings helper methods as a shortcut to get values from permitted params based
|
|
283
283
|
|
284
284
|
After installing the gem you simply need to:
|
285
285
|
|
286
|
-
1. Include
|
287
|
-
2. Define the resources
|
288
|
-
3. Define routes;
|
289
|
-
4. Use
|
286
|
+
1. Include the gem's module (`include JSONAPI::Utils`) in a controller (eg. `BaseController`);
|
287
|
+
2. Define the resources which will be exposed via REST API;
|
288
|
+
3. Define the application's routes;
|
289
|
+
4. Use JSONAPI Utils' helper methods (eg. renders, formatters, params helpers etc).
|
290
290
|
|
291
|
-
Ok, it's time
|
291
|
+
Ok, now it's time to start our complete example. So, let's say we have a Rails application for a super simple blog:
|
292
292
|
|
293
293
|
### Models
|
294
294
|
|
@@ -299,7 +299,7 @@ class User < ActiveRecord::Base
|
|
299
299
|
validates :first_name, :last_name, presence: true
|
300
300
|
end
|
301
301
|
|
302
|
-
# app/models/
|
302
|
+
# app/models/post.rb
|
303
303
|
class Post < ActiveRecord::Base
|
304
304
|
belongs_to :author, class_name: 'User', foreign_key: 'user_id'
|
305
305
|
validates :title, :body, presence: true
|
@@ -308,7 +308,7 @@ end
|
|
308
308
|
|
309
309
|
### Resources
|
310
310
|
|
311
|
-
Here is where we define how
|
311
|
+
Here is where we define how our models are exposed as resources on the API:
|
312
312
|
|
313
313
|
```ruby
|
314
314
|
# app/resources/user_resource.rb
|
@@ -331,20 +331,19 @@ end
|
|
331
331
|
|
332
332
|
### Routes & Controllers
|
333
333
|
|
334
|
-
Let's define the routes using the `jsonapi_resources`
|
334
|
+
Let's define the routes using the `jsonapi_resources` method provided by JR:
|
335
335
|
|
336
336
|
```ruby
|
337
337
|
Rails.application.routes.draw do
|
338
338
|
jsonapi_resources :users do
|
339
339
|
jsonapi_resources :posts
|
340
|
-
jsonapi_links :posts
|
341
340
|
end
|
342
341
|
end
|
343
342
|
```
|
344
343
|
|
345
344
|
In controllers we just need to include the `JSONAPI::Utils` module.
|
346
345
|
|
347
|
-
> Note:
|
346
|
+
> Note: some default rendering can be set like the below example where `jsonapi_render_not_found` is used when a record is not found in the database.
|
348
347
|
|
349
348
|
```ruby
|
350
349
|
# app/controllers/base_controller.rb
|
@@ -355,7 +354,7 @@ class BaseController < ActionController::Base
|
|
355
354
|
end
|
356
355
|
```
|
357
356
|
|
358
|
-
|
357
|
+
With the helper methods inhirited from `JSONAPI::Utils` in our `BaseController`, now it's all about to write our actions like the following:
|
359
358
|
|
360
359
|
```ruby
|
361
360
|
# app/controllers/users_controller.rb
|
@@ -22,7 +22,7 @@ module JSONAPI
|
|
22
22
|
private
|
23
23
|
|
24
24
|
def active_record_obj?(data)
|
25
|
-
data.is_a?(ActiveRecord::Base)|| data.singleton_class.include?(ActiveModel::Model)
|
25
|
+
defined?(ActiveRecord::Base) && (data.is_a?(ActiveRecord::Base) || data.singleton_class.include?(ActiveModel::Model))
|
26
26
|
end
|
27
27
|
|
28
28
|
def build_response_document(records, options)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi-utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.0
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tiago Guedes
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2017-
|
12
|
+
date: 2017-05-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: jsonapi-resources
|
@@ -193,12 +193,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
193
193
|
version: '0'
|
194
194
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
195
195
|
requirements:
|
196
|
-
- - "
|
196
|
+
- - ">="
|
197
197
|
- !ruby/object:Gem::Version
|
198
|
-
version:
|
198
|
+
version: '0'
|
199
199
|
requirements: []
|
200
200
|
rubyforge_project:
|
201
|
-
rubygems_version: 2.6.
|
201
|
+
rubygems_version: 2.6.11
|
202
202
|
signing_key:
|
203
203
|
specification_version: 4
|
204
204
|
summary: JSON::Utils is a simple way to get a full-featured JSON API serialization
|