Skip to content

Commit e3dc15c

Browse files
authored
Merge pull request #375 from rails/schneems/deprecate-fallback-behavior
Deprecate asset fallback
2 parents 1153896 + 09730f9 commit e3dc15c

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,12 @@ Each asset task will invoke `assets:environment` first. By default this loads th
4848

4949
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).
5050

51-
5251
### Initializer options
5352

53+
**`config.assets.unknown_asset_fallback`**
54+
55+
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`.
56+
5457
**`config.assets.precompile`**
5558

5659
Add additional assets to compile on deploy. Defaults to `application.js`, `application.css` and any other non-js/css file under `app/assets`.

lib/sprockets/rails/helper.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
module Sprockets
77
module Rails
88
module Helper
9+
class AssetNotFound < StandardError; end
10+
911
class AssetNotPrecompiled < StandardError
1012
include Sprockets::Rails::Utils
1113
def initialize(source)
@@ -33,7 +35,8 @@ def initialize(source)
3335
:assets_environment, :assets_manifest,
3436
:assets_precompile, :precompiled_asset_checker,
3537
:assets_prefix, :digest_assets, :debug_assets,
36-
:resolve_assets_with, :check_precompiled_asset
38+
:resolve_assets_with, :check_precompiled_asset,
39+
:unknown_asset_fallback
3740
]
3841

3942
def self.included(klass)
@@ -68,12 +71,26 @@ def assets_environment
6871
end
6972
end
7073

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

7479
if asset_path = resolve_asset_path(path, debug)
7580
File.join(assets_prefix || "/", legacy_debug_path(asset_path, debug))
7681
else
82+
message = "The asset #{ path.inspect } is not present in the asset pipeline."
83+
raise AssetNotFound, message unless unknown_asset_fallback
84+
85+
if respond_to?(:public_compute_asset_path)
86+
message << "Falling back to an asset that may be in the public folder.\n"
87+
message << "This behavior is deprecated and will be removed.\n"
88+
message << "To bypass the asset pipeline and preserve this behavior,\n"
89+
message << "use the `skip_pipeline: true` option.\n"
90+
91+
call_stack = respond_to?(:caller_locations) ? caller_locations : caller
92+
ActiveSupport::Deprecation.warn(message, call_stack)
93+
end
7794
super
7895
end
7996
end

lib/sprockets/railtie.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ def configure(&block)
122122
config.assets.cache_limit = 50.megabytes
123123
config.assets.gzip = true
124124
config.assets.check_precompiled_asset = true
125+
config.assets.unknown_asset_fallback = true
125126

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

247248
self.check_precompiled_asset = config.assets.check_precompiled_asset
248-
249+
self.unknown_asset_fallback = config.assets.unknown_asset_fallback
249250
# Expose the app precompiled asset check to the view
250251
self.precompiled_asset_checker = -> logical_path { app.asset_precompiled? logical_path }
251252
end

test/test_helper.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def setup
2828
@view.assets_precompile = %w( manifest.js )
2929
precompiled_assets = @manifest.find(@view.assets_precompile).map(&:logical_path)
3030
@view.check_precompiled_asset = true
31+
@view.unknown_asset_fallback = true
3132
@view.precompiled_asset_checker = -> logical_path { precompiled_assets.include? logical_path }
3233
@view.request = ActionDispatch::Request.new({
3334
"rack.url_scheme" => "https"
@@ -876,6 +877,37 @@ def test_index_files
876877
end
877878
end
878879

880+
class DeprecationTest < HelperTest
881+
def test_deprecations_for_asset_path
882+
@view.send(:define_singleton_method, :public_compute_asset_path, -> {})
883+
assert_deprecated do
884+
@view.asset_path("does_not_exist.noextension")
885+
end
886+
ensure
887+
@view.instance_eval('undef :public_compute_asset_path')
888+
end
889+
890+
def test_deprecations_for_asset_url
891+
@view.send(:define_singleton_method, :public_compute_asset_path, -> {})
892+
893+
assert_deprecated do
894+
@view.asset_url("does_not_exist.noextension")
895+
end
896+
ensure
897+
@view.instance_eval('undef :public_compute_asset_path')
898+
end
899+
900+
def test_deprecations_for_image_tag
901+
@view.send(:define_singleton_method, :public_compute_asset_path, -> {})
902+
903+
assert_deprecated do
904+
@view.image_tag("does_not_exist.noextension")
905+
end
906+
ensure
907+
@view.instance_eval('undef :public_compute_asset_path')
908+
end
909+
end
910+
879911
class RaiseUnlessPrecompiledAssetDisabledTest < HelperTest
880912
def test_check_precompiled_asset_enabled
881913
@view.check_precompiled_asset = true

0 commit comments

Comments
 (0)