Sha256: 3fdd0020a15b05fc6a160f9f54e19bfbebb1a30888effbe28ee3f4810bfddd3f

Contents?: true

Size: 1.56 KB

Versions: 8

Compression:

Stored size: 1.56 KB

Contents

require "madmin/generator_helpers"

module Madmin
  module Generators
    class InstallGenerator < Rails::Generators::Base
      include Madmin::GeneratorHelpers

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

      def eager_load
        Rails.application.eager_load!
      end

      def copy_controller
        template("controller.rb.tt", "app/controllers/madmin/application_controller.rb")
      end

      def generate_routes
        if rails6_1_and_up?
          route "draw :madmin", file: ROUTES_FILE[:default]
          template("routes.rb.tt", "config/routes/madmin.rb")
        end

        if route_namespace_exists?
          route "root to: \"dashboard#show\"", indentation: separated_routes_file? ? 2 : 4, sentinel: /namespace :madmin do\s*\n/m
        else
          route "root to: \"dashboard#show\"", namespace: [:madmin]
        end
      end

      def generate_resources
        generateable_models.each do |model|
          if model.table_exists?
            call_generator "madmin:resource", model.to_s
          else
            puts "Skipping #{model} because database table does not exist"
          end
        end
      end

      private

      # Skip Abstract classes, ActiveRecord::Base, and auto-generated HABTM models
      def generateable_models
        active_record_models.reject do |model|
          model.abstract_class? || model == ActiveRecord::Base || model.name.start_with?("HABTM_")
        end
      end

      def active_record_models
        ObjectSpace.each_object(ActiveRecord::Base.singleton_class)
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
madmin-1.2.8 lib/generators/madmin/install/install_generator.rb
madmin-1.2.7 lib/generators/madmin/install/install_generator.rb
madmin-1.2.6 lib/generators/madmin/install/install_generator.rb
madmin-1.2.5 lib/generators/madmin/install/install_generator.rb
madmin-1.2.4 lib/generators/madmin/install/install_generator.rb
madmin-1.2.3 lib/generators/madmin/install/install_generator.rb
madmin-1.2.2 lib/generators/madmin/install/install_generator.rb
madmin-1.2.1 lib/generators/madmin/install/install_generator.rb