Skip to content

Commit

Permalink
Merge pull request #375 from rails/schneems/deprecate-fallback-behavior
Browse files Browse the repository at this point in the history
Deprecate asset fallback
  • Loading branch information
schneems committed Sep 1, 2016
2 parents 1153896 + 09730f9 commit e3dc15c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ Each asset task will invoke `assets:environment` first. By default this loads th

Also see [Sprockets::Rails::Task](https://github.com/rails/sprockets-rails/blob/master/lib/sprockets/rails/task.rb) and [Rake::SprocketsTask](https://github.com/rails/sprockets/blob/master/lib/rake/sprocketstask.rb).


### Initializer options

**`config.assets.unknown_asset_fallback`**

When set to a truthy value, a result will be returned even if the requested asset is not found in the asset pipeline. When set to a falsey value it will raise an error when no asset is found in the pipeline. Defaults to `true`.

**`config.assets.precompile`**

Add additional assets to compile on deploy. Defaults to `application.js`, `application.css` and any other non-js/css file under `app/assets`.
Expand Down
19 changes: 18 additions & 1 deletion lib/sprockets/rails/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
module Sprockets
module Rails
module Helper
class AssetNotFound < StandardError; end

class AssetNotPrecompiled < StandardError
include Sprockets::Rails::Utils
def initialize(source)
Expand Down Expand Up @@ -33,7 +35,8 @@ def initialize(source)
:assets_environment, :assets_manifest,
:assets_precompile, :precompiled_asset_checker,
:assets_prefix, :digest_assets, :debug_assets,
:resolve_assets_with, :check_precompiled_asset
:resolve_assets_with, :check_precompiled_asset,
:unknown_asset_fallback
]

def self.included(klass)
Expand Down Expand Up @@ -68,12 +71,26 @@ def assets_environment
end
end

# Writes over the built in ActionView::Helpers::AssetUrlHelper#compute_asset_path
# to use the asset pipeline.
def compute_asset_path(path, options = {})
debug = options[:debug]

if asset_path = resolve_asset_path(path, debug)
File.join(assets_prefix || "/", legacy_debug_path(asset_path, debug))
else
message = "The asset #{ path.inspect } is not present in the asset pipeline."
raise AssetNotFound, message unless unknown_asset_fallback

if respond_to?(:public_compute_asset_path)
message << "Falling back to an asset that may be in the public folder.\n"
message << "This behavior is deprecated and will be removed.\n"
message << "To bypass the asset pipeline and preserve this behavior,\n"
message << "use the `skip_pipeline: true` option.\n"

call_stack = respond_to?(:caller_locations) ? caller_locations : caller
ActiveSupport::Deprecation.warn(message, call_stack)
end
super
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/sprockets/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def configure(&block)
config.assets.cache_limit = 50.megabytes
config.assets.gzip = true
config.assets.check_precompiled_asset = true
config.assets.unknown_asset_fallback = true

config.assets.configure do |env|
config.assets.paths.each { |path| env.append_path(path) }
Expand Down Expand Up @@ -245,7 +246,7 @@ def self.build_manifest(app)
self.resolve_assets_with = config.assets.resolve_with

self.check_precompiled_asset = config.assets.check_precompiled_asset

self.unknown_asset_fallback = config.assets.unknown_asset_fallback
# Expose the app precompiled asset check to the view
self.precompiled_asset_checker = -> logical_path { app.asset_precompiled? logical_path }
end
Expand Down
32 changes: 32 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def setup
@view.assets_precompile = %w( manifest.js )
precompiled_assets = @manifest.find(@view.assets_precompile).map(&:logical_path)
@view.check_precompiled_asset = true
@view.unknown_asset_fallback = 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 @@ -876,6 +877,37 @@ def test_index_files
end
end

class DeprecationTest < HelperTest
def test_deprecations_for_asset_path
@view.send(:define_singleton_method, :public_compute_asset_path, -> {})
assert_deprecated do
@view.asset_path("does_not_exist.noextension")
end
ensure
@view.instance_eval('undef :public_compute_asset_path')
end

def test_deprecations_for_asset_url
@view.send(:define_singleton_method, :public_compute_asset_path, -> {})

assert_deprecated do
@view.asset_url("does_not_exist.noextension")
end
ensure
@view.instance_eval('undef :public_compute_asset_path')
end

def test_deprecations_for_image_tag
@view.send(:define_singleton_method, :public_compute_asset_path, -> {})

assert_deprecated do
@view.image_tag("does_not_exist.noextension")
end
ensure
@view.instance_eval('undef :public_compute_asset_path')
end
end

class RaiseUnlessPrecompiledAssetDisabledTest < HelperTest
def test_check_precompiled_asset_enabled
@view.check_precompiled_asset = true
Expand Down

0 comments on commit e3dc15c

Please sign in to comment.