Class: ReactManifest::LayoutPatcher

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/react_manifest/layout_patcher.rb

Overview

Patches Rails layout files to insert ‘react_bundle_tag` after the `javascript_include_tag` call, or before `</head>` as a fallback.

Supports .html.erb, .html.haml, and .html.slim. Skips files that already contain ‘react_bundle_tag`. Atomic write: backs up original before writing.

Usage:

ReactManifest::LayoutPatcher.new(config).patch!

Defined Under Namespace

Classes: Result

Constant Summary collapse

LAYOUTS_GLOB =
"app/views/layouts/*.html.{erb,haml,slim}".freeze
BUNDLE_TAG_ERB =
"<%= react_bundle_tag %>\n".freeze
BUNDLE_TAG_HAML =
"= react_bundle_tag\n".freeze

Instance Method Summary collapse

Methods included from Logging

#log_debug, #log_info, #log_warn

Constructor Details

#initialize(config = ReactManifest.configuration) ⇒ LayoutPatcher

Returns a new instance of LayoutPatcher.



20
21
22
# File 'lib/react_manifest/layout_patcher.rb', line 20

def initialize(config = ReactManifest.configuration)
  @config = config
end

Instance Method Details

#patch!Object

Patch all layout files. Returns array of Result objects.



25
26
27
28
29
30
31
32
# File 'lib/react_manifest/layout_patcher.rb', line 25

def patch!
  layouts = find_layouts
  if layouts.empty?
    log_info "No layout files found in #{layouts_dir}"
    return []
  end
  layouts.map { |f| patch_file(f) }
end