Sha256: e6b82a86ce6ef0355c9d44c428f83162875d3bc9fd8512b0f1b4064a9f8dab30

Contents?: true

Size: 1.9 KB

Versions: 1

Compression:

Stored size: 1.9 KB

Contents

require "rails/generators/named_base"
require "madmin/generator_helpers"

module Madmin
  module Generators
    class ResourceGenerator < Rails::Generators::NamedBase
      ATTRIBUTE_TYPE_MAPPING = {
        boolean: "Field::CheckBox",
        date: "Field::DateTime",
        datetime: "Field::DateTime",
        enum: "Field::Text",
        float: "Field::Number",
        integer: "Field::Number",
        time: "Field::DateTime",
        text: "Field::TextArea",
        string: "Field::Text",
      }

      source_root File.expand_path("../templates", __FILE__)

      def create_resource_file
        template(
          "resource.rb.erb",
          Rails.root.join("app/madmin/resources/#{file_name}.rb"),
        )
      end

      private

      def attributes
        klass.attribute_types.map { |name, attr|
          # Skip attributes related to associations
          next if ignored_attributes.include?(name)

          [name, madmin_type_for_column(attr.type)]
        }.compact
      end

      def associations
        klass.reflections.map do |name, association|
          if association.polymorphic?
            [name, "Field::Polymorphic"]
          elsif association.belongs_to?
            [name, "Field::BelongsTo"]
          elsif association.has_one?
            [name, "Field::HasOne"]
          else
            [name, "Field::HasMany"]
          end
        end
      end

      def ignored_attributes
        attrs = []

        klass.reflections.map do |name, association|
          if association.polymorphic?
            attrs += [association.foreign_key, association.foreign_type]
          elsif association.belongs_to?
            attrs += [association.foreign_key]
          end
        end

        attrs
      end

      def klass
        @klass ||= Object.const_get(class_name)
      end

      def madmin_type_for_column(column_type)
        ATTRIBUTE_TYPE_MAPPING[column_type]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
madmin-0.1.0 lib/generators/madmin/resource/resource_generator.rb