api_scaffold 0.1.9 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '035423092a1cbcd12e5389d792d39479eb85000569a787625365c0325b99abf0'
4
- data.tar.gz: 7723b115197ac35b1a36b955ed829981bd1d0a02dc555c2842cfb5a60e3b7383
3
+ metadata.gz: bcd72aeae0a6c2bb2083da25216c0c58297ae64efbbe5300d8cdc65b36488cd5
4
+ data.tar.gz: 53049968a234146abaa00dd295b255939658c888093d3350b5913bac30ca1563
5
5
  SHA512:
6
- metadata.gz: 4cee5a77e3969898ce193d4d84becab080c5225c6a0ef73905e78356c485188fa934f8ccd538d17353132cecc430b398a9c4626c3174a5f748ae54abc13d636b
7
- data.tar.gz: fccb0a63a957ead2e90773076bbce9923ae7f6d4a8f5ad7a050da6dd0c307b31aee77edca5a36053591c06c5de6201a06179826362abb3808d3de2ea30dbd7e4
6
+ metadata.gz: 8a6841a34cedf61627603a1186e7fabf19f344fff5abb384b7e2c2e71e348bb09d4c3b445355876215da2ddfcea345217006487e9a3057463dcfc032b0c21b70
7
+ data.tar.gz: ad2a7743d7441b37894b00d7b4d5653f05a3830f726a780e38926bafec6ed64b7e07de5f5bbcc1ccd0de14ccc85f5f9826087a2184bbd28c8d93506ab895e96a
@@ -1,3 +1,3 @@
1
1
  module ApiScaffold
2
- VERSION = "0.1.9"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -49,7 +49,10 @@ module ApiScaffold
49
49
  end
50
50
 
51
51
  def create_serializer_files
52
- invoke "serializer" if gem_available?('fast_jsonapi') || gem_available?('active_model_serializers')
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: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity
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: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity
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.1.9
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