api_scaffold 0.1.9 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/api_scaffold/version.rb +1 -1
- data/lib/generators/api_scaffold/api_scaffold_generator.rb +4 -1
- data/lib/generators/api_scaffold/generator_helpers.rb +4 -0
- data/lib/generators/api_scaffold/templates/controllers/serializer_controller.rb +2 -2
- data/lib/generators/api_scaffold/templates/serializers/error_serializer.rb +13 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bcd72aeae0a6c2bb2083da25216c0c58297ae64efbbe5300d8cdc65b36488cd5
|
4
|
+
data.tar.gz: 53049968a234146abaa00dd295b255939658c888093d3350b5913bac30ca1563
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8a6841a34cedf61627603a1186e7fabf19f344fff5abb384b7e2c2e71e348bb09d4c3b445355876215da2ddfcea345217006487e9a3057463dcfc032b0c21b70
|
7
|
+
data.tar.gz: ad2a7743d7441b37894b00d7b4d5653f05a3830f726a780e38926bafec6ed64b7e07de5f5bbcc1ccd0de14ccc85f5f9826087a2184bbd28c8d93506ab895e96a
|
data/lib/api_scaffold/version.rb
CHANGED
@@ -49,7 +49,10 @@ module ApiScaffold
|
|
49
49
|
end
|
50
50
|
|
51
51
|
def create_serializer_files
|
52
|
-
|
52
|
+
if gem_available?('fast_jsonapi') || gem_available?('active_model_serializers')
|
53
|
+
invoke "serializer"
|
54
|
+
template "serializers/error_serializer.rb", File.join("app/serializers", "error_serializer.rb") unless error_serializer_created?
|
55
|
+
end
|
53
56
|
end
|
54
57
|
|
55
58
|
end
|
@@ -26,6 +26,10 @@ module ApiScaffold
|
|
26
26
|
File.exists? File.join(destination_root, "config/initializers/apipie.rb")
|
27
27
|
end
|
28
28
|
|
29
|
+
def error_serializer_created?
|
30
|
+
File.exists? File.join(destination_root, "app/serializers/error_serializer.rb")
|
31
|
+
end
|
32
|
+
|
29
33
|
def test_framework
|
30
34
|
Rails.application.config.generators.options[:rails][:test_framework]
|
31
35
|
end
|
@@ -33,7 +33,7 @@ class <%= prefixed_controller_class_name %>Controller < ApplicationController
|
|
33
33
|
if @<%= orm_instance.save %>
|
34
34
|
render json: <%= class_name %>Serializer.new(<%= "@#{singular_table_name}" %>), status: :created, location: [:api, :<%= prefix %>, <%= "@#{singular_table_name}" %>]
|
35
35
|
else
|
36
|
-
render json: <%= "@#{
|
36
|
+
render json: { errors: ErrorSerializer.new(<%= "@#{singular_table_name}" %>) }, status: :unprocessable_entity
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
@@ -43,7 +43,7 @@ class <%= prefixed_controller_class_name %>Controller < ApplicationController
|
|
43
43
|
if @<%= orm_instance.update("#{singular_table_name}_params") %>
|
44
44
|
render json: <%= class_name %>Serializer.new(<%= "@#{singular_table_name}" %>)
|
45
45
|
else
|
46
|
-
render json: <%= "@#{
|
46
|
+
render json: { errors: ErrorSerializer.new(<%= "@#{singular_table_name}" %>) }, status: :unprocessable_entity
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module ErrorSerializer
|
2
|
+
def self.new(object)
|
3
|
+
object.errors.messages.map do |field, errors|
|
4
|
+
errors.map do |error_message|
|
5
|
+
{
|
6
|
+
status: 422,
|
7
|
+
source: { pointer: "/data/attributes/#{field}" },
|
8
|
+
detail: error_message
|
9
|
+
}
|
10
|
+
end
|
11
|
+
end.flatten
|
12
|
+
end
|
13
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api_scaffold
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stefan Atkinson
|
@@ -75,6 +75,7 @@ files:
|
|
75
75
|
- lib/generators/api_scaffold/generator_helpers.rb
|
76
76
|
- lib/generators/api_scaffold/templates/controllers/controller.rb
|
77
77
|
- lib/generators/api_scaffold/templates/controllers/serializer_controller.rb
|
78
|
+
- lib/generators/api_scaffold/templates/serializers/error_serializer.rb
|
78
79
|
- lib/generators/api_scaffold/templates/tests/rspec/controller_spec.rb
|
79
80
|
- lib/generators/api_scaffold/templates/tests/test_unit/controller_spec.rb
|
80
81
|
homepage: https://github.com/stefatkins/api_scaffold
|