Skip to content

Commit

Permalink
Merge pull request #143 from dskang/js-css-path
Browse files Browse the repository at this point in the history
Use `asset_path` with error checking for all calls to `path_to_asset`
  • Loading branch information
rafaelfranca committed Apr 22, 2014
2 parents 9856b6d + 7e89379 commit 2de6c40
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 23 deletions.
15 changes: 5 additions & 10 deletions lib/sprockets/rails/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,12 @@ def compute_asset_path(path, options = {})
# Computes the full URL to a asset in the public directory. This
# method checks for errors before returning path.
def asset_path(source, options = {})
check_errors_for(source, options)
path_to_asset(source, options)
end
alias :path_to_asset_with_errors :asset_path

# 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.
def asset_url(source, options = {})
path_to_asset_with_errors(source, options.merge(:protocol => :request))
unless options[:debug]
check_errors_for(source, options)
end
super(source, options)
end
alias :path_to_asset :asset_path

# Get digest for asset path.
#
Expand Down
76 changes: 63 additions & 13 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def setup
@foo_js_digest = @assets['foo.js'].digest
@foo_css_digest = @assets['foo.css'].digest
@logo_digest = @assets["logo.png"].digest

Sprockets::Rails::Helper.raise_runtime_errors = false
end

def test_truth
Expand Down Expand Up @@ -370,10 +372,25 @@ def test_stylesheet_path
assert_equal "/assets/foo-#{@foo_css_digest}.css", @view.stylesheet_path("foo")
end

def test_public_folder_fallback_works_correctly
def test_asset_digest_path
assert_equal "foo-#{@foo_js_digest}.js", @view.asset_digest_path("foo.js")
assert_equal "foo-#{@foo_css_digest}.css", @view.asset_digest_path("foo.css")
end

def test_asset_digest
assert_equal @foo_js_digest, @view.asset_digest("foo.js")
assert_equal @foo_css_digest, @view.asset_digest("foo.css")
end
end

class PrecompileHelperTest < HelperTest
def setup
super

Sprockets::Rails::Helper.raise_runtime_errors = true
@view.debug_assets = true
end

def test_public_folder_fallback_works_correctly
@view.asset_path("asset-does-not-exist-foo.js")
@view.asset_url("asset-does-not-exist-foo.js")
@view.stylesheet_link_tag("asset-does-not-exist-foo.js")
Expand All @@ -397,10 +414,7 @@ def test_absolute_asset_path_error
end

def test_asset_not_precompiled_error
Sprockets::Rails::Helper.raise_runtime_errors = true
Sprockets::Rails::Helper.precompile = [ lambda {|logical_path| false } ]
@view.assets_environment = @assets
@view.debug_assets = true
Sprockets::Rails::Helper.precompile = [ lambda {|logical_path| false } ]

assert_raises(Sprockets::Rails::Helper::AssetFilteredError) do
@view.asset_path("foo.js")
Expand All @@ -410,6 +424,14 @@ def test_asset_not_precompiled_error
@view.asset_url("foo.js")
end

assert_raises(Sprockets::Rails::Helper::AssetFilteredError) do
@view.javascript_path("foo")
end

assert_raises(Sprockets::Rails::Helper::AssetFilteredError) do
@view.javascript_url("foo")
end

assert_raises(Sprockets::Rails::Helper::AssetFilteredError) do
@view.javascript_include_tag("foo.js")
end
Expand All @@ -426,10 +448,20 @@ def test_asset_not_precompiled_error

@view.asset_path("foo.js")
@view.asset_url("foo.js")
@view.javascript_path("foo")
@view.javascript_url("foo")
@view.javascript_include_tag("foo.js")
@view.javascript_include_tag("foo")
@view.javascript_include_tag(:foo)

assert_raises(Sprockets::Rails::Helper::AssetFilteredError) do
@view.stylesheet_path("foo")
end

assert_raises(Sprockets::Rails::Helper::AssetFilteredError) do
@view.stylesheet_url("foo")
end

assert_raises(Sprockets::Rails::Helper::AssetFilteredError) do
@view.stylesheet_link_tag("foo")
end
Expand All @@ -440,31 +472,49 @@ def test_asset_not_precompiled_error
@view.javascript_include_tag("foo")
end

@view.stylesheet_path("foo")
@view.stylesheet_url("foo")
@view.stylesheet_link_tag("foo")

Sprockets::Rails::Helper.precompile = [ lambda {|logical_path| true } ]

@view.asset_path("foo.js")
@view.asset_url("foo.js")
@view.javascript_path("foo")
@view.javascript_url("foo")
@view.javascript_include_tag("foo.js")
@view.javascript_include_tag("foo")
@view.javascript_include_tag(:foo)
@view.stylesheet_path("foo")
@view.stylesheet_url("foo")
@view.stylesheet_link_tag("foo")
end

def test_asset_digest_path
assert_equal "foo-#{@foo_js_digest}.js", @view.asset_digest_path("foo.js")
assert_equal "foo-#{@foo_css_digest}.css", @view.asset_digest_path("foo.css")
def test_debug_mode
@view.debug_assets = true

Sprockets::Rails::Helper.precompile = [ lambda {|logical_path| false } ]

assert_raises(Sprockets::Rails::Helper::AssetFilteredError) do
@view.javascript_include_tag("bar")
end

Sprockets::Rails::Helper.precompile = ['bar.js']

@view.javascript_include_tag("bar")
end

def test_asset_digest
assert_equal @foo_js_digest, @view.asset_digest("foo.js")
assert_equal @foo_css_digest, @view.asset_digest("foo.css")
def test_non_javascripts_and_stylesheets
loose_app_assets = lambda do |filename|
!%w(.js .css).include?(File.extname(filename))
end
Sprockets::Rails::Helper.precompile = [loose_app_assets, /(?:\/|\\|\A)application\.(css|js)$/]

@view.asset_path("logo.png")
end
end

class AutomaticDependenciesFromHelpersTest < HelperTest

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

0 comments on commit 2de6c40

Please sign in to comment.