Module: Madmin::GeneratorHelpers

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#call_generator(generator, *args) ⇒ Object



5
6
7
# File 'lib/madmin/generator_helpers.rb', line 5

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

#optimize_indentation(value, amount = 0) ⇒ Object

Method copied from Rails 6.1 master



32
33
34
35
# File 'lib/madmin/generator_helpers.rb', line 32

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

#rails6_1_and_up?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/madmin/generator_helpers.rb', line 13

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

#route(routing_code, namespace: nil, sentinel: nil, indentation: 2, file: default_routes_file) ⇒ Object

Method copied from Rails 6.1 master



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/madmin/generator_helpers.rb', line 18

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

#route_namespace_exists?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/madmin/generator_helpers.rb', line 9

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