Skip to content

Commit 270d0df

Browse files
committed
Check precompile for the asset's logical_path
.. not whatever source we were originally given, which may be missing an extension. In doing so, we must also ensure that when we look up an asset from an extensionless name, we end up with the one we intended -- so check_errors_for should pass a :type through. Fixes #128.
1 parent fdce388 commit 270d0df

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

lib/sprockets/rails/helper.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def compute_asset_path(path, options = {})
7070
# Computes the full URL to a asset in the public directory. This
7171
# method checks for errors before returning path.
7272
def asset_path(source, options = {})
73-
check_errors_for(source)
73+
check_errors_for(source, options)
7474
path_to_asset(source, options)
7575
end
7676
alias :path_to_asset_with_errors :asset_path
@@ -124,7 +124,7 @@ def javascript_include_tag(*sources)
124124

125125
if options["debug"] != false && request_debug_assets?
126126
sources.map { |source|
127-
check_errors_for(source)
127+
check_errors_for(source, :type => :javascript)
128128
if asset = lookup_asset_for_path(source, :type => :javascript)
129129
asset.to_a.map do |a|
130130
super(path_to_javascript(a.logical_path, :debug => true), options)
@@ -146,7 +146,7 @@ def stylesheet_link_tag(*sources)
146146
options = sources.extract_options!.stringify_keys
147147
if options["debug"] != false && request_debug_assets?
148148
sources.map { |source|
149-
check_errors_for(source)
149+
check_errors_for(source, :type => :stylesheet)
150150
if asset = lookup_asset_for_path(source, :type => :stylesheet)
151151
asset.to_a.map do |a|
152152
super(path_to_stylesheet(a.logical_path, :debug => true), options)
@@ -169,13 +169,13 @@ def check_dependencies!(dep)
169169
end
170170

171171
# Raise errors when source does not exist or is not in the precompiled list
172-
def check_errors_for(source)
172+
def check_errors_for(source, options)
173173
source = source.to_s
174174
return source if !self.raise_runtime_errors || source.blank? || source =~ URI_REGEXP
175-
asset = lookup_asset_for_path(source)
175+
asset = lookup_asset_for_path(source, options)
176176

177-
if asset && asset_needs_precompile?(source, asset.pathname.to_s)
178-
raise AssetFilteredError.new(source)
177+
if asset && asset_needs_precompile?(asset.logical_path, asset.pathname.to_s)
178+
raise AssetFilteredError.new(asset.logical_path)
179179
end
180180
end
181181

test/test_helper.rb

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,18 +406,34 @@ def test_asset_not_precompiled_error
406406
@view.javascript_include_tag(:foo)
407407
end
408408

409+
Sprockets::Rails::Helper.precompile = ['foo.js']
410+
411+
@view.asset_path("foo.js")
412+
@view.asset_url("foo.js")
413+
@view.javascript_include_tag("foo.js")
414+
@view.javascript_include_tag("foo")
415+
@view.javascript_include_tag(:foo)
416+
417+
assert_raises(Sprockets::Rails::Helper::AssetFilteredError) do
418+
@view.stylesheet_link_tag("foo")
419+
end
420+
421+
Sprockets::Rails::Helper.precompile = ['foo.css']
422+
409423
assert_raises(Sprockets::Rails::Helper::AssetFilteredError) do
410-
@view.stylesheet_link_tag("foo.js")
424+
@view.javascript_include_tag("foo")
411425
end
412426

427+
@view.stylesheet_link_tag("foo")
428+
413429
Sprockets::Rails::Helper.precompile = [ lambda {|logical_path| true } ]
414430

415431
@view.asset_path("foo.js")
416432
@view.asset_url("foo.js")
417433
@view.javascript_include_tag("foo.js")
418434
@view.javascript_include_tag("foo")
419435
@view.javascript_include_tag(:foo)
420-
@view.stylesheet_link_tag("foo.js")
436+
@view.stylesheet_link_tag("foo")
421437
end
422438

423439
def test_asset_digest_path
@@ -434,8 +450,6 @@ def test_asset_digest
434450
class AutomaticDependenciesFromHelpersTest < HelperTest
435451

436452
def test_dependency_added
437-
@view.assets_environment = @assets
438-
439453
@view.asset_path("url.css")
440454

441455
assert_equal ["logo.png", "url.css.erb"], @assets['url.css'].send(:dependency_paths).map {|d| File.basename(d.pathname) }.sort

0 commit comments

Comments
 (0)