mirror of
https://github.com/ditkrg/schemable.git
synced 2026-01-22 22:26:41 +00:00
Adds Generators and their dependencies
This commit is contained in:
parent
81f5587486
commit
5c5ee89742
29
lib/generators/schemable/install_generator.rb
Normal file
29
lib/generators/schemable/install_generator.rb
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
module Schemable
|
||||||
|
class InstallGenerator < Rails::Generators::Base
|
||||||
|
|
||||||
|
source_root File.expand_path('../../templates', __dir__)
|
||||||
|
class_option :model_name, type: :string, default: 'Model', desc: 'Name of the model'
|
||||||
|
|
||||||
|
def initialize(*)
|
||||||
|
super(*)
|
||||||
|
end
|
||||||
|
|
||||||
|
def copy_initializer
|
||||||
|
target_path = 'spec/swagger/common_definitions.rb'
|
||||||
|
|
||||||
|
if Rails.root.join(target_path).exist?
|
||||||
|
say_status('skipped', 'Common definitions already exists')
|
||||||
|
else
|
||||||
|
copy_file('common_definitions.rb', target_path)
|
||||||
|
end
|
||||||
|
|
||||||
|
target_path = 'app/helpers/serializers_helper.rb'
|
||||||
|
|
||||||
|
if Rails.root.join(target_path).exist?
|
||||||
|
say_status('skipped', 'Serializers helper already exists')
|
||||||
|
else
|
||||||
|
copy_file('serializers_helper.rb', target_path)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
53
lib/generators/schemable/model_generator.rb
Normal file
53
lib/generators/schemable/model_generator.rb
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
module Schemable
|
||||||
|
class ModelGenerator < Rails::Generators::Base
|
||||||
|
|
||||||
|
source_root File.expand_path('../../templates', __dir__)
|
||||||
|
class_option :model_name, type: :string, default: 'Model', desc: 'Name of the model'
|
||||||
|
|
||||||
|
def initialize(*)
|
||||||
|
super(*)
|
||||||
|
|
||||||
|
@model_name = options[:model_name]
|
||||||
|
@model_name != 'Model' || raise('Model name is required')
|
||||||
|
end
|
||||||
|
|
||||||
|
def copy_initializer
|
||||||
|
target_path = "lib/swagger/definitions/#{@model_name.underscore.downcase.singlurize}.rb"
|
||||||
|
|
||||||
|
if Rails.root.join(target_path).exist?
|
||||||
|
say_status('skipped', 'Model definition already exists')
|
||||||
|
else
|
||||||
|
|
||||||
|
create_file(target_path, <<-FILE
|
||||||
|
require './lib/swagger/concerns/schemable'
|
||||||
|
|
||||||
|
module Swagger
|
||||||
|
module Definitions
|
||||||
|
class #{@model_name.classify}
|
||||||
|
|
||||||
|
include Schemable
|
||||||
|
include SerializersHelper # This is a helper module that contains a method "serializers_map" that maps models to serializers
|
||||||
|
|
||||||
|
attr_accessor :instance
|
||||||
|
|
||||||
|
def initialize
|
||||||
|
@instance ||= JSONAPI::Serializable::Renderer.new.render(FactoryBot.create(:#{@model_name.underscore.downcase.singlurize}), class: serializers_map, include: [])
|
||||||
|
end
|
||||||
|
|
||||||
|
def serializer
|
||||||
|
V1::#{@model_name.classify}Serializer
|
||||||
|
end
|
||||||
|
|
||||||
|
def excluded_request_attributes
|
||||||
|
%i[id updatedAt createdAt]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
FILE
|
||||||
|
)
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
13
lib/templates/common_definitions.rb
Normal file
13
lib/templates/common_definitions.rb
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
module SwaggerDefinitions
|
||||||
|
module CommonDefinitions
|
||||||
|
def self.aggregate
|
||||||
|
[
|
||||||
|
# Import definitions like this:
|
||||||
|
# Swagger::Definitions::Model.definitions
|
||||||
|
|
||||||
|
# Make sure in swagger_helper.rb's components section you have:
|
||||||
|
# schemas: SwaggerDefinitions::CommonDefinitions.aggregate
|
||||||
|
].flatten.reduce({}, :merge)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
7
lib/templates/serializers_helper.rb
Normal file
7
lib/templates/serializers_helper.rb
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
module SerializersHelper
|
||||||
|
def serializers_map
|
||||||
|
{
|
||||||
|
# TheModel: V1::TheModelSerializer
|
||||||
|
}.freeze
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in New Issue
Block a user