Skip to content

Commit 3fbbfba

Browse files
committed
Merge pull request #126 from matthewd/automatic_dependencies
Instead of complaining, add the dependency automatically
2 parents ff1e483 + 5723c7e commit 3fbbfba

File tree

4 files changed

+9
-25
lines changed

4 files changed

+9
-25
lines changed

lib/sprockets/rails/helper.rb

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,6 @@ def raise_runtime_errors
2121
Sprockets::Rails::Helper.raise_runtime_errors
2222
end
2323

24-
class DependencyError < StandardError
25-
def initialize(path, dep)
26-
msg = "Asset depends on '#{dep}' to generate properly but has not declared the dependency\n"
27-
msg << "Please add: `//= depend_on_asset \"#{dep}\"` to '#{path}'"
28-
super msg
29-
end
30-
end
31-
3224
class AssetFilteredError < StandardError
3325
def initialize(source)
3426
msg = "Asset filtered out and will not be served: " <<
@@ -71,7 +63,7 @@ def self.extended(obj)
7163

7264
def compute_asset_path(path, options = {})
7365
# Check if we are inside Sprockets context before calling check_dependencies!.
74-
check_dependencies!(path) if defined?(_dependency_assets)
66+
check_dependencies!(path) if defined?(depend_on)
7567

7668
if digest_path = asset_digest_path(path)
7769
path = digest_path if digest_assets
@@ -177,11 +169,10 @@ def stylesheet_link_tag(*sources)
177169
end
178170

179171
protected
180-
# Checks if the asset is included in the dependencies list.
172+
# Ensures the asset is included in the dependencies list.
181173
def check_dependencies!(dep)
182-
if raise_runtime_errors && !_dependency_assets.detect { |asset| asset.include?(dep) }
183-
raise DependencyError.new(self.pathname, dep)
184-
end
174+
depend_on(dep)
175+
depend_on_asset(dep)
185176
end
186177

187178
# Raise errors when source does not exist or is not in the precompiled list

test/fixtures/url.css.erb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
//= depend_on_asset "logo.png"
21
p { background: url(<%= image_path "logo.png" %>); }

test/fixtures/url.js.erb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
//= depend_on_asset "foo.js"
21
var url = '<%= javascript_path :foo %>';

test/test_helper.rb

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -435,18 +435,13 @@ def test_asset_digest
435435
end
436436
end
437437

438-
class ErrorsInHelpersTest < HelperTest
438+
class AutomaticDependenciesFromHelpersTest < HelperTest
439439

440-
def test_dependency_error
441-
Sprockets::Rails::Helper.raise_runtime_errors = true
442-
Sprockets::Rails::Helper.precompile = [ lambda {|logical_path| true } ]
443-
@view.assets_environment = @assets
440+
def test_dependency_added
441+
@view.assets_environment = @assets
444442

445-
assert_raises Sprockets::Rails::Helper::DependencyError do
446-
@view.asset_path("error/dependency.js")
447-
end
443+
@view.asset_path("url.css")
448444

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

0 commit comments

Comments
 (0)