module Condenser::Rails::Helper

Constants

VIEW_ACCESSORS

Public Class Methods

extended(obj) click to toggle source
# File lib/condenser/rails/helper.rb, line 32
def self.extended(obj)
  obj.class_eval do
    attr_accessor(*VIEW_ACCESSORS)
  end
end
included(klass) click to toggle source
# File lib/condenser/rails/helper.rb, line 28
def self.included(klass)
  klass.class_attribute(*VIEW_ACCESSORS)
end

Public Instance Methods

asset_integrity(path, options = {}) click to toggle source

Get integrity for asset path.

path - String path options - Hash options

Returns String integrity attribute or nil if no asset was found.

# File lib/condenser/rails/helper.rb, line 61
def asset_integrity(path, options = {})
  asset_resolver.integrity(path)
end
compute_asset_path(path, options = {}) click to toggle source

Writes over the built in ActionView::Helpers::AssetUrlHelper#compute_asset_path to use the asset pipeline.

# File lib/condenser/rails/helper.rb, line 40
def compute_asset_path(path, options = {})
  if asset_path = resolve_asset_path(path)
    File.join(assets_prefix || "/", asset_path)
  else
    raise Condenser::Rails::AssetNotPrecompiledError.new(path)
    # raise AssetNotFound, "The asset #{ path.inspect } is not present in the asset pipeline.\n"
  end
end
javascript_include_tag(*sources) click to toggle source

Override javascript tag helper to provide debugging support.

Eventually will be deprecated and replaced by source maps.

# File lib/condenser/rails/helper.rb, line 68
def javascript_include_tag(*sources)
  options = sources.extract_options!.stringify_keys
  path_options = options.extract!("protocol", "extname", "host", "skip_pipeline").symbolize_keys
  early_hints_links = []

  sources_tags = sources.uniq.map { |source|
    href = path_to_javascript(source, path_options)
    early_hints_links << "<#{href}>; rel=preload; as=script"
    tag_options = {
      "src" => href
    }.merge!(options)
    
    if tag_options["nonce"] == true
      tag_options["nonce"] = content_security_policy_nonce
    end
    
    if secure_subresource_integrity_context?
      if tag_options["integrity"] == true
        tag_options["integrity"] = asset_integrity(source.to_s.delete_suffix('.js')+'.js')
      elsif tag_options["integrity"] == false
        tag_options.delete('integrity')
      end
    else
      tag_options.delete('integrity')
    end
    
    content_tag("script", "", tag_options)
  }.join("\n").html_safe

  request.send_early_hints("Link" => early_hints_links.join("\n")) if respond_to?(:request) && request

  sources_tags
end

Protected Instance Methods

asset_resolver() click to toggle source

Try each asset resolver and return the first non-nil result.

# File lib/condenser/rails/helper.rb, line 152
def asset_resolver
  @asset_resolver ||= HelperAssetResolvers[resolve_assets_with].new(self)
end
path_with_extname(path, options) click to toggle source

compute_asset_extname is in AV::Helpers::AssetUrlHelper

# File lib/condenser/rails/helper.rb, line 146
def path_with_extname(path, options)
  path = path.to_s
  "#{path}#{compute_asset_extname(path, options)}"
end
secure_subresource_integrity_context?() click to toggle source

Only serve integrity metadata for HTTPS requests:

http://www.w3.org/TR/SRI/#non-secure-contexts-remain-non-secure
# File lib/condenser/rails/helper.rb, line 140
def secure_subresource_integrity_context?
  self.request.nil? || self.request && (self.request.local? || self.request.ssl?)
  # respond_to?(:request) && self.request && (self.request.local? || self.request.ssl?)
end