Skip to content

Commit

Permalink
Merge pull request #350 from sbfaulkner/optional-raise-unless-precomp…
Browse files Browse the repository at this point in the history
…iled

add option to make raising exception for missing assets optional
  • Loading branch information
rafaelfranca committed May 23, 2016
2 parents 89ba006 + a88f181 commit b4f9006
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ is not enabled, then we check the environment if compiling is enabled:
```
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.

**`config.assets.check_precompiled_asset`**

When enabled, an exception is raised for missing assets. This option is enabled by default.

## Complementary plugins

The following plugins provide some extras for the Sprockets Asset Pipeline.
Expand Down
5 changes: 3 additions & 2 deletions lib/sprockets/rails/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def initialize(source)
:assets_environment, :assets_manifest,
:assets_precompile, :precompiled_asset_checker,
:assets_prefix, :digest_assets, :debug_assets,
:resolve_assets_with
:resolve_assets_with, :check_precompiled_asset
]

def self.included(klass)
Expand Down Expand Up @@ -299,6 +299,7 @@ def initialize(view)
raise ArgumentError, 'config.assets.resolve_with includes :environment, but app.assets is nil' unless view.assets_environment
@env = view.assets_environment
@precompiled_asset_checker = view.precompiled_asset_checker
@check_precompiled_asset = view.check_precompiled_asset
end

def asset_path(path, digest, allow_non_precompiled = false)
Expand Down Expand Up @@ -342,7 +343,7 @@ def precompiled?(path)
end

def raise_unless_precompiled_asset(path)
raise Helper::AssetNotPrecompiled.new(path) unless precompiled?(path)
raise Helper::AssetNotPrecompiled.new(path) if @check_precompiled_asset && !precompiled?(path)
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/sprockets/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def configure(&block)
config.assets.digest = true
config.assets.cache_limit = 50.megabytes
config.assets.gzip = true
config.assets.check_precompiled_asset = true

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

self.resolve_assets_with = config.assets.resolve_with

self.check_precompiled_asset = config.assets.check_precompiled_asset

# Expose the app precompiled asset check to the view
self.precompiled_asset_checker = -> logical_path { app.asset_precompiled? logical_path }
end
Expand Down
15 changes: 15 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def setup
@view.assets_prefix = "/assets"
@view.assets_precompile = %w( manifest.js )
precompiled_assets = @manifest.find(@view.assets_precompile).map(&:logical_path)
@view.check_precompiled_asset = true
@view.precompiled_asset_checker = -> logical_path { precompiled_assets.include? logical_path }
@view.request = ActionDispatch::Request.new({
"rack.url_scheme" => "https"
Expand Down Expand Up @@ -826,6 +827,20 @@ def test_index_files
end
end

class RaiseUnlessPrecompiledAssetDisabledTest < HelperTest
def test_check_precompiled_asset_enabled
@view.check_precompiled_asset = true
assert_raises(Sprockets::Rails::Helper::AssetNotPrecompiled) do
@view.asset_path("not_precompiled.css")
end
end

def test_check_precompiled_asset_disabled
@view.check_precompiled_asset = false
assert @view.asset_path("not_precompiled.css")
end
end

class PrecompiledDebugAssetHelperTest < PrecompiledAssetHelperTest

# Re-run all PrecompiledAssetHelperTest with a different setup
Expand Down
16 changes: 16 additions & 0 deletions test/test_railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,22 @@ def test_gzip_config
assert_equal false, env.gzip?
end

def test_default_check_precompiled_assets
assert app.config.assets.check_precompiled_asset
app.initialize!
@view = ActionView::Base.new
assert @view.check_precompiled_asset
end

def test_configure_check_precompiled_assets
app.configure do
config.assets.check_precompiled_asset = false
end
app.initialize!
@view = ActionView::Base.new
refute @view.check_precompiled_asset
end

def test_version
app.configure do
config.assets.version = 'v2'
Expand Down

0 comments on commit b4f9006

Please sign in to comment.