Class: RubyCms::RoutesAssembler

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

Overview

Regenerates the RubyCMS-managed routes (the admin namespace plus any top-level public route fragments) between marker comments in config/routes.rb. Idempotent and additive: re-running with a larger module set replaces the managed block with the full current set, so install --add correctly adds new modules' routes.

Public fragments are sibling files named ".public.rb" and are emitted OUTSIDE the admin namespace; the core module's public fragment (the catch-all 404) is forced last.

Constant Summary collapse

BEGIN_MARKER =
"# >>> ruby_cms managed routes (regenerated by ruby_cms; do not edit inside)"
END_MARKER =
"# <<< ruby_cms managed routes"
REPLACE_PATTERN =
/^[ \t]*#{Regexp.escape(BEGIN_MARKER)}.*?#{Regexp.escape(END_MARKER)}[ \t]*\n/m

Instance Method Summary collapse

Constructor Details

#initialize(templates_root:, app_root:) ⇒ RoutesAssembler

Returns a new instance of RoutesAssembler.



20
21
22
23
# File 'lib/ruby_cms/routes_assembler.rb', line 20

def initialize(templates_root:, app_root:)
  @templates_root = Pathname.new(templates_root)
  @routes_path = Pathname.new(app_root).join("config/routes.rb")
end

Instance Method Details

#inject(modules) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ruby_cms/routes_assembler.rb', line 25

def inject(modules)
  return unless @routes_path.exist?

  block = build_block(modules)
  content = @routes_path.read
  content = ensure_root(content)
  content = if content.include?(BEGIN_MARKER)
              replace_block(content, block)
            else
              insert_before_final_end(content, block)
            end
  @routes_path.write(content)
end