Skip to content

Commit bb43c49

Browse files
rafaelfrancaRafael Mendonça França
authored andcommitted
Merge pull request #126 from matthewd/automatic_dependencies
Instead of complaining, add the dependency automatically
1 parent 3fd8ee3 commit bb43c49

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: " <<
@@ -64,7 +56,7 @@ def self.extended(obj)
6456

6557
def compute_asset_path(path, options = {})
6658
# Check if we are inside Sprockets context before calling check_dependencies!.
67-
check_dependencies!(path) if defined?(_dependency_assets)
59+
check_dependencies!(path) if defined?(depend_on)
6860

6961
if digest_path = asset_digest_path(path)
7062
path = digest_path if digest_assets
@@ -170,11 +162,10 @@ def stylesheet_link_tag(*sources)
170162
end
171163

172164
protected
173-
# Checks if the asset is included in the dependencies list.
165+
# Ensures the asset is included in the dependencies list.
174166
def check_dependencies!(dep)
175-
if raise_runtime_errors && !_dependency_assets.detect { |asset| asset.include?(dep) }
176-
raise DependencyError.new(self.pathname, dep)
177-
end
167+
depend_on(dep)
168+
depend_on_asset(dep)
178169
end
179170

180171
# 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)