Module: Sprockets::Rails::Helper

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

Defined Under Namespace

Classes: AssetAliasUsed, AssetNotPrecompiled

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(obj) ⇒ Object


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

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


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/sprockets/rails/helper.rb', line 34

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

Instance Method Details

#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.


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/sprockets/rails/helper.rb', line 82

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]
      unless options[:debug]
        if path != asset.logical_path
          raise AssetAliasUsed.new(path, asset.logical_path)
        end

        if !precompiled_assets.include?(asset)
          raise AssetNotPrecompiled.new(asset.logical_path)
        end
      end
      return asset.digest_path
    end
  end
end

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

Experimental: Get integrity for asset path.

path - String path options - Hash options

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


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/sprockets/rails/helper.rb', line 111

def asset_integrity(path, options = {})
  path = path.to_s
  if extname = compute_asset_extname(path, options)
    path = "#{path}#{extname}"
  end

  if manifest = assets_manifest
    if digest_path = manifest.assets[path]
      if  = manifest.files[digest_path]
        return ["integrity"]
      end
    end
  end

  if environment = assets_environment
    if asset = environment[path]
      return asset.integrity
    end
  end

  nil
end

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


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

def compute_asset_path(path, options = {})
  if digest_path = asset_digest_path(path, options)
    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.


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/sprockets/rails/helper.rb', line 137

def javascript_include_tag(*sources)
  options = sources.extract_options!.stringify_keys

  unless request_ssl?
    options.delete("integrity")
  end

  case options["integrity"]
  when true, false, nil
    compute_integrity = options.delete("integrity")
  end

  if options["debug"] != false && 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.map { |source|
      super(source, compute_integrity ?
        options.merge("integrity" => asset_integrity(source, :type => :javascript)) :
        options)
    }.join("\n").html_safe
  end
end

Override stylesheet tag helper to provide debugging support.

Eventually will be deprecated and replaced by source maps.


171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/sprockets/rails/helper.rb', line 171

def stylesheet_link_tag(*sources)
  options = sources.extract_options!.stringify_keys

  unless request_ssl?
    options.delete("integrity")
  end

  case options["integrity"]
  when true, false, nil
    compute_integrity = options.delete("integrity")
  end

  if options["debug"] != false && request_debug_assets?
    sources.map { |source|
      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.map { |source|
      super(source, compute_integrity ?
        options.merge("integrity" => asset_integrity(source, :type => :stylesheet)) :
        options)
    }.join("\n").html_safe
  end
end