json_api_responders 2.1.0 → 2.1.3
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 -5
- data/lib/json_api_responders/config.rb +21 -4
- data/lib/json_api_responders/responder/actions.rb +2 -2
- data/lib/json_api_responders/version.rb +1 -1
- data/lib/json_api_responders.rb +0 -9
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1183871fbe98771c58abcc316bf0f38bdb631668
|
4
|
+
data.tar.gz: 454bfdb1b9172646beee12f2fae8f8b7407aa6d7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c5a94081106448d1522331b5ea27a66b8c9ccd5fdf99060f7943ca9f7e9a2b788826029b9c69ab506df77429901cc5da2db65bce5d7bf488ac73b759f7b18acc
|
7
|
+
data.tar.gz: 6cb309410bc083aa6084a179dae0fd8a8bbf839a55294ba163001f1ea5acd43eeba2a3e0e57c83fbd5b9f83830b7ca1637f0118abfc980369186289f73f128d0
|
data/README.md
CHANGED
@@ -31,16 +31,21 @@ whatever can be passed to controller.render can be passed here:
|
|
31
31
|
respond_with user, serializer: UserSerializer
|
32
32
|
|
33
33
|
## Configuration
|
34
|
-
Currently you can only configure which options are required to be passed through the `respond_with` method. Bellow you can find an example:
|
34
|
+
Currently you can only configure which options are required to be passed through the `respond_with` method. These required options are categorized by the controller's actions. Bellow you can find an example:
|
35
35
|
|
36
36
|
JsonApiResponders.configure do |config|
|
37
|
-
config.required_options =
|
37
|
+
config.required_options = {
|
38
|
+
index: [:each_serializer],
|
39
|
+
create: [:serializer]
|
40
|
+
}
|
38
41
|
end
|
39
42
|
...
|
40
|
-
|
41
|
-
|
43
|
+
def create
|
44
|
+
user = User.create(...)
|
45
|
+
respond_with user, serializer: UserSerializer
|
46
|
+
end
|
42
47
|
|
43
|
-
If `:
|
48
|
+
If `:serializer` was left out of the above `respond_with` method you would see the `JsonApiResponders::Errors::RequiredOptionMissingError` be raised.
|
44
49
|
|
45
50
|
## Responses
|
46
51
|
|
@@ -2,15 +2,32 @@ module JsonApiResponders
|
|
2
2
|
class Config
|
3
3
|
attr_reader :required_options
|
4
4
|
|
5
|
-
def required_options=(opts =
|
5
|
+
def required_options=(opts = {})
|
6
6
|
@required_options = opts
|
7
7
|
end
|
8
8
|
|
9
9
|
def check_required_options(options)
|
10
|
-
return if @required_options.nil?
|
11
|
-
|
12
|
-
|
10
|
+
return if @required_options.nil? || @required_options.empty?
|
11
|
+
action = action(options)
|
12
|
+
|
13
|
+
if action && @required_options.key?(action)
|
14
|
+
@required_options[action].each do |key|
|
15
|
+
raise JsonApiResponders::Errors::RequiredOptionMissingError, key unless options.key? key
|
16
|
+
end
|
13
17
|
end
|
14
18
|
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def action(options)
|
23
|
+
options[:params][:action].to_sym if action_present?(options)
|
24
|
+
end
|
25
|
+
|
26
|
+
def action_present?(options)
|
27
|
+
!options[:params].nil? &&
|
28
|
+
!options[:params].empty? &&
|
29
|
+
!options[:params][:action].nil? &&
|
30
|
+
!options[:params][:action].empty?
|
31
|
+
end
|
15
32
|
end
|
16
33
|
end
|
@@ -18,7 +18,7 @@ module JsonApiResponders
|
|
18
18
|
self.status ||= :created
|
19
19
|
render_resource
|
20
20
|
else
|
21
|
-
self.status ||= :
|
21
|
+
self.status ||= :unprocessable_entity
|
22
22
|
render_error
|
23
23
|
end
|
24
24
|
end
|
@@ -28,7 +28,7 @@ module JsonApiResponders
|
|
28
28
|
self.status ||= :ok
|
29
29
|
render_resource
|
30
30
|
else
|
31
|
-
self.status ||= :
|
31
|
+
self.status ||= :unprocessable_entity
|
32
32
|
render_error
|
33
33
|
end
|
34
34
|
end
|
data/lib/json_api_responders.rb
CHANGED
@@ -31,15 +31,6 @@ module JsonApiResponders
|
|
31
31
|
Responder.new(self, nil, on_error: { status: status, detail: detail }).respond_error
|
32
32
|
end
|
33
33
|
|
34
|
-
def deserialized_params
|
35
|
-
@_deserialized_options ||=
|
36
|
-
ActionController::Parameters.new(
|
37
|
-
ActiveModelSerializers::Deserialization.jsonapi_parse(
|
38
|
-
params, json_api_parse_options
|
39
|
-
)
|
40
|
-
)
|
41
|
-
end
|
42
|
-
|
43
34
|
private
|
44
35
|
|
45
36
|
def record_not_found!
|