module Sprockets::Rails::Helper

Constants

VIEW_ACCESSORS

Public Class Methods

extended(obj) click to toggle source
# File lib/sprockets/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/sprockets/rails/helper.rb, line 20
def self.included(klass)
  if klass < Sprockets::Context
    klass.class_eval do
      alias_method :assets_environment, :environment
      def assets_manifest; end
      class_attribute :config, :assets_prefix, :digest_assets
    end
  else
    klass.class_attribute(*VIEW_ACCESSORS)
  end
end

Public Instance Methods

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

Get digest for asset path.

path - String path options - Hash options

Returns String Hex digest or nil if digests are disabled.

# File lib/sprockets/rails/helper.rb, line 54
def asset_digest(path, options = {})
  return unless digest_assets

  if digest_path = asset_digest_path(path, options)
    digest_path[/-(.+)\./, 1]
  end
end
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 68
def asset_digest_path(path, options = {})
  if manifest = assets_manifest
    if digest_path = manifest.assets[path]
      return digest_path
    end
  end

  if environment = assets_environment
    if asset = environment[path]
      return asset.digest_path
    end
  end
end
assets_manifest() click to toggle source
# File lib/sprockets/rails/helper.rb, line 24
def assets_manifest; end
compute_asset_path(path, options = {}) click to toggle source
# File lib/sprockets/rails/helper.rb, line 38
def compute_asset_path(path, options = {})
  if digest_path = asset_digest_path(path)
    path = digest_path if digest_assets
    path += "?body=1" if options[:debug]
    File.join(assets_prefix || "/", path)
  else
    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.

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

  if request_debug_assets?
    sources.map { |source|
      if asset = lookup_asset_for_path(source, :type => :javascript)
        asset.to_a.map do |a|
          super(path_to_javascript(a.logical_path, :debug => true), options)
        end
      else
        super(source, options)
      end
    }.flatten.uniq.join("\n").html_safe
  else
    sources.push(options)
    super(*sources)
  end
end

Protected Instance Methods

lookup_asset_for_path(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 135
def lookup_asset_for_path(path, options = {})
  return unless env = assets_environment
  path = path.to_s
  if extname = compute_asset_extname(path, options)
    path = "#{path}#{extname}"
  end
  env[path]
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 129
def request_debug_assets?
  debug_assets || (defined?(controller) && controller && params[:debug_assets])
end