Class: RubyCms::HelperWiring

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

Overview

Mixes the modules RubyCMS admin views rely on into the host app's ApplicationHelper:

* RubyUI            — Phlex::Kit, makes components callable as methods in ERB
                    (e.g. AdminPageHeader(...)); defined by ruby_ui:install
* CmsApplicationHelpers / ContentBlocksHelper — shipped by the gem

Pagy 43+ has no Pagy::Frontend; admin pagination uses Pagy::Offset in AdminPagination and RubyUI::DataTable components instead. Idempotent: only adds includes that are missing.

Constant Summary collapse

INCLUDES =
%w[CmsApplicationHelpers RubyUI ContentBlocksHelper].freeze
OBSOLETE_INCLUDES =
%w[Pagy::Frontend].freeze

Instance Method Summary collapse

Constructor Details

#initialize(app_root:) ⇒ HelperWiring

Returns a new instance of HelperWiring.



17
18
19
# File 'lib/ruby_cms/helper_wiring.rb', line 17

def initialize(app_root:)
  @path = Pathname.new(app_root).join("app/helpers/application_helper.rb")
end

Instance Method Details

#apply!Object

rubocop:disable Naming/PredicateMethod



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ruby_cms/helper_wiring.rb', line 21

def apply! # rubocop:disable Naming/PredicateMethod
  return false unless @path.exist?

  content = @path.read
  cleaned = OBSOLETE_INCLUDES.reduce(content) do |text, mod|
    text.gsub(/^\s*include #{Regexp.escape(mod)}\n/, "")
  end
  missing = INCLUDES.reject {|mod| cleaned.include?("include #{mod}") }
  changed = cleaned != content
  return false if missing.empty? && !changed
  return false unless cleaned.include?("module ApplicationHelper\n")

  if missing.any?
    lines = missing.map {|mod| "  include #{mod}" }.join("\n")
    cleaned = cleaned.sub("module ApplicationHelper\n", "module ApplicationHelper\n#{lines}\n")
  end
  @path.write(cleaned)
  true
end