Module: IsolateAssets::Helper

Includes:
ActionView::Helpers::AssetTagHelper, ActionView::Helpers::TagHelper
Defined in:
lib/isolate_assets/helper.rb

Instance Method Summary collapse

Instance Method Details

#asset_path(source) ⇒ Object Also known as: image_path, font_path, audio_path, video_path



43
44
45
46
47
# File 'lib/isolate_assets/helper.rb', line 43

def asset_path(source)
  ext = File.extname(source).delete_prefix(".")
  source_without_ext = source.sub(/\.#{Regexp.escape(ext)}\z/, "")
  isolated_assets.asset_url(source_without_ext, ext)
end

#audio_tag(source, **options) ⇒ Object



58
59
60
# File 'lib/isolate_assets/helper.rb', line 58

def audio_tag(source, **options)
  tag.audio(src: audio_path(source), **options)
end

#image_tag(source, **options) ⇒ Object



54
55
56
# File 'lib/isolate_assets/helper.rb', line 54

def image_tag(source, **options)
  tag.img(src: image_path(source), **options)
end

#javascript_importmap_tags(entry_point = "application", imports = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/isolate_assets/helper.rb', line 26

def javascript_importmap_tags(entry_point = "application", imports = {})
  assets_root = isolated_assets.engine.root.join("app/#{isolated_assets.assets_subdir}/javascripts")
  engine_imports = isolated_assets.javascript_files.each_with_object({}) do |path, hash|
    relative_path = path.relative_path_from(assets_root).to_s
    key = "#{isolated_assets.engine.engine_name}/#{relative_path.sub(/\.js\z/, "")}"
    hash[key] = isolated_assets.asset_url(relative_path.sub(/\.js\z/, ""), "js")
  end
  [
    tag.script(type: "importmap") do
      JSON.pretty_generate({"imports" => imports.merge(engine_imports)}).html_safe
    end,
    tag.script(<<~JS.html_safe, type: "module")
      import "#{isolated_assets.engine.engine_name}/#{entry_point}"
    JS
  ].join("\n").html_safe
end

#javascript_include_tag(source, **options) ⇒ Object



19
20
21
22
23
24
# File 'lib/isolate_assets/helper.rb', line 19

def javascript_include_tag(source, **options)
  tag.script(
    src: isolated_assets.asset_url(source, "js"),
    **options
  )
end

Note: isolated_assets method is defined by the including module, created dynamically in EngineExtension#isolate_assets



11
12
13
14
15
16
17
# File 'lib/isolate_assets/helper.rb', line 11

def stylesheet_link_tag(source, **options)
  tag.link(
    rel: "stylesheet",
    href: isolated_assets.asset_url(source, "css"),
    **options
  )
end

#video_tag(source, **options) ⇒ Object



62
63
64
# File 'lib/isolate_assets/helper.rb', line 62

def video_tag(source, **options)
  tag.video(src: video_path(source), **options)
end