# module Sprockets::Rails::Helper

### Constants

VIEW_ACCESSORS

### Public Class Methods

extended(obj) click to toggle source

    # File lib/sprockets/rails/helper.rb, line 59
    def self.extended(obj)
      obj.class_eval do
        attr_accessor(*VIEW_ACCESSORS)

        remove_method :assets_environment
        def assets_environment
          if env = @assets_environment
            @assets_environment = env.cached
          else
            nil
          end
        end
      end
    end

included(klass) click to toggle source

    # File lib/sprockets/rails/helper.rb, line 42
    def self.included(klass)
      klass.class_attribute(*VIEW_ACCESSORS)

      klass.class_eval do
        remove_method :assets_environment
        def assets_environment
          if instance_variable_defined?(:@assets_environment)
            @assets_environment = @assets_environment.cached
          elsif env = self.class.assets_environment
            @assets_environment = env.cached
          else
            nil
          end
        end
      end
    end

### Public Instance Methods

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

Expand asset path to digested form.

path - String path options - Hash options

Returns String path or nil if no asset was found.

    # File lib/sprockets/rails/helper.rb, line 112
    def asset_digest_path(path, options = {})
      resolve_asset do |resolver|
        resolver.digest_path path, options[:debug]
      end
    end

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

Experimental: Get integrity for asset path.

path - String path options - Hash options

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

    # File lib/sprockets/rails/helper.rb, line 124
    def asset_integrity(path, options = {})
      path = path_with_extname(path, options)

      resolve_asset do |resolver|
        resolver.integrity path
      end
    end

assets_environment() click to toggle source

    # File lib/sprockets/rails/helper.rb, line 47
    def assets_environment
      if instance_variable_defined?(:@assets_environment)
        @assets_environment = @assets_environment.cached
      elsif env = self.class.assets_environment
        @assets_environment = env.cached
      else
        nil
      end
    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.

Calls superclass method

    # File lib/sprockets/rails/helper.rb, line 76
    def compute_asset_path(path, options = {})
      debug = options[:debug]

      if asset_path = resolve_asset_path(path, debug)
        File.join(assets_prefix || "/", legacy_debug_path(asset_path, debug))
      else
        message =  "The asset #{ path.inspect } is not present in the asset pipeline."
        raise AssetNotFound, message unless unknown_asset_fallback

        if respond_to?(:public_compute_asset_path)
          message << "Falling back to an asset that may be in the public folder.\n"
          message << "This behavior is deprecated and will be removed.\n"
          message << "To bypass the asset pipeline and preserve this behavior,\n"
          message << "use the `skip_pipeline: true` option.\n"

          call_stack = Kernel.respond_to?(:caller_locations) && ::Rails::VERSION::MAJOR >= 5 ? caller_locations : caller
          ActiveSupport::Deprecation.warn(message, call_stack)
        end
        super
      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.

Calls superclass method

    # File lib/sprockets/rails/helper.rb, line 135
    def javascript_include_tag(*sources)
      options = sources.extract_options!.stringify_keys
      integrity = compute_integrity?(options)

      if options["debug"] != false && request_debug_assets?
        sources.map { |source|
          if asset = lookup_debug_asset(source, type: :javascript)
            if asset.respond_to?(:to_a)
              asset.to_a.map do |a|
                super(path_to_javascript(a.logical_path, debug: true), options)
              end
            else
              super(path_to_javascript(asset.logical_path, debug: true), options)
            end
          else
            super(source, options)
          end
        }.flatten.uniq.join("\n").html_safe
      else
        sources.map { |source|
          options = options.merge('integrity' => asset_integrity(source, type: :javascript)) if integrity
          super source, options
        }.join("\n").html_safe
      end
    end

stylesheet_link_tag(*sources) click to toggle source

Override stylesheet tag helper to provide debugging support.

Eventually will be deprecated and replaced by source maps.

Calls superclass method

    # File lib/sprockets/rails/helper.rb, line 164
    def stylesheet_link_tag(*sources)
      options = sources.extract_options!.stringify_keys
      integrity = compute_integrity?(options)

      if options["debug"] != false && request_debug_assets?
        sources.map { |source|
          if asset = lookup_debug_asset(source, type: :stylesheet)
            if asset.respond_to?(:to_a)
              asset.to_a.map do |a|
                super(path_to_stylesheet(a.logical_path, debug: true), options)
              end
            else
              super(path_to_stylesheet(asset.logical_path, debug: true), options)
            end
          else
            super(source, options)
          end
        }.flatten.uniq.join("\n").html_safe
      else
        sources.map { |source|
          options = options.merge('integrity' => asset_integrity(source, type: :stylesheet)) if integrity
          super source, options
        }.join("\n").html_safe
      end
    end

### Protected Instance Methods

asset_resolver_strategies() click to toggle source

List of resolvers in `config.assets.resolve_with` order.

    # File lib/sprockets/rails/helper.rb, line 249
    def asset_resolver_strategies
      @asset_resolver_strategies ||=
        Array(resolve_assets_with).map do |name|
          HelperAssetResolvers[name].new(self)
        end
    end

compute_integrity?(options) click to toggle source

This is awkward: `integrity` is a boolean option indicating whether we want to include or omit the subresource integrity hash, but the options hash is also passed through as literal tag attributes. That means we have to delete the shortcut boolean option so it doesn't bleed into the tag attributes, but also check its value if it's boolean-ish.

    # File lib/sprockets/rails/helper.rb, line 197
    def compute_integrity?(options)
      if secure_subresource_integrity_context?
        case options['integrity']
        when nil, false, true
          options.delete('integrity') == true
        end
      else
        options.delete 'integrity'
        false
      end
    end

legacy_debug_path(path, debug) click to toggle source

Append ?body=1 if debug is on and we're on old Sprockets.

    # File lib/sprockets/rails/helper.rb, line 257
    def legacy_debug_path(path, debug)
      if debug && !using_sprockets4?
        "#{path}?body=1"
      else
        path
      end
    end

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

Internal method to support multifile debugging. Will eventually be removed w/ Sprockets 3.x.

    # File lib/sprockets/rails/helper.rb, line 225
    def lookup_debug_asset(path, options = {})
      path = path_with_extname(path, options)

      resolve_asset do |resolver|
        resolver.find_debug_asset path
      end
    end

path_with_extname(path, options) click to toggle source

compute_asset_extname is in AV::Helpers::AssetUrlHelper

    # File lib/sprockets/rails/helper.rb, line 234
    def path_with_extname(path, options)
      path = path.to_s
      "#{path}#{compute_asset_extname(path, options)}"
    end

request_debug_assets?() click to toggle source

Enable split asset debugging. Eventually will be deprecated and replaced by source maps in Sprockets 3.x.

    # File lib/sprockets/rails/helper.rb, line 217
    def request_debug_assets?
      debug_assets || (defined?(controller) && controller && params[:debug_assets])
    rescue # FIXME: what exactly are we rescuing?
      false
    end

resolve_asset() { |resolver| ... } click to toggle source

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

    # File lib/sprockets/rails/helper.rb, line 240
    def resolve_asset
      asset_resolver_strategies.detect do |resolver|
        if result = yield(resolver)
          break result
        end
      end
    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/sprockets/rails/helper.rb, line 211
    def secure_subresource_integrity_context?
      respond_to?(:request) && self.request && (self.request.local? || self.request.ssl?)
    end