Skip to content

Commit b4f9006

Browse files
committed
Merge pull request #350 from sbfaulkner/optional-raise-unless-precompiled
add option to make raising exception for missing assets optional
2 parents 89ba006 + a88f181 commit b4f9006

File tree

5 files changed

+41
-2
lines changed

5 files changed

+41
-2
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ is not enabled, then we check the environment if compiling is enabled:
121121
```
122122
If the resolver list is empty (e.g. if debug is true and compile is false), the standard rails public path resolution will be used.
123123

124+
**`config.assets.check_precompiled_asset`**
125+
126+
When enabled, an exception is raised for missing assets. This option is enabled by default.
127+
124128
## Complementary plugins
125129

126130
The following plugins provide some extras for the Sprockets Asset Pipeline.

lib/sprockets/rails/helper.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def initialize(source)
3333
:assets_environment, :assets_manifest,
3434
:assets_precompile, :precompiled_asset_checker,
3535
:assets_prefix, :digest_assets, :debug_assets,
36-
:resolve_assets_with
36+
:resolve_assets_with, :check_precompiled_asset
3737
]
3838

3939
def self.included(klass)
@@ -299,6 +299,7 @@ def initialize(view)
299299
raise ArgumentError, 'config.assets.resolve_with includes :environment, but app.assets is nil' unless view.assets_environment
300300
@env = view.assets_environment
301301
@precompiled_asset_checker = view.precompiled_asset_checker
302+
@check_precompiled_asset = view.check_precompiled_asset
302303
end
303304

304305
def asset_path(path, digest, allow_non_precompiled = false)
@@ -342,7 +343,7 @@ def precompiled?(path)
342343
end
343344

344345
def raise_unless_precompiled_asset(path)
345-
raise Helper::AssetNotPrecompiled.new(path) unless precompiled?(path)
346+
raise Helper::AssetNotPrecompiled.new(path) if @check_precompiled_asset && !precompiled?(path)
346347
end
347348
end
348349
end

lib/sprockets/railtie.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ def configure(&block)
113113
config.assets.digest = true
114114
config.assets.cache_limit = 50.megabytes
115115
config.assets.gzip = true
116+
config.assets.check_precompiled_asset = true
116117

117118
config.assets.configure do |env|
118119
config.assets.paths.each { |path| env.append_path(path) }
@@ -235,6 +236,8 @@ def self.build_manifest(app)
235236

236237
self.resolve_assets_with = config.assets.resolve_with
237238

239+
self.check_precompiled_asset = config.assets.check_precompiled_asset
240+
238241
# Expose the app precompiled asset check to the view
239242
self.precompiled_asset_checker = -> logical_path { app.asset_precompiled? logical_path }
240243
end

test/test_helper.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def setup
2727
@view.assets_prefix = "/assets"
2828
@view.assets_precompile = %w( manifest.js )
2929
precompiled_assets = @manifest.find(@view.assets_precompile).map(&:logical_path)
30+
@view.check_precompiled_asset = true
3031
@view.precompiled_asset_checker = -> logical_path { precompiled_assets.include? logical_path }
3132
@view.request = ActionDispatch::Request.new({
3233
"rack.url_scheme" => "https"
@@ -826,6 +827,20 @@ def test_index_files
826827
end
827828
end
828829

830+
class RaiseUnlessPrecompiledAssetDisabledTest < HelperTest
831+
def test_check_precompiled_asset_enabled
832+
@view.check_precompiled_asset = true
833+
assert_raises(Sprockets::Rails::Helper::AssetNotPrecompiled) do
834+
@view.asset_path("not_precompiled.css")
835+
end
836+
end
837+
838+
def test_check_precompiled_asset_disabled
839+
@view.check_precompiled_asset = false
840+
assert @view.asset_path("not_precompiled.css")
841+
end
842+
end
843+
829844
class PrecompiledDebugAssetHelperTest < PrecompiledAssetHelperTest
830845

831846
# Re-run all PrecompiledAssetHelperTest with a different setup

test/test_railtie.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,22 @@ def test_gzip_config
182182
assert_equal false, env.gzip?
183183
end
184184

185+
def test_default_check_precompiled_assets
186+
assert app.config.assets.check_precompiled_asset
187+
app.initialize!
188+
@view = ActionView::Base.new
189+
assert @view.check_precompiled_asset
190+
end
191+
192+
def test_configure_check_precompiled_assets
193+
app.configure do
194+
config.assets.check_precompiled_asset = false
195+
end
196+
app.initialize!
197+
@view = ActionView::Base.new
198+
refute @view.check_precompiled_asset
199+
end
200+
185201
def test_version
186202
app.configure do
187203
config.assets.version = 'v2'

0 commit comments

Comments
 (0)