Sha256: e848364c02c524717515afbf19791c8722d9616f20f53576e2de5524f8e65d67

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

require "rails/generators/named_base"

module Administrate
  module Generators
    class DashboardGenerator < Rails::Generators::NamedBase
      TABLE_ATTRIBUTE_LIMIT = 4

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

      def create_dashboard_definition
        template "dashboard.rb.erb", "app/dashboards/#{file_name}_dashboard.rb"
      end

      def create_resource_controller
        template(
          "controller.rb.erb",
          "app/controllers/admin/#{file_name.pluralize}_controller.rb"
        )
      end

      private

      def attributes
        klass.attribute_names + klass.reflections.keys
      end

      def field_type(attribute)
        klass.type_for_attribute(attribute).type ||
          association_type(attribute)
      end

      def association_type(attribute)
        reflection = klass.reflections[attribute.to_s]
        if reflection.collection?
          :has_many
        else
          :belongs_to
        end
      end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
administrate-0.0.6 lib/generators/administrate/dashboard/dashboard_generator.rb