Class: ReactManifest::LayoutPatcher

Inherits:
Object
  • Object
show all
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 defer: true %>\n".freeze
BUNDLE_TAG_HAML =
"= react_bundle_tag defer: true\n".freeze

Instance Method Summary collapse

Constructor Details

#initialize(config = ReactManifest.configuration) ⇒ LayoutPatcher

Returns a new instance of LayoutPatcher.



18
19
20
# File 'lib/react_manifest/layout_patcher.rb', line 18

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

Instance Method Details

#patch!Object

Patch all layout files. Returns array of Result objects.



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

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