jsonapi-swagger 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5ac2522e526917a176239ca6a576b1313b935b13e5f1abd6c361a4d2650867e4
4
- data.tar.gz: cba8776fff22be079490f5c450e707b2b94b08da277b58f5fa88ebbdda7bdc77
3
+ metadata.gz: 672b181b285eb30f45c6c065d501ff38ae81eec6e99f9e2af9769c96aec13310
4
+ data.tar.gz: e1505e3fb17b074cf995e3205afebd1e780f99aa0834fdd96e8b1b8e99ff9057
5
5
  SHA512:
6
- metadata.gz: c776c446efd665e72bbbb54595984fa37c92a250c351a4f237eebcc83f73505f63eb18574ccde7b18bc9695ac155c2d3bdd12697dd445faff56a8871c4e74994
7
- data.tar.gz: ebf4e2010729433bc3b8e2af940d744b33810bf1a5579e24c0db61bf51b65c0196092110528ff9fe97132f52bee508d57029c18aa3af13897096c4607e0583db
6
+ metadata.gz: a02dfefbeac1694c3a2e01082278c3f09f1d84df9491453015dc1e4e8f571fbfdb18ef325d82f9b5332a0d05396d10d047e039b7cfbdaf8743ea99c622fba1c6
7
+ data.tar.gz: 41555cee548db4afd52714266f291bb8281ea35ed04f2b4266db30b0edf991f9dc596b5dc8dcfc55942dd7463f48e06cd6ba818fac9dc0394feb3e08fa7a16a7
data/README.md CHANGED
@@ -40,7 +40,7 @@ end
40
40
 
41
41
  ```sh
42
42
  # gen swagger/v1/swagger.json
43
- bundle exec rails generate jsonapi:swagger User # UserResponse < JSONAPI::Resource
43
+ bundle exec rails generate jsonapi:swagger User # UserResource < JSONAPI::Resource
44
44
  ```
45
45
 
46
46
  3. additional
@@ -86,15 +86,15 @@ module Jsonapi
86
86
  end
87
87
 
88
88
  def resource_klass
89
- "#{model_class_name}Resource".safe_constantize
89
+ @resource_klass ||= Jsonapi::Swagger::Resource.with(model_class_name)
90
90
  end
91
91
 
92
92
  def attributes
93
- resource_klass._attributes.except(:id)
93
+ resource_klass.attributes.except(:id)
94
94
  end
95
95
 
96
96
  def relationships
97
- resource_klass._relationships
97
+ resource_klass.relationships
98
98
  end
99
99
 
100
100
  def sortable_fields
@@ -113,6 +113,10 @@ module Jsonapi
113
113
  resource_klass.filters
114
114
  end
115
115
 
116
+ def mutable?
117
+ resource_klass.mutable?
118
+ end
119
+
116
120
  def columns_with_comment(need_encoding: true)
117
121
  @columns_with_comment ||= {}.tap do |clos|
118
122
  model_klass.columns.each do |col|
@@ -128,7 +128,7 @@
128
128
  },
129
129
  description: tt(:related_ids, model: relation_name_camelize)
130
130
  }
131
- if relation.belongs_to?
131
+ if relation.try(:belongs_to?)
132
132
  relat_props[relation_name][:properties][:data] = {
133
133
  type: :object,
134
134
  properties: {
@@ -277,7 +277,7 @@
277
277
  }
278
278
  }
279
279
 
