Class: Antoinette::InjectScriptTag

Inherits:
Object
  • Object
show all
Defined in:
lib/antoinette/services/inject_script_tag.rb

Constant Summary collapse

MARKER =
/<!-- antoinette( [a-f0-9]+)? -->/

Instance Method Summary collapse

Instance Method Details

#inject(template_path:, bundle_name:) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/antoinette/services/inject_script_tag.rb', line 7

def inject(template_path:, bundle_name:)
  full_path = Rails.root.join(template_path)
  content = File.read(full_path)

  script_tag = "<%= javascript_include_tag \"antoinette/#{bundle_name}\" %> <!-- antoinette -->"

  updated_content = if content.match?(MARKER)
    content.gsub(/^.*#{MARKER}.*$/o, script_tag)
  else
    content + "\n" + script_tag
  end

  File.write(full_path, updated_content)
end