Class: RubyCms::NavAssembler

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_cms/nav_assembler.rb

Overview

Generates config/initializers/admin_nav.rb from selected modules' nav fragments, wrapped once in a to_prepare block. Overwrites — regenerated on each install/add so it always reflects the installed module set.

Constant Summary collapse

HEADER =
"# RubyCMS sidebar navigation - generated by ruby_cms. Safe to edit."

Instance Method Summary collapse

Constructor Details

#initialize(templates_root:, app_root:) ⇒ NavAssembler

Returns a new instance of NavAssembler.



13
14
15
16
# File 'lib/ruby_cms/nav_assembler.rb', line 13

def initialize(templates_root:, app_root:)
  @templates_root = Pathname.new(templates_root)
  @dest = Pathname.new(app_root).join("config/initializers/admin_nav.rb")
end

Instance Method Details

#write(modules) ⇒ Object



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

def write(modules)
  fragments = modules.filter_map do |mod|
    next unless mod.nav

    path = @templates_root.join(mod.nav)
    path.exist? ? strip_header(path.read(encoding: "UTF-8")).chomp : nil
  end

  body = indent(fragments.join("\n\n"), 2)
  FileUtils.mkdir_p(@dest.dirname)
  @dest.write("# frozen_string_literal: true\n\n#{HEADER}\nRails.application.config.to_prepare do\n#{body}\nend\n",
              encoding: "UTF-8")
end