Skip to content

Commit d00b8ed

Browse files
committed
public_* API is not desired
- Use `public_folder: true` instead. - Allow configurable deprecation/error behavior via **`config.assets.unknown_asset_fallback`** When set to a truthy value, the a result will be returned even if the requested asset is not found in the asset pipeline. When set to `false` it will raise an error.
1 parent e2f615d commit d00b8ed

File tree

4 files changed

+19
-20
lines changed

4 files changed

+19
-20
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, the a result will be returned even if the requested asset is not found in the asset pipeline. When set to `false` it will raise an error.
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: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
module Sprockets
77
module Rails
88
module Helper
9+
class AssetNotFound < StandardError; end
910
class AssetNotPrecompiled < StandardError
1011
include Sprockets::Rails::Utils
1112
def initialize(source)
@@ -33,7 +34,8 @@ def initialize(source)
3334
:assets_environment, :assets_manifest,
3435
:assets_precompile, :precompiled_asset_checker,
3536
:assets_prefix, :digest_assets, :debug_assets,
36-
:resolve_assets_with, :check_precompiled_asset
37+
:resolve_assets_with, :check_precompiled_asset,
38+
:unknown_asset_fallback
3739
]
3840

3941
def self.included(klass)
@@ -76,27 +78,19 @@ def compute_asset_path(path, options = {})
7678
if asset_path = resolve_asset_path(path, debug)
7779
File.join(assets_prefix || "/", legacy_debug_path(asset_path, debug))
7880
else
79-
result = super
81+
message = "The asset #{ path.inspect } you are looking for is not present in the asset pipeline."
82+
raise AssetNotFound, message unless unknown_asset_fallback
83+
8084
if respond_to?(:public_compute_asset_path)
81-
throw(:asset_not_found, result)
82-
else
83-
result
84-
end
85-
end
86-
end
85+
message << "The public fallback behavior is being deprecaed and will be removed.\n"
86+
message << "pass in `public_folder: true` instead.\n"
8787

88-
# Writes over the built in ActionView::Helpers::AssetUrlHelper#asset_path
89-
# to use the asset pipeline.
90-
def asset_path(*args)
91-
catch_asset_not_found = catch(:asset_not_found) do
92-
return super(*args)
88+
call_stack = respond_to?(:caller_locations) ? caller_locations : caller
89+
ActiveSupport::Deprecation.warn(message, call_stack)
90+
end
91+
super
9392
end
94-
95-
result = catch_asset_not_found
96-
deprecate_invalid_asset_lookup(result, respond_to?(:caller_locations) ? caller_locations : caller)
97-
result
9893
end
99-
alias_method :path_to_asset, :asset_path # aliased to avoid conflicts with an asset_path named route
10094

10195
# Resolve the asset path against the Sprockets manifest or environment.
10296
# Returns nil if it's an asset we don't know about.

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: 1 addition & 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"

0 commit comments

Comments
 (0)