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, 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



66
67
68
69
70
# File 'lib/sprockets/rails/helper.rb', line 66

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

.included(klass) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sprockets/rails/helper.rb', line 54

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.



101
102
103
104
105
106
107
# File 'lib/sprockets/rails/helper.rb', line 101

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.



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/sprockets/rails/helper.rb', line 115

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



87
88
89
90
91
92
# File 'lib/sprockets/rails/helper.rb', line 87

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



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/sprockets/rails/helper.rb', line 72

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.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/sprockets/rails/helper.rb', line 132

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.



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/sprockets/rails/helper.rb', line 155

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