schemable 0.1.4 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
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 factory and the model's attributes. It is also highly customizable, allowing you to modify the schema to your liking by overriding the default methods.
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
 
@@ -12,25 +14,24 @@ gem 'schemable'
12
14
 
13
15
  And then execute:
14
16
 
15
- $ bundle install
17
+ bundle install
16
18
 
17
19
  Or install it yourself as:
18
20
 
19
- $ gem install schemable
21
+ gem install schemable
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 add the following files to your Rails application:
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
- To generate these files, run the following command:
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
+
34
35
  ### Generating Definition Files
35
36
 
36
37
  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 +42,317 @@ rails g schemable:model --model_name <model_name>
41
42
 
42
43
  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
44
 
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>
45
+ ### Configuration
46
+
47
+ 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.
48
+
49
+ 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.
50
+
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
+ ### Customizing the Schema
71
+
72
+ 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.
73
+
74
+ Please read the method inline documentation before overriding to avoid any unexpected behavior.
75
+
76
+ 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.)
77
+
78
+ ---
79
+
80
+ | Method Name | Description |
81
+ | -------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
82
+ | `serializer` | Returns the serializer of the model for the definition. |
83
+ | `attributes` | Returns the attributes for the definition based on the configuration. |
84
+ | `relationships` | Returns the relationships defined in the model. |
85
+ | `array_types` | Returns a hash of all the arrays defined for the model. |
86
+ | `optional_create_request_attributes` | Returns the attributes that are not required in the create request. |
87
+ | `optional_update_request_attributes` | Returns the attributes that are not required in the update request. |
88
+ | `nullable_attributes` | Returns the attributes that are nullable in the request/response body. |
89
+ | `additional_create_request_attributes` | Returns the additional create request attributes that are not automatically generated. |
90
+ | `additional_update_request_attributes` | Returns the additional update request attributes that are not automatically generated. |
91
+ | `additional_response_attributes` | Returns the additional response attributes that are not automatically generated. |
92
+ | `additional_response_relations` | Returns the additional response relations that are not automatically generated. |
93
+ | `additional_response_included` | Returns the additional response included that are not automatically generated. |
94
+ | `excluded_create_request_attributes` | Returns the attributes that are excluded from the create request schema. |
95
+ | `excluded_update_request_attributes` | Returns the attributes that are excluded from the update request schema. |
96
+ | `default_value_for_enum_attributes` | Returns the default value for the enum attributes. Used when you want a custom value for the default enum. By default the first key is used as default |
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
+
108
+ ## Examples
109
+
110
+ 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.
111
+
112
+ ### Annex 1.0 - Add custom type mapper
78
113
 