280
- if resource_klass.mutable?
280
+ if mutable?
281
281
  doc['paths']["/#{route_resouces}"].merge!({
282
282
  post: {
283
283
  summary: "#{route_resouces} #{tt(:create)}",
@@ -173,7 +173,7 @@ RSpec.describe '<%= resouces_name %>', type: :request do
173
173
  end
174
174
  end
175
175
 
176
- <% if resource_klass.mutable? -%>
176
+ <% if mutable? -%>
177
177
  path '/<%= route_resouces %>' do
178
178
  post '<%= route_resouces %> <%= t(:create) %>' do
179
179
  tags '<%= route_resouces %>'
@@ -204,7 +204,7 @@ RSpec.describe '<%= resouces_name %>', type: :request do
204
204
  <%= relation_name %>: {
205
205
  type: :object,
206
206
  properties: {
207
- <% if relation.belongs_to? -%>
207
+ <% if relation.try(:belongs_to?) -%>
208
208
  data: {
209
209
  type: :object,
210
210
  properties: {
@@ -3,6 +3,7 @@
3
3
  require 'jsonapi/swagger/version'
4
4
  require 'jsonapi/swagger/railtie' if defined?(Rails)
5
5
  require 'jsonapi/swagger/json'
6
+ require 'jsonapi/swagger/resource'
6
7
 
7
8
  module Jsonapi
8
9
  module Swagger
@@ -0,0 +1,26 @@
1
+ require 'forwardable'
2
+ module Jsonapi
3
+ module Swagger
4
+ class Resource
5
+ def self.with(model_class_name)
6
+ if Object.const_defined?("#{model_class_name}Resource")
7
+ @resource_class = "#{model_class_name}Resource".safe_constantize
8
+ unless @resource_class < JSONAPI::Resource
9
+ raise Jsonapi::Swagger::Error, "#{@resource_class.class} is not Subclass of JSONAPI::Resource!"
10
+ end
11
+ require 'jsonapi/swagger/resources/jsonapi_resource'
12
+ return Jsonapi::Swagger::JsonapiResource.new(@resource_class)
13
+ elsif Object.const_defined?("Serializable#{model_class_name}")
14
+ @resource_class = "Serializable#{model_class_name}".safe_constantize
15
+ unless @resource_class < JSONAPI::Serializable::Resource
16
+ raise Jsonapi::Swagger::Error, "#{@resource_class.class} is not Subclass of JSONAPI::Serializable::Resource!"
17
+ end
18
+ require 'jsonapi/swagger/resources/serializable_resource'
19
+ return Jsonapi::Swagger::SerializableResource.new(@resource_class)
20
+ else
21
+ raise Jsonapi::Swagger::Error, "#{model_class_name} not support!"
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,18 @@
1
+ require 'forwardable'
2
+ module Jsonapi
3
+ module Swagger
4
+ class JsonapiResource
5
+ extend Forwardable
6
+
7
+ def_delegators :@jr, :_attributes, :_relationships, :sortable_fields,
8
+ :creatable_fields, :updatable_fields, :filters, :mutable?
9
+
10
+ def initialize(jr)
11
+ @jr = jr
12
+ end
13
+
14
+ alias attributes _attributes
15
+ alias relationships _relationships
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,45 @@
1
+ require 'forwardable'
2
+ module Jsonapi
3
+ module Swagger
4
+ class SerializableResource
5
+ extend Forwardable
6
+
7
+ def_delegators :@sr, :type_val, :attribute_blocks, :relationship_blocks, :link_blocks
8
+
9
+ def initialize(sr)
10
+ @sr = sr
11
+ end
12
+
13
+ alias attributes attribute_blocks
14
+
15
+ def relationships
16
+ {}.tap do |relations|
17
+ relationship_blocks.each do |rel, block|
18
+ relations[rel] = OpenStruct.new(class_name: rel.to_s)
19
+ end
20
+ end
21
+ end
22
+
23
+ # TODO: from jsonapi serializable resource
24
+ def sortable_fields
25
+ []
26
+ end
27
+
28
+ def creatable_fields
29
+ []
30
+ end
31
+
32
+ def updatable_fields
33
+ []
34
+ end
35
+
36
+ def filters
37
+ []
38
+ end
39
+
40
+ def mutable?
41
+ false
42
+ end
43
+ end
44
+ end
45
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Jsonapi
4
4
  module Swagger
5
- VERSION = '0.6.0'
5
+ VERSION = '0.7.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-swagger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - YingRui Lu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-19 00:00:00.000000000 Z
11
+ date: 2019-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -83,6 +83,9 @@ files:
83
83
  - lib/jsonapi/swagger.rb
84
84
  - lib/jsonapi/swagger/json.rb
85
85
  - lib/jsonapi/swagger/railtie.rb
86
+ - lib/jsonapi/swagger/resource.rb
87
+ - lib/jsonapi/swagger/resources/jsonapi_resource.rb
88
+ - lib/jsonapi/swagger/resources/serializable_resource.rb
86
89
  - lib/jsonapi/swagger/version.rb
87
90
  homepage: https://github.com/superiorlu/jsonapi-swagger
88
91
  licenses: