schemable 0.1.3 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +5 -1
- data/Gemfile +5 -5
- data/Gemfile.lock +82 -50
- data/README.md +324 -100
- data/lib/generators/schemable/install_generator.rb +3 -13
- data/lib/generators/schemable/model_generator.rb +1 -16
- data/lib/schemable/attribute_schema_generator.rb +130 -0
- data/lib/schemable/configuration.rb +114 -0
- data/lib/schemable/definition.rb +322 -0
- data/lib/schemable/included_schema_generator.rb +107 -0
- data/lib/schemable/relationship_schema_generator.rb +119 -0
- data/lib/schemable/request_schema_generator.rb +88 -0
- data/lib/schemable/response_schema_generator.rb +124 -0
- data/lib/schemable/schema_modifier.rb +248 -0
- data/lib/schemable/version.rb +1 -1
- data/lib/schemable.rb +60 -927
- data/lib/templates/schemable.rb +76 -0
- data/schemable.gemspec +2 -5
- data/sig/schemable/attribute_schema_generator.rbs +13 -0
- data/sig/schemable/configuration.rbs +21 -0
- data/sig/schemable/definition.rbs +32 -0
- data/sig/schemable/included_schema_generator.rbs +11 -0
- data/sig/schemable/relationship_schema_generator.rbs +11 -0
- data/sig/schemable/request_schema_generator.rbs +10 -0
- data/sig/schemable/response_schema_generator.rbs +13 -0
- data/sig/schemable/schema_modifier.rbs +9 -0
- data/sig/schemable.rbs +4 -1
- metadata +23 -36
- data/lib/templates/common_definitions.rb +0 -13
- data/lib/templates/serializers_helper.rb +0 -7
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# Schemable
|
2
2
|
|
3
|
-
The Schemable gem provides a simple way to define a schema for a Rails model in [JSONAPI](https://jsonapi.org/) format. It can automatically generate a schema for a model based on the model's
|
3
|
+
The Schemable gem provides a simple way to define a schema for a Rails model in [JSONAPI](https://jsonapi.org/) format. It can automatically generate a schema for a model based on the model's attributes. It is also highly customizable, allowing you to modify the schema to your liking by overriding configuration options and methods.
|
4
|
+
|
5
|
+
This gem is preferably to be used with [RSwag](https://github.com/rswag/rswag) gem to generate the swagger documentation for your API.
|
4
6
|
|
5
7
|
## Installation
|
6
8
|
|
@@ -20,17 +22,17 @@ Or install it yourself as:
|
|
20
22
|
|
21
23
|
## Usage
|
22
24
|
|
23
|
-
The installation command above will install the Schemable gem and its dependencies. However, in order for Schemable to work, you must also
|
24
|
-
|
25
|
-
- `app/helpers/serializers_helper.rb` - This file contains the `serializers_map` helper method, which is used to map a model to its serializer.
|
26
|
-
- `spec/swagger/common_definitions.rb` - This file contains the `aggregate` method, which is used to aggregate the schemas of all the models in your application into a single place. This file is recommeded, but not required. If you do not use this file, you will need to manually aggregate the schemas of all the models in your application into a single place.
|
25
|
+
The installation command above will install the Schemable gem and its dependencies. However, in order for Schemable to work, you must also implement your own logic to use the generated schemas to feed it to RSwag.
|
27
26
|
|
28
|
-
|
27
|
+
The below command is to initialize the gem and generate the configuration file.
|
29
28
|
|
30
29
|
```ruby
|
31
30
|
rails g schemable:install
|
32
31
|
```
|
33
32
|
|
33
|
+
This will generate `schemable.rb` in your `config/initializers` directory. This file will contain the configuration for the Schemable gem. You can modify the configuration to your liking. For more information on the configuration options, see the [Configuration](#configuration) section below.
|
34
|
+
|
35
|
+
|
34
36
|
### Generating Definition Files
|
35
37
|
|
36
38
|
The Schemable gem provides a generator that can be used to generate definition files for your models. To generate a definition file for a model, run the following command:
|
@@ -41,98 +43,315 @@ rails g schemable:model --model_name <model_name>
|
|
41
43
|
|
42
44
|
This will generate a definition file for the specified model in the `lib/swagger/definitions` directory. The definition file will be named `<model_name>.rb`. This file will have the bare minimum code required to generate a schema for the model. You can then modify the definition file to your liking by overriding the default methods. For example, you can add or remove attributes from the schema, or you can add or remove relationships from the schema. You can also add custom attributes to the schema. For more information on how to customize the schema, see the [Customizing the Schema](#customizing-the-schema) section below.
|
43
45
|
|
44
|
-
## Customizing the Schema
|
45
|
-
|
46
|
-
The Schemable gem provides a number of methods that can be used to customize the schema. These methods are defined in the `Schemable` module of the gem. To customize the schema, simply override the default methods in the definition file for the model. The following is a list of the methods that can be overridden:
|
47
|
-
|
48
|
-
| WARNING: please read the method inline documentation before overriding to avoid any unexpected behavior. |
|
49
|
-
| -------------------------------------------------------------------------------------------------------- |
|
50
|
-
|
51
|
-
The list of methods that can be overridden are as follows:
|
52
|
-
|
53
|
-
| Method Name | Description |
|
54
|
-
| -------------------------------- | ---------------------------------------------------------------------------------------------------------- |
|
55
|
-
| `serializer` | Returns the serializer class. |
|
56
|
-
| `attributes` | Returns the attributes that are auto generated from the model. |
|
57
|
-
| `relationships` | Returns the relationships in the format of { belongs_to: {}, has_many: {} }. |
|
58
|
-
| `array_type` | Returns the type of arrays in the model that needs to be manually defined. |
|
59
|
-
| `optional_request_attributes` | Returns the attributes that are optional in the request schema. |
|
60
|
-
| `nullable_attributes` | Returns the attributes that are nullable in the request/response schema. |
|
61
|
-
| `additional_request_attributes` | Returns the attributes that are additional in the request schema. |
|
62
|
-
| `additional_response_attributes` | Returns the attributes that are additional in the response schema. |
|
63
|
-
| `additional_response_relations` | Returns the relationships that are additional in the response schema (Appended to relationships). |
|
64
|
-
| `additional_response_included` | Returns the included that are additional in the response schema (Appended to included). |
|
65
|
-
| `excluded_request_attributes` | Returns the attributes that are excluded from the request schema. |
|
66
|
-
| `excluded_response_attributes` | Returns the attributes that are excluded from the response schema. |
|
67
|
-
| `excluded_response_relations` | Returns the relationships that are excluded from the response schema. |
|
68
|
-
| `excluded_response_included` | (not implemented yet) Returns the included that are excluded from the response schema. |
|
69
|
-
| `nested_relationships` | Returns the relationships to be further expanded in the response schema. |
|
70
|
-
| `model` | Returns the model class (Constantized from the definition class name). |
|
71
|
-
| `model_name` | Returns the model name. Used for schema type naming. |
|
72
|
-
| `definitions` | Returns the generated schemas in JSONAPI format (It is recommended to override this method to your liking) |
|
73
|
-
|
74
|
-
The following is an example of a definition file for a model that has been customized:
|
75
|
-
|
76
|
-
<Details>
|
77
|
-
<Summary>Click to view the example</Summary>
|
78
46
|
|
47
|
+
### Configuration
|
48
|
+
The Schemable gem provides a number of configuration options that can be used to customize the behavior of the gem. The following is a list of the configuration options that are available.
|
49
|
+
|
50
|
+
Please note that the configurations options below are defined in the `Schemable` module of the gem. To configure the gem, simply override the default values in the `config/initializers/schemable.rb` file. Also the changes will affect all the definition classes globally.
|
51
|
+
|
52
|
+
---
|
53
|
+
| Option Name | Description | Default Value |
|
54
|
+
| -------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------- |
|
55
|
+
| `orm` | The ORM that is used in the application. The options are `:active_record` and `:mongoid`. | `true` |
|
56
|
+
| `float_as_string` | Whether or not to convert the `float` type to a `string` type in the schema. | `false` |
|
57
|
+
| `decimal_as_string` | Whether or not to convert the `decimal` type to a `string` type in the schema. | `false` |
|
58
|
+
| `custom_type_mappers` | A hash of custom type mappers that can be used to override the default type mappers. A specific method should be used, see [Annex 1.0 - Add custom type mapper](#annex-10---add-custom-type-mapper) for more information. | `{}` |
|
59
|
+
| `use_serialized_instance` | Whether or not to use the serialized instance in the process of schema generation as type fallback for virtual attributes. See [Annex 1.1 - Use serialized instance](#annex-11---use-serialized-instance) for more information. | `false` |
|
60
|
+
| `custom_defined_enum_method` | The name of the method that is used to get the enum keys and values. This allows applications with the orm `mongoid` define a method that mimicks what `defined_enums` does in `activerecord`. Please see [Annex 1.2 - Custom defined enum method](#annex-12---custom-defined-enum-method) for an example. | `nil` |
|
61
|
+
| `enum_prefix_for_simple_enum` | The prefix to be used for the enum values when `mongoid` is used. | `nil` |
|
62
|
+
| `enum_suffix_for_simple_enum` | The suffix to be used for the enum values when `mongoid` is used. | `nil` |
|
63
|
+
| `infer_attributes_from_custom_method` | The name of the custom method that is used to get the attributes to be generated in the schema. See [Annex 1.3 - Infer attributes from custom method](#annex-13---infer-attributes-from-custom-method) for more information. | `nil` |
|
64
|
+
| `infer_attributes_from_jsonapi_serializable` | Whether or not to infer the attributes from the `JSONAPI::Serializable::Resource` class. See the previous example [Annex 1.1 - Use serialized instance](#annex-11---use-serialized-instance) for more information. | `false` |
|
65
|
+
| `custom_meta_response_schema` | A hash of custom meta response schema that can be used to override the default meta response schema. See [Annex 1.4 - Custom meta response schema](#annex-14---custom-meta-response-schema) for more information. | `nil` |
|
66
|
+
| `pagination_enabled` | Enable pagination schema generation in the `meta` section of the response schema. | `true` |
|
67
|
+
|
68
|
+
|
69
|
+
---
|
70
|
+
|
71
|
+
### Customizing the Schema
|
72
|
+
|
73
|
+
The Schemable gem provides a number of methods that can be used to customize the schema. These methods are defined in the `Schemable::Definition` class of the gem. To customize the schema for a specific model, simply override the default methods in the `Schemable::Definition` class for the model.
|
74
|
+
|
75
|
+
Please read the method inline documentation before overriding to avoid any unexpected behavior.
|
76
|
+
|
77
|
+
The following is a list of the methods that can be overridden. (See the example in [Annex 1.5 - Highly Customized Definition](#annex-15---highly-customized-definition) for a highly customized definition file.)
|
78
|
+
|
79
|
+
----
|
80
|
+
|
81
|
+
| Method Name | Description |
|
82
|
+
|---------------------------------------|------------------------------------------------------------------------------------------------------------|
|
83
|
+
| `serializer` | Returns the serializer of the model for the definition. |
|
84
|
+
| `attributes` | Returns the attributes for the definition based on the configuration. |
|
85
|
+
| `relationships` | Returns the relationships defined in the model. |
|
86
|
+
| `array_types` | Returns a hash of all the arrays defined for the model. |
|
87
|
+
| `optional_create_request_attributes` | Returns the attributes that are not required in the create request. |
|
88
|
+
| `optional_update_request_attributes` | Returns the attributes that are not required in the update request. |
|
89
|
+
| `nullable_attributes` | Returns the attributes that are nullable in the request/response body. |
|
90
|
+
| `additional_create_request_attributes`| Returns the additional create request attributes that are not automatically generated. |
|
91
|
+
| `additional_update_request_attributes`| Returns the additional update request attributes that are not automatically generated. |
|
92
|
+
| `additional_response_attributes` | Returns the additional response attributes that are not automatically generated. |
|
93
|
+
| `additional_response_relations` | Returns the additional response relations that are not automatically generated. |
|
94
|
+
| `additional_response_included` | Returns the additional response included that are not automatically generated. |
|
95
|
+
| `excluded_create_request_attributes` | Returns the attributes that are excluded from the create request schema. |
|
96
|
+
| `excluded_update_request_attributes` | Returns the attributes that are excluded from the update request schema. |
|
97
|
+
| `excluded_response_attributes` | Returns the attributes that are excluded from the response schema. |
|
98
|
+
| `excluded_response_relations` | Returns the relationships that are excluded from the response schema. |
|
99
|
+
| `excluded_response_included` | Returns the included that are excluded from the response schema. |
|
100
|
+
| `serialized_instance` | Returns an instance of the model class that is already serialized into jsonapi format. |
|
101
|
+
| `model` | Returns the model class (Constantized from the definition class name). |
|
102
|
+
| `model_name` | Returns the model name. Used for schema type naming. |
|
103
|
+
| `camelize_keys` | Given a hash, it returns a new hash with all the keys camelized. |
|
104
|
+
| `generate` | Returns the schema for the create request body, update request body, and response body. |
|
105
|
+
----
|
106
|
+
|
107
|
+
## Examples
|
108
|
+
|
109
|
+
The followings are some examples of configuration of the gem to have different behaviors based on the application needs. In the above section, we have already seen how to generate the definition files for the models. The following examples will show how to customize the schema for the models. Also, we will see how to use the generated schema in RSwag to generate the swagger documentation for the API.
|
110
|
+
|
111
|
+
|
112
|
+
### Annex 1.0 - Add custom type mapper
|
113
|
+
|
79
114
|
```ruby
|
80
|
-
|
81
|
-
|
82
|
-
|
115
|
+
# config/initializers/schemable.rb
|
116
|
+
|
117
|
+
Schemable.configure do |config|
|
118
|
+
config.add_custom_type_mapper(
|
119
|
+
:string,
|
120
|
+
{ type: :text }
|
121
|
+
)
|
122
|
+
end
|
123
|
+
|
124
|
+
```
|
125
|
+
|
126
|
+
### Annex 1.1 - Use serialized instance
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
# config/initializers/schemable.rb
|
130
|
+
|
131
|
+
Schemable.configure do |config|
|
132
|
+
config.use_serialized_instance = true
|
133
|
+
end
|
134
|
+
```
|
83
135
|
|
84
|
-
|
85
|
-
include SerializersHelper
|
136
|
+
Then in the definition file, you can override the `serialized_instance` method to return the serialized instance of the model. Let's also assume that we want to use the `JSONAPI::Serializable::Resource` class to serialize the instance.
|
86
137
|
|
138
|
+
```ruby
|
139
|
+
# lib/swagger/definitions/user_application.rb
|
140
|
+
|
141
|
+
module Swagger
|
142
|
+
module Definitions
|
143
|
+
class User < Schemable::Definition
|
87
144
|
attr_accessor :instance
|
88
145
|
|
89
|
-
def
|
90
|
-
|
146
|
+
def serializer
|
147
|
+
V1::UserSerializer
|
91
148
|
end
|
92
149
|
|
93
|
-
def
|
94
|
-
|
150
|
+
def serialized_instance
|
151
|
+
@instance ||= JSONAPI::Serializable::Renderer.new.render(
|
152
|
+
FactoryBot.create(:user),
|
153
|
+
class: { User: serializer },
|
154
|
+
include: []
|
155
|
+
)
|
95
156
|
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
```
|
161
|
+
|
162
|
+
### Annex 1.2 - Custom defined enum method
|
163
|
+
|
164
|
+
Let's assume that we also want to use mongoid in our application. In this case, we need to define a method that mimicks what `defined_enums` does in `activerecord`. Let's assume that we have a model called `User` that has an enum field called `status`. The following is an example of how to define the method:
|
165
|
+
|
166
|
+
```ruby
|
167
|
+
# app/models/user.rb
|
168
|
+
|
169
|
+
class User < ApplicationModel
|
170
|
+
include Mongoid::Document
|
171
|
+
include SimpleEnum::Mongoid
|
172
|
+
|
173
|
+
as_enum :status, active: 0, inactive: 1
|
174
|
+
end
|
175
|
+
```
|
176
|
+
|
177
|
+
Then in the `ApplicationModel` class, we can define the method as follows:
|
178
|
+
|
179
|
+
```ruby
|
180
|
+
# app/models/application_model.rb
|
181
|
+
|
182
|
+
def self.custom_defined_enum(suffix: '_cd', prefix: nil)
|
183
|
+
defined_enums = {}
|
184
|
+
enum_fields = if prefix
|
185
|
+
fields.select { |k, v| k.to_s.start_with?(prefix) }
|
186
|
+
else
|
187
|
+
fields.select { |k, v| k.to_s.end_with?(suffix) }
|
188
|
+
end
|
189
|
+
|
190
|
+
enum_fields.each do |k, v|
|
191
|
+
enum_name = k.to_s.gsub(prefix || suffix, '')
|
192
|
+
enum = send(enum_name.pluralize)
|
193
|
+
|
194
|
+
defined_enums[enum_name] = enum.hash
|
195
|
+
end
|
196
|
+
|
197
|
+
defined_enums
|
198
|
+
end
|
199
|
+
```
|
200
|
+
This method will work for all the models that have enum fields. Since Simple Enum gem defines enum fields with the suffix `_cd`, we can use the `suffix` option to get the enum fields. However, if the enum fields are defined with a different suffix, we can use the `prefix` option to get the enum fields.
|
201
|
+
|
202
|
+
Now, we need to specify theses options in the configuration file:
|
203
|
+
|
204
|
+
```ruby
|
205
|
+
# config/initializers/schemable.rb
|
206
|
+
|
207
|
+
Schemable.configure do |config|
|
208
|
+
config.custom_defined_enum_method = :custom_defined_enum
|
209
|
+
config.enum_suffix_for_simple_enum = '_cd'
|
210
|
+
end
|
211
|
+
```
|
212
|
+
|
213
|
+
This will generate the schema for the enum fields in the model as follows:
|
214
|
+
|
215
|
+
```ruby
|
216
|
+
{
|
217
|
+
# ...
|
218
|
+
status: {
|
219
|
+
type: string,
|
220
|
+
enum: ['active', 'inactive']
|
221
|
+
}
|
222
|
+
# ...
|
223
|
+
}
|
224
|
+
```
|
225
|
+
|
226
|
+
### Annex 1.3 - Infer attributes from custom method
|
227
|
+
|
228
|
+
Sometimes, we may want to infer the attributes from a custom method. For example, let's assume that we have a model called `User` that has a method called `base_attributes` that returns an array of attributes. The following is an example of how to define the method:
|
229
|
+
|
230
|
+
```ruby
|
231
|
+
# app/models/user.rb
|
232
|
+
|
233
|
+
class User < ApplicationModel
|
234
|
+
def self.base_attributes
|
235
|
+
%i[
|
236
|
+
id
|
237
|
+
name
|
238
|
+
email
|
239
|
+
status
|
240
|
+
roles
|
241
|
+
created_at
|
242
|
+
updated_at
|
243
|
+
]
|
244
|
+
end
|
245
|
+
end
|
246
|
+
```
|
247
|
+
|
248
|
+
Then in the configuration file, we can override the `infer_attributes_from_custom_method` method to return the attributes from the custom method:
|
249
|
+
|
250
|
+
```ruby
|
251
|
+
# config/initializers/schemable.rb
|
96
252
|
|
253
|
+
Schemable.configure do |config|
|
254
|
+
config.infer_attributes_from_custom_method = :base_attributes
|
255
|
+
end
|
256
|
+
```
|
257
|
+
|
258
|
+
if we want to use the `base_attributes` method for only the User model, we can override the `attributes` method in the `Schemable::Definition` class as follows:
|
259
|
+
|
260
|
+
```ruby
|
261
|
+
# lib/swagger/definitions/user.rb
|
262
|
+
|
263
|
+
module Swagger
|
264
|
+
module Definitions
|
265
|
+
class User < Schemable::Definition
|
266
|
+
def attributes
|
267
|
+
model.base_attributes
|
268
|
+
end
|
269
|
+
end
|
270
|
+
end
|
271
|
+
end
|
272
|
+
```
|
273
|
+
|
274
|
+
### Annex 1.4 - Custom meta response schema
|
275
|
+
|
276
|
+
Sometimes, we may want to customize the meta response schema. For example, let's assume that we want to add a `total` attribute to the meta response schema. The following is an example of how to do that:
|
277
|
+
|
278
|
+
```ruby
|
279
|
+
# config/initializers/schemable.rb
|
280
|
+
|
281
|
+
Schemable.configure do |config|
|
282
|
+
config.custom_meta_response_schema = {
|
283
|
+
type: :object,
|
284
|
+
properties: {
|
285
|
+
total: { type: :integer }
|
286
|
+
}
|
287
|
+
}
|
288
|
+
end
|
289
|
+
```
|
290
|
+
|
291
|
+
### Annex 1.5 - Highly Customized Definition
|
292
|
+
|
293
|
+
The below is a definition file for a model that has been customized in a way that many of the methods have been overridden. This is just an example of how to customize the schema. You can customize the schema to your liking. The below hypothetical model's logic does not matter. The only thing that matters is the schema customization and being familiar with the methods that can be overridden.
|
294
|
+
|
295
|
+
<Summary>
|
296
|
+
<Details>
|
297
|
+
<Summary>Click to expand</Summary>
|
298
|
+
|
299
|
+
```ruby
|
300
|
+
module Swagger
|
301
|
+
module Definitions
|
302
|
+
class Order < Schemable::Definition
|
97
303
|
def relationships
|
98
|
-
{
|
304
|
+
@relationships ||= {
|
99
305
|
belongs_to: {
|
100
|
-
|
306
|
+
address: Swagger::Definitions::Address.new
|
101
307
|
},
|
102
308
|
has_many: {
|
103
|
-
|
309
|
+
items: Swagger::Definitions::Item.new
|
310
|
+
},
|
311
|
+
addition_to_included: {
|
312
|
+
store: Swagger::Definitions::Store.new,
|
313
|
+
attachments: Swagger::Definitions::Upload.new
|
104
314
|
}
|
105
315
|
}
|
106
316
|
end
|
107
317
|
|
108
|
-
def
|
109
|
-
{
|
110
|
-
|
318
|
+
def excluded_create_request_attributes
|
319
|
+
create_params = model.create_params.select { |item| item.is_a?(Symbol) }
|
320
|
+
model.base_attributes + %i[comment applicable_transitions] - create_params
|
321
|
+
end
|
322
|
+
|
323
|
+
def excluded_update_request_attributes
|
324
|
+
update_params = model.update_params.select { |item| item.is_a?(Symbol) }
|
325
|
+
model.base_attributes + %i[comment applicable_transitions] - update_params
|
326
|
+
end
|
327
|
+
|
328
|
+
def additional_create_request_attributes
|
329
|
+
@additional_create_request_attributes ||= {
|
330
|
+
items_attributes:
|
111
331
|
{
|
112
332
|
type: :array,
|
113
|
-
items:
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
333
|
+
items: {
|
334
|
+
anyOf: [
|
335
|
+
{
|
336
|
+
type: :object,
|
337
|
+
properties: Schemable::RequestSchemaGenerator.new(Swagger::Definitions::Item.new).generate_for_create.as_json['properties']['data']['properties']
|
338
|
+
}
|
339
|
+
]
|
340
|
+
}
|
118
341
|
}
|
119
342
|
}
|
120
343
|
end
|
121
344
|
|
122
|
-
def
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
def additional_request_attributes
|
127
|
-
{
|
128
|
-
applicants_attributes:
|
345
|
+
def additional_update_request_attributes
|
346
|
+
@additional_update_request_attributes ||={
|
347
|
+
items_attributes:
|
129
348
|
{
|
130
349
|
type: :array,
|
131
350
|
items: {
|
132
351
|
anyOf: [
|
133
352
|
{
|
134
353
|
type: :object,
|
135
|
-
properties:
|
354
|
+
properties:Schemable::RequestSchemaGenerator.new(Swagger::Definitions::Item.new).generate_for_update.as_json['properties']['data']['properties']
|
136
355
|
}
|
137
356
|
]
|
138
357
|
}
|
@@ -142,54 +361,59 @@ module Swagger
|
|
142
361
|
|
143
362
|
def additional_response_attributes
|
144
363
|
{
|
145
|
-
comment: { type: :object, properties: {}, nullable: true }
|
364
|
+
comment: { type: :object, properties: {}, nullable: true },
|
365
|
+
item_status: { type: :string, nullable: true },
|
366
|
+
applicable_transitions: {
|
367
|
+
type: :array,
|
368
|
+
items:
|
369
|
+
{
|
370
|
+
type: :object, nullable: true,
|
371
|
+
properties:
|
372
|
+
{
|
373
|
+
name: { type: :string, nullable: true },
|
374
|
+
metadata: { type: :object, nullable: true }
|
375
|
+
}
|
376
|
+
},
|
377
|
+
nullable: true
|
378
|
+
}
|
146
379
|
}
|
147
380
|
end
|
148
381
|
|
149
|
-
def
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
},
|
156
|
-
has_many: {
|
157
|
-
attachments: Swagger::Definitions::Upload,
|
158
|
-
}
|
159
|
-
}
|
160
|
-
}
|
382
|
+
def nullable_attributes
|
383
|
+
%i[contact_number email]
|
384
|
+
end
|
385
|
+
|
386
|
+
def optional_create_request_attributes
|
387
|
+
%i[contact_number email]
|
161
388
|
end
|
162
389
|
|
163
|
-
def
|
164
|
-
|
390
|
+
def optional_update_request_attributes
|
391
|
+
%i[contact_number email]
|
392
|
+
end
|
393
|
+
|
394
|
+
def self.generate
|
395
|
+
schema_instance = new
|
396
|
+
|
165
397
|
[
|
166
|
-
"#{schema_instance.model}
|
167
|
-
"#{schema_instance.model}
|
168
|
-
"#{schema_instance.model}
|
398
|
+
"#{schema_instance.model}CreateRequest": schema_instance.camelize_keys(Schemable::RequestSchemaGenerator.new(schema_instance).generate_for_create),
|
399
|
+
"#{schema_instance.model}UpdateRequest": schema_instance.camelize_keys(Schemable::RequestSchemaGenerator.new(schema_instance).generate_for_update),
|
400
|
+
"#{schema_instance.model}Response": schema_instance.camelize_keys(Schemable::ResponseSchemaGenerator.new(schema_instance).generate(expand: true, collection: true, relationships_to_exclude_from_expansion: %w[addresses stores attachments])),
|
401
|
+
"#{schema_instance.model}ResponseExpanded": schema_instance.camelize_keys(Schemable::ResponseSchemaGenerator.new(schema_instance).generate(expand: true))
|
169
402
|
]
|
170
403
|
end
|
171
404
|
end
|
172
405
|
end
|
173
406
|
end
|
174
|
-
|
175
407
|
```
|
176
|
-
|
177
|
-
</
|
178
|
-
|
179
|
-
## Development
|
180
|
-
|
181
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
182
|
-
|
183
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
408
|
+
</Details>
|
409
|
+
</Summary>
|
184
410
|
|
185
411
|
## Contributing
|
186
412
|
|
187
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/schemable. This project is intended to be a safe, welcoming space for collaboration, and contributors
|
413
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/schemable. This project is intended to be a safe, welcoming space for collaboration, and contributors. Please go to issues page to report any bugs or feature requests. If you would like to contribute, please fork the repository and submit a pull request.
|
414
|
+
|
415
|
+
To, use the gem locally, clone the repository and run `bundle install` to install dependencies. Then, run `bundle exec rspec` to run the tests.
|
188
416
|
|
189
417
|
## License
|
190
418
|
|
191
419
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
192
|
-
|
193
|
-
## Code of Conduct
|
194
|
-
|
195
|
-
Everyone interacting in the Schemable project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/schemable/blob/master/CODE_OF_CONDUCT.md).
|
@@ -1,28 +1,18 @@
|
|
1
1
|
module Schemable
|
2
2
|
class InstallGenerator < Rails::Generators::Base
|
3
|
-
|
4
3
|
source_root File.expand_path('../../templates', __dir__)
|
5
|
-
class_option :model_name, type: :string, default: 'Model', desc: 'Name of the model'
|
6
4
|
|
7
5
|
def initialize(*args)
|
8
6
|
super(*args)
|
9
7
|
end
|
10
8
|
|
11
9
|
def copy_initializer
|
12
|
-
target_path = '
|
13
|
-
|
14
|
-
if Rails.root.join(target_path).exist?
|
15
|
-
say_status('skipped', 'Common definitions already exists')
|
16
|
-
else
|
17
|
-
copy_file('common_definitions.rb', target_path)
|
18
|
-
end
|
19
|
-
|
20
|
-
target_path = 'app/helpers/serializers_helper.rb'
|
10
|
+
target_path = 'config/initializers/schemable.rb'
|
21
11
|
|
22
12
|
if Rails.root.join(target_path).exist?
|
23
|
-
say_status('skipped', '
|
13
|
+
say_status('skipped', 'Schemable initializer already exists')
|
24
14
|
else
|
25
|
-
copy_file('
|
15
|
+
copy_file('schemable.rb', target_path)
|
26
16
|
end
|
27
17
|
end
|
28
18
|
end
|
@@ -21,21 +21,7 @@ module Schemable
|
|
21
21
|
create_file(target_path, <<-FILE
|
22
22
|
module Swagger
|
23
23
|
module Definitions
|
24
|
-
class #{@model_name.classify}
|
25
|
-
|
26
|
-
include Schemable
|
27
|
-
include SerializersHelper # This is a helper module that contains a method "serializers_map" that maps models to serializers
|
28
|
-
|
29
|
-
attr_accessor :instance
|
30
|
-
|
31
|
-
def initialize
|
32
|
-
@instance ||= JSONAPI::Serializable::Renderer.new.render(FactoryBot.create(:#{@model_name.underscore.downcase.singularize}), class: serializers_map, include: [])
|
33
|
-
end
|
34
|
-
|
35
|
-
def serializer
|
36
|
-
V1::#{@model_name.classify}Serializer
|
37
|
-
end
|
38
|
-
|
24
|
+
class #{@model_name.classify} < Schemable::Definition
|
39
25
|
def excluded_create_request_attributes
|
40
26
|
%i[updated_at created_at]
|
41
27
|
end
|
@@ -48,7 +34,6 @@ module Swagger
|
|
48
34
|
end
|
49
35
|
FILE
|
50
36
|
)
|
51
|
-
|
52
37
|
end
|
53
38
|
end
|
54
39
|
end
|