Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instead of complaining, add the dependency automatically #126

Merged
merged 1 commit into from
Apr 9, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 4 additions & 13 deletions lib/sprockets/rails/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ def raise_runtime_errors
Sprockets::Rails::Helper.raise_runtime_errors
end

class DependencyError < StandardError
def initialize(path, dep)
msg = "Asset depends on '#{dep}' to generate properly but has not declared the dependency\n"
msg << "Please add: `//= depend_on_asset \"#{dep}\"` to '#{path}'"
super msg
end
end

class AssetFilteredError < StandardError
def initialize(source)
msg = "Asset filtered out and will not be served: " <<
Expand Down Expand Up @@ -71,7 +63,7 @@ def self.extended(obj)

def compute_asset_path(path, options = {})
# Check if we are inside Sprockets context before calling check_dependencies!.
check_dependencies!(path) if defined?(_dependency_assets)
check_dependencies!(path) if defined?(depend_on)

if digest_path = asset_digest_path(path)
path = digest_path if digest_assets
Expand Down Expand Up @@ -177,11 +169,10 @@ def stylesheet_link_tag(*sources)
end

protected
# Checks if the asset is included in the dependencies list.
# Ensures the asset is included in the dependencies list.
def check_dependencies!(dep)
if raise_runtime_errors && !_dependency_assets.detect { |asset| asset.include?(dep) }
raise DependencyError.new(self.pathname, dep)
end
depend_on(dep)
depend_on_asset(dep)
end

# Raise errors when source does not exist or is not in the precompiled list
Expand Down
1 change: 0 additions & 1 deletion test/fixtures/url.css.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
//= depend_on_asset "logo.png"
p { background: url(<%= image_path "logo.png" %>); }
1 change: 0 additions & 1 deletion test/fixtures/url.js.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
//= depend_on_asset "foo.js"
var url = '<%= javascript_path :foo %>';
15 changes: 5 additions & 10 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -435,18 +435,13 @@ def test_asset_digest
end
end

class ErrorsInHelpersTest < HelperTest
class AutomaticDependenciesFromHelpersTest < HelperTest

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

assert_raises Sprockets::Rails::Helper::DependencyError do
@view.asset_path("error/dependency.js")
end
@view.asset_path("url.css")

Sprockets::Rails::Helper.raise_runtime_errors = false
@view.asset_path("error/dependency.js")
assert_equal ["logo.png", "url.css.erb"], @assets['url.css'].send(:dependency_paths).map {|d| File.basename(d.pathname) }.sort
end
end