Sha256: 5b381db56b9493e8f3507d785c245d68901ecc29922100ef809db1a960922a77

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
schemable-0.1.0 lib/generators/schemable/model_generator.rb