module IziLightup::InlineAsset

Public Class Methods

inline_css(paths = []) click to toggle source
# File lib/izi_lightup/inline_asset.rb, line 18
def inline_css(paths = [])
  return '' if paths.blank?

  "<style type='text/css'>#{inline_file(paths, :css)}</style>"
end
inline_file(paths, format = nil) click to toggle source
# File lib/izi_lightup/inline_asset.rb, line 6
def inline_file(paths, format = nil)
  Array.wrap(paths).map do |asset_path|
    raw_source(with_ext(asset_path, format))
  end.join("\n" * 3)
end
inline_js(paths = []) click to toggle source
# File lib/izi_lightup/inline_asset.rb, line 12
def inline_js(paths = [])
  return '' if paths.blank?

  "<script type='text/javascript'>#{inline_file(paths, :js)}</script>"
end

Private Class Methods

find_sources_fallback(asset_path) click to toggle source
# File lib/izi_lightup/inline_asset.rb, line 51
def find_sources_fallback(asset_path)
  Rails.application.assets.find_asset(asset_path)&.source&.html_safe
end
manifest() click to toggle source
# File lib/izi_lightup/inline_asset.rb, line 45
def manifest
  return Rails.application.assets unless Rails.application.respond_to?(:assets_manifest)

  @manifest ||= Rails.application&.assets_manifest
end
old_manifest?() click to toggle source
# File lib/izi_lightup/inline_asset.rb, line 41
def old_manifest?
  !manifest.respond_to?(:find_sources)
end
raw_source(asset_path) click to toggle source
# File lib/izi_lightup/inline_asset.rb, line 35
def raw_source(asset_path)
  return find_sources_fallback(asset_path) if old_manifest?

  manifest.find_sources(asset_path).first&.html_safe
end
with_ext(path, format = nil) click to toggle source
# File lib/izi_lightup/inline_asset.rb, line 26
def with_ext(path, format = nil)
  return path if format.nil?

  ext = File.extname(path)
  return path if ext.present?

  "#{path}.#{format}"
end