Sha256: 8af951d1291e3823f8446d5c136e235f8e8808d70a2820c226a3619954e5546f

Contents?: true

Size: 1.6 KB

Versions: 12

Compression:

Stored size: 1.6 KB

Contents

module Madmin
  module GeneratorHelpers
    ROUTES_FILE = {default: "config/routes.rb", separated: "config/routes/madmin.rb"}.freeze

    def call_generator(generator, *args)
      Rails::Generators.invoke(generator, args, generator_options)
    end

    def route_namespace_exists?
      File.readlines(Rails.root.join(default_routes_file)).grep(/namespace :madmin/).size > 0
    end

    def rails6_1_and_up?
      Gem.loaded_specs["rails"].version >= Gem::Version.new(6.1)
    end

    # Method copied from Rails 6.1 master
    def route(routing_code, namespace: nil, sentinel: nil, indentation: 2, file: default_routes_file)
      routing_code = Array(namespace).reverse.reduce(routing_code) { |code, ns|
        "namespace :#{ns} do\n#{indent(code, 2)}\nend"
      }

      log :route, routing_code
      sentinel ||= default_sentinel(file)

      in_root do
        inject_into_file file, optimize_indentation(routing_code, indentation), after: sentinel, verbose: false, force: false
      end
    end

    # Method copied from Rails 6.1 master
    def optimize_indentation(value, amount = 0)
      return "#{value}\n" unless value.is_a?(String)
      "#{value.strip_heredoc.indent(amount).chomp}\n"
    end

    private

    def separated_routes_file?
      default_routes_file.eql?(ROUTES_FILE[:separated])
    end

    def default_sentinel(file)
      file.eql?(ROUTES_FILE[:default]) ? /\.routes\.draw do\s*\n/m : /namespace :madmin do\s*\n/m
    end

    def default_routes_file
      rails6_1_and_up? ? ROUTES_FILE[:separated] : ROUTES_FILE[:default]
    end

    def generator_options
      {behavior: behavior}
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
madmin-2.0.0 lib/madmin/generator_helpers.rb
madmin-1.2.11 lib/madmin/generator_helpers.rb
madmin-1.2.10 lib/madmin/generator_helpers.rb
madmin-1.2.9 lib/madmin/generator_helpers.rb
madmin-1.2.8 lib/madmin/generator_helpers.rb
madmin-1.2.7 lib/madmin/generator_helpers.rb
madmin-1.2.6 lib/madmin/generator_helpers.rb
madmin-1.2.5 lib/madmin/generator_helpers.rb
madmin-1.2.4 lib/madmin/generator_helpers.rb
madmin-1.2.3 lib/madmin/generator_helpers.rb
madmin-1.2.2 lib/madmin/generator_helpers.rb
madmin-1.2.1 lib/madmin/generator_helpers.rb