Module: Sprockets::Rails::Helper

Includes:
ActionView::Helpers::AssetTagHelper, ActionView::Helpers::AssetUrlHelper, LegacyAssetTagHelper, LegacyAssetUrlHelper
Defined in:
lib/sprockets/rails/helper.rb

Defined Under Namespace

Classes: AbsoluteAssetPathError, AssetAliasUsed, AssetFilteredError

Constant Summary collapse

VIEW_ACCESSORS =
[:assets_environment, :assets_manifest,
:assets_prefix, :digest_assets, :debug_assets]

Constants included from LegacyAssetUrlHelper

LegacyAssetUrlHelper::ASSET_EXTENSIONS, LegacyAssetUrlHelper::ASSET_PUBLIC_DIRECTORIES, LegacyAssetUrlHelper::URI_REGEXP

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LegacyAssetUrlHelper

#asset_url, #audio_path, #compute_asset_extname, #compute_asset_host, #font_path, #image_path, #javascript_path, #stylesheet_path, #video_path

Class Attribute Details

.assetsObject

Returns the value of attribute assets.



9
10
11
# File 'lib/sprockets/rails/helper.rb', line 9

def assets
  @assets
end

.precompileObject

Returns the value of attribute precompile.



9
10
11
# File 'lib/sprockets/rails/helper.rb', line 9

def precompile
  @precompile
end

.raise_runtime_errorsObject

Returns the value of attribute raise_runtime_errors.



9
10
11
# File 'lib/sprockets/rails/helper.rb', line 9

def raise_runtime_errors
  @raise_runtime_errors
end

Class Method Details

.extended(obj) ⇒ Object



75
76
77
78
79
# File 'lib/sprockets/rails/helper.rb', line 75

def self.extended(obj)
  obj.class_eval do
    attr_accessor(*VIEW_ACCESSORS)
  end
end

.included(klass) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/sprockets/rails/helper.rb', line 63

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

Instance Method Details

#asset_digest(path, options = {}) ⇒ Object

Get digest for asset path.

path - String path options - Hash options

Returns String Hex digest or nil if digests are disabled.



110
111
112
113
114
115
116
# File 'lib/sprockets/rails/helper.rb', line 110

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 = {}) ⇒ Object

Expand asset path to digested form.

path - String path options - Hash options

Returns String path or nil if no asset was found.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/sprockets/rails/helper.rb', line 124

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]
      if self.raise_runtime_errors && path != asset.logical_path
        raise AssetAliasUsed.new(path, asset.logical_path)
      end

      return asset.digest_path
    end
  end
end

#asset_path(source, options = {}) ⇒ Object Also known as: path_to_asset

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



96
97
98
99
100
101
# File 'lib/sprockets/rails/helper.rb', line 96

def asset_path(source, options = {})
  unless options[:debug]
    check_errors_for(source, options)
  end
  super(source, options)
end

#assetsObject



16
17
18
# File 'lib/sprockets/rails/helper.rb', line 16

def assets
  Sprockets::Rails::Helper.assets
end

#compute_asset_path(path, options = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/sprockets/rails/helper.rb', line 81

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) ⇒ Object

Override javascript tag helper to provide debugging support.

Eventually will be deprecated and replaced by source maps.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/sprockets/rails/helper.rb', line 145

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

#precompileObject



12
13
14
# File 'lib/sprockets/rails/helper.rb', line 12

def precompile
  Sprockets::Rails::Helper.precompile
end

#raise_runtime_errorsObject



20
21
22
# File 'lib/sprockets/rails/helper.rb', line 20

def raise_runtime_errors
  Sprockets::Rails::Helper.raise_runtime_errors
end

Override stylesheet tag helper to provide debugging support.

Eventually will be deprecated and replaced by source maps.



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/sprockets/rails/helper.rb', line 168

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