79
114
  ```ruby
80
- module Swagger
81
- module Definitions
82
- class UserApplication
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
83
130
 
84
- include Schemable
85
- include SerializersHelper
131
+ Schemable.configure do |config|
132
+ config.use_serialized_instance = true
133
+ end
134
+ ```
135
+
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.
137
+
138
+ ```ruby
139
+ # lib/swagger/definitions/user_application.rb
86
140
 
141
+ module Swagger
142
+ module Definitions
143
+ class User < Schemable::Definition
87
144
  attr_accessor :instance
88
145
 
89
- def initialize
90
- @instance ||= JSONAPI::Serializable::Renderer.new.render(FactoryBot.create(:user, :with_user_application_applicants), class: serializers_map, include: [])
146
+ def serializer
147
+ V1::UserSerializer
91
148
  end
92
149
 
93
- def serializer
94
- V1::UserApplicationSerializer
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
+
201
+ 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.
202
+
203
+ Now, we need to specify theses options in the configuration file:
204
+
205
+ ```ruby
206
+ # config/initializers/schemable.rb
207
+
208
+ Schemable.configure do |config|
209
+ config.custom_defined_enum_method = :custom_defined_enum
210
+ config.enum_suffix_for_simple_enum = '_cd'
211
+ end
212
+ ```
213
+
214
+ This will generate the schema for the enum fields in the model as follows:
215
+
216
+ ```ruby
217
+ {
218
+ # ...
219
+ status: {
220
+ type: string,
221
+ enum: ['active', 'inactive']
222
+ }
223
+ # ...
224
+ }
225
+ ```
226
+
227
+ ### Annex 1.3 - Infer attributes from custom method
228
+
229
+ 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:
96
230
 
231
+ ```ruby
232
+ # app/models/user.rb
233
+
234
+ class User < ApplicationModel
235
+ def self.base_attributes
236
+ %i[
237
+ id
238
+ name
239
+ email
240
+ status
241
+ roles
242
+ created_at
243
+ updated_at
244
+ ]
245
+ end
246
+ end
247
+ ```
248
+
249
+ Then in the configuration file, we can override the `infer_attributes_from_custom_method` method to return the attributes from the custom method:
250
+
251
+ ```ruby
252
+ # config/initializers/schemable.rb
253
+
254
+ Schemable.configure do |config|
255
+ config.infer_attributes_from_custom_method = :base_attributes
256
+ end
257
+ ```
258
+
259
+ 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:
260
+
261
+ ```ruby
262
+ # lib/swagger/definitions/user.rb
263
+
264
+ module Swagger
265
+ module Definitions
266
+ class User < Schemable::Definition
267
+ def attributes
268
+ model.base_attributes
269
+ end
270
+ end
271
+ end
272
+ end
273
+ ```
274
+
275
+ ### Annex 1.4 - Custom meta response schema
276
+
277
+ 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:
278
+
279
+ ```ruby
280
+ # config/initializers/schemable.rb
281
+
282
+ Schemable.configure do |config|
283
+ config.custom_meta_response_schema = {
284
+ type: :object,
285
+ properties: {
286
+ total: { type: :integer }
287
+ }
288
+ }
289
+ end
290
+ ```
291
+
292
+ ### Annex 1.5 - Highly Customized Definition
293
+
294
+ 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.
295
+
296
+ <Summary>
297
+ <Details>
298
+ <Summary>Click to expand</Summary>
299
+
300
+ ```ruby
301
+ module Swagger
302
+ module Definitions
303
+ class Order < Schemable::Definition
97
304
  def relationships
98
- {
305
+ @relationships ||= {
99
306
  belongs_to: {
100
- category: Swagger::Definitions::Category,
307
+ address: Swagger::Definitions::Address.new
101
308
  },
102
309
  has_many: {
103
- applicants: Swagger::Definitions::Applicant,
310
+ items: Swagger::Definitions::Item.new
311
+ },
312
+ addition_to_included: {
313
+ store: Swagger::Definitions::Store.new,
314
+ attachments: Swagger::Definitions::Upload.new
104
315
  }
105
316
  }
106
317
  end
107
318
 
108
- def array_types
109
- {
110
- applicant_ids:
319
+ def excluded_create_request_attributes
320
+ create_params = model.create_params.select { |item| item.is_a?(Symbol) }
321
+ model.base_attributes + %i[comment applicable_transitions] - create_params
322
+ end
323
+
324
+ def excluded_update_request_attributes
325
+ update_params = model.update_params.select { |item| item.is_a?(Symbol) }
326
+ model.base_attributes + %i[comment applicable_transitions] - update_params
327
+ end
328
+
329
+ def additional_create_request_attributes
330
+ @additional_create_request_attributes ||= {
331
+ items_attributes:
111
332
  {
112
333
  type: :array,
113
- items:
114
- {
115
- type: :string
116
- },
117
- nullable: true
334
+ items: {
335
+ anyOf: [
336
+ {
337
+ type: :object,
338
+ properties: Schemable::RequestSchemaGenerator.new(Swagger::Definitions::Item.new).generate_for_create.as_json['properties']['data']['properties']
339
+ }
340
+ ]
341
+ }
118
342
  }
119
343
  }
120
344
  end
121
345
 
122
- def excluded_request_attributes
123
- %i[id updated_at created_at applicant_ids comment]
124
- end
125
-
126
- def additional_request_attributes
127
- {
128
- applicants_attributes:
346
+ def additional_update_request_attributes
347
+ @additional_update_request_attributes ||={
348
+ items_attributes:
129
349
  {
130
350
  type: :array,
131
351
  items: {
132
352
  anyOf: [
133
353
  {
134
354
  type: :object,
135
- properties: Swagger::Definitions::Applicant.new.request_schema.as_json['properties']['data']['properties']
355
+ properties:Schemable::RequestSchemaGenerator.new(Swagger::Definitions::Item.new).generate_for_update.as_json['properties']['data']['properties']
136
356
  }
137
357
  ]
138
358
  }
@@ -142,54 +362,60 @@ module Swagger
142
362
 
143
363
  def additional_response_attributes
144
364
  {
145
- comment: { type: :object, properties: {}, nullable: true }
365
+ comment: { type: :object, properties: {}, nullable: true },
366
+ item_status: { type: :string, nullable: true },
367
+ applicable_transitions: {
368
+ type: :array,
369
+ items:
370
+ {
371
+ type: :object, nullable: true,
372
+ properties:
373
+ {
374
+ name: { type: :string, nullable: true },
375
+ metadata: { type: :object, nullable: true }
376
+ }
377
+ },
378
+ nullable: true
379
+ }
146
380
  }
147
381
  end
148
382
 
149
- def nested_relationships
150
- {
151
- applicants: {
152
- belongs_to: {
153
- district: Swagger::Definitions::District,
154
- province: Swagger::Definitions::Province,
155
- },
156
- has_many: {
157
- attachments: Swagger::Definitions::Upload,
158
- }
159
- }
160
- }
383
+ def nullable_attributes
384
+ %i[contact_number email]
385
+ end
386
+
387
+ def optional_create_request_attributes
388
+ %i[contact_number email]
389
+ end
390
+
391
+ def optional_update_request_attributes
392
+ %i[contact_number email]
161
393
  end
162
394
 
163
- def self.definitions
164
- schema_instance = self.new
395
+ def self.generate
396
+ schema_instance = new
397
+
165
398
  [
166
- "#{schema_instance.model}Request": schema_instance.camelize_keys(schema_instance.request_schema),
167
- "#{schema_instance.model}Response": schema_instance.camelize_keys(schema_instance.response_schema(expand: true, exclude_from_expansion: [:category], multi: true)),
168
- "#{schema_instance.model}ResponseExpanded": schema_instance.camelize_keys(schema_instance.response_schema(expand: true, nested: true))
399
+ "#{schema_instance.model}CreateRequest": schema_instance.camelize_keys(Schemable::RequestSchemaGenerator.new(schema_instance).generate_for_create),
400
+ "#{schema_instance.model}UpdateRequest": schema_instance.camelize_keys(Schemable::RequestSchemaGenerator.new(schema_instance).generate_for_update),
401
+ "#{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])),
402
+ "#{schema_instance.model}ResponseExpanded": schema_instance.camelize_keys(Schemable::ResponseSchemaGenerator.new(schema_instance).generate(expand: true))
169
403
  ]
170
404
  end
171
405
  end
172
406
  end
173
407
  end
174
-
175
408
  ```
176
409
 
177
- </Details>
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).
410
+ </Details>
411
+ </Summary>
184
412
 
185
413
  ## Contributing
186
414
 
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 are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/schemable/blob/master/CODE_OF_CONDUCT.md).
415
+ 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.
416
+
417
+ 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
418
 
189
419
  ## License
190
420
 
191
421
  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 = 'spec/swagger/common_definitions.rb'
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', 'Serializers helper already exists')
13
+ say_status('skipped', 'Schemable initializer already exists')
24
14
  else
25
- copy_file('serializers_helper.rb', target_path)
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