jsonapi-rails 0.1.1.beta2 → 0.1.1.beta3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +72 -1
- data/lib/jsonapi/rails/railtie.rb +23 -24
- data/lib/jsonapi/rails/renderer.rb +0 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3e7688f7537e54bd034bb90534c54b24a45b6609
|
4
|
+
data.tar.gz: 5ccdd0a8cec51bc47b4bf9975ee4b4ac4ac2679f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 473e402fc57f365aad748692290a4c4f675d6309e4b95f1d76426c40b5b6b48dce57417581463754184a3572b3ad50e1e02de24dafb4650d9db8c64d46494fee
|
7
|
+
data.tar.gz: 4d152fc21a5bfedbcb86c6504621230ae47e4a0ed8126bc03c056d3128b40a6ce2a709a5eb56e472dd1a10c66442fbb273582322954b3fcc9b2f2b0be5cbf701
|
data/README.md
CHANGED
@@ -23,7 +23,78 @@ $ gem install jsonapi-rails
|
|
23
23
|
|
24
24
|
## Usage
|
25
25
|
|
26
|
-
|
26
|
+
### Serialization
|
27
|
+
|
28
|
+
Example:
|
29
|
+
```ruby
|
30
|
+
# app/serializable/serializable_user.rb
|
31
|
+
class SerializableUser < JSONAPI::Serializable::Model
|
32
|
+
type 'users'
|
33
|
+
|
34
|
+
attribute :name
|
35
|
+
attribute :email do
|
36
|
+
"#{@model.name}@foo.bar"
|
37
|
+
end
|
38
|
+
|
39
|
+
has_many :posts do
|
40
|
+
# data is auto-inferred
|
41
|
+
link(:related) { @url_helpers.user_posts(@model) }
|
42
|
+
meta foo: :bar
|
43
|
+
end
|
44
|
+
|
45
|
+
has_many :comments do
|
46
|
+
data do
|
47
|
+
@user.comments.order(:desc)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
has_many :reviews, Foo::Bar::SerializableRev
|
52
|
+
|
53
|
+
link(:self) { @url_helpers.user_url(@model.id) }
|
54
|
+
meta do
|
55
|
+
{ foo: 'bar' }
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# app/controllers/users_controller.rb
|
60
|
+
# ...
|
61
|
+
user = User.find_by(id: id)
|
62
|
+
render jsonapi: user, include: { posts: [:comments] }, meta: { foo: 'bar' }
|
63
|
+
# ...
|
64
|
+
```
|
65
|
+
|
66
|
+
### Deserialization
|
67
|
+
|
68
|
+
Example:
|
69
|
+
```ruby
|
70
|
+
class PostsController < ActionController::Base
|
71
|
+
deserializable_resource :post, only: [:create, :update] do
|
72
|
+
attribute :title
|
73
|
+
attribute :date
|
74
|
+
has_one :author do |rel, id, type|
|
75
|
+
field user_id: id
|
76
|
+
field user_type: type
|
77
|
+
end
|
78
|
+
has_many :comments
|
79
|
+
end
|
80
|
+
|
81
|
+
def create_params
|
82
|
+
params.require(:user).permit!
|
83
|
+
end
|
84
|
+
|
85
|
+
def create
|
86
|
+
create_params[:title]
|
87
|
+
create_params[:date]
|
88
|
+
create_params[:comment_ids]
|
89
|
+
create_params[:comment_types]
|
90
|
+
create_params[:user_id]
|
91
|
+
create_params[:user_type]
|
92
|
+
# ...
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
|
97
|
+
```
|
27
98
|
|
28
99
|
## License
|
29
100
|
|
@@ -1,6 +1,5 @@
|
|
1
1
|
require 'rails/railtie'
|
2
2
|
require 'action_controller'
|
3
|
-
require 'action_controller/railtie'
|
4
3
|
require 'active_support'
|
5
4
|
|
6
5
|
require 'jsonapi/rails/parser'
|
@@ -12,35 +11,35 @@ module JSONAPI
|
|
12
11
|
MEDIA_TYPE = 'application/vnd.api+json'.freeze
|
13
12
|
PARSER = JSONAPI::Rails.parser
|
14
13
|
|
15
|
-
initializer '
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
else
|
20
|
-
ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime[:jsonapi]] = PARSER
|
21
|
-
end
|
14
|
+
initializer 'jsonapi-rails.action_controller' do
|
15
|
+
ActiveSupport.on_load(:action_controller) do
|
16
|
+
require 'jsonapi/rails/action_controller'
|
17
|
+
include ::JSONAPI::Rails::ActionController
|
22
18
|
|
23
|
-
|
24
|
-
|
25
|
-
|
19
|
+
Mime::Type.register MEDIA_TYPE, :jsonapi
|
20
|
+
if ::Rails::VERSION::MAJOR >= 5
|
21
|
+
::ActionDispatch::Request.parameter_parsers[:jsonapi] = PARSER
|
22
|
+
else
|
23
|
+
::ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime[:jsonapi]] = PARSER
|
24
|
+
end
|
25
|
+
|
26
|
+
::ActionController::Renderers.add :jsonapi do |json, options|
|
27
|
+
unless json.is_a?(String)
|
28
|
+
json = JSONAPI::Rails::Renderer.render(json, options)
|
29
|
+
end
|
30
|
+
self.content_type ||= Mime[:jsonapi]
|
31
|
+
self.response_body = json
|
26
32
|
end
|
27
|
-
self.content_type ||= Mime[:jsonapi]
|
28
|
-
self.response_body = json
|
29
|
-
end
|
30
33
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
+
::ActionController::Renderers.add :jsonapi_errors do |json, options|
|
35
|
+
unless json.is_a?(String)
|
36
|
+
json = JSONAPI::Rails::ErrorRender.render_errors(json, options)
|
37
|
+
end
|
38
|
+
self.content_type ||= Mime[:jsonapi]
|
39
|
+
self.response_body = json
|
34
40
|
end
|
35
|
-
self.content_type ||= Mime[:jsonapi]
|
36
|
-
self.response_body = json
|
37
41
|
end
|
38
42
|
end
|
39
43
|
end
|
40
44
|
end
|
41
45
|
end
|
42
|
-
|
43
|
-
ActiveSupport.on_load(:action_controller) do
|
44
|
-
require 'jsonapi/rails/action_controller'
|
45
|
-
include JSONAPI::Rails::ActionController
|
46
|
-
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.1.
|
4
|
+
version: 0.1.1.beta3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lucas Hosseini
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-11-
|
11
|
+
date: 2016-11-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: jsonapi-renderer
|