module Sprockets::Rails::Helper - sprockets-rails-2.1.3 Documentation

# module Sprockets::Rails::Helper

### Constants

VIEW_ACCESSORS

### Attributes

assets[RW]
precompile[RW]
raise_runtime_errors[RW]

### Public Class Methods

extended(obj) click to toggle source

    # File lib/sprockets/rails/helper.rb, line 58
    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 46
    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, :debug_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 98
    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 112
    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

asset_path(source, options = {}) click to toggle source

Computes the full URL to a asset in the public directory. This method checks for errors before returning path.

    # File lib/sprockets/rails/helper.rb, line 79
    def asset_path(source, options = {})
      check_errors_for(source, options)
      path_to_asset(source, options)
    end

Also aliased as: path_to_asset_with_errors
asset_url(source, options = {}) click to toggle source

Computes the full URL to a asset in the public directory. This will use `asset_path` internally, so most of their behaviors will be the same.

    # File lib/sprockets/rails/helper.rb, line 88
    def asset_url(source, options = {})
      path_to_asset_with_errors(source, options.merge(:protocol => :request))
    end

assets() click to toggle source

    # File lib/sprockets/rails/helper.rb, line 16
    def assets
      Sprockets::Rails::Helper.assets
    end

assets_manifest() click to toggle source

    # File lib/sprockets/rails/helper.rb, line 50
    def assets_manifest; end

compute_asset_path(path, options = {}) click to toggle source
Calls superclass method Sprockets::Rails::LegacyAssetUrlHelper#compute_asset_path

    # File lib/sprockets/rails/helper.rb, line 64
    def compute_asset_path(path, options = {})
      # Check if we are inside Sprockets context before calling check_dependencies!.
      check_dependencies!(path) if defined?(depend_on)

      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.

Calls superclass method Sprockets::Rails::LegacyAssetTagHelper#javascript_include_tag

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

      if options["debug"] != false && request_debug_assets?
        sources.map { |source|
          check_errors_for(source, :type => :javascript)
          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

path_to_asset_with_errors(source, options = {})
Alias for: asset_path
precompile() click to toggle source

    # File lib/sprockets/rails/helper.rb, line 12
    def precompile
      Sprockets::Rails::Helper.precompile
    end

raise_runtime_errors() click to toggle source

    # File lib/sprockets/rails/helper.rb, line 20
    def raise_runtime_errors
      Sprockets::Rails::Helper.raise_runtime_errors
    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 Sprockets::Rails::LegacyAssetTagHelper#stylesheet_link_tag

    # File lib/sprockets/rails/helper.rb, line 152
    def stylesheet_link_tag(*sources)
      options = sources.extract_options!.stringify_keys
      if options["debug"] != false && request_debug_assets?
        sources.map { |source|
          check_errors_for(source, :type => :stylesheet)
          if asset = lookup_asset_for_path(source, :type => :stylesheet)
            asset.to_a.map do |a|
              super(path_to_stylesheet(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

asset_needs_precompile?(source, filename) click to toggle source

Returns true when an asset will not be available after precompile is run

    # File lib/sprockets/rails/helper.rb, line 190
    def asset_needs_precompile?(source, filename)
      if assets_environment && assets_environment.send(:matches_filter, precompile || [], source, filename)
        false
      else
        true
      end
    end

check_dependencies!(dep) click to toggle source

Ensures the asset is included in the dependencies list.

    # File lib/sprockets/rails/helper.rb, line 173
    def check_dependencies!(dep)
      depend_on(dep)
      depend_on_asset(dep)
    end

check_errors_for(source, options) click to toggle source

Raise errors when source does not exist or is not in the precompiled list

    # File lib/sprockets/rails/helper.rb, line 179
    def check_errors_for(source, options)
      source = source.to_s
      return source if !self.raise_runtime_errors || source.blank? || source =~ URI_REGEXP
      asset = lookup_asset_for_path(source, options)

      if asset && asset_needs_precompile?(asset.logical_path, asset.pathname.to_s)
        raise AssetFilteredError.new(asset.logical_path)
      end
    end

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 208
    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 200
    def request_debug_assets?
      debug_assets || (defined?(controller) && controller && params[:debug_assets])
    rescue
      return false
    end