Skip to content

Commit e2f615d

Browse files
committed
public_* API is not desired
Use `public_folder: true` instead.
1 parent 5902abe commit e2f615d

File tree

2 files changed

+12
-45
lines changed

2 files changed

+12
-45
lines changed

lib/sprockets/rails/helper.rb

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def compute_asset_path(path, options = {})
7777
File.join(assets_prefix || "/", legacy_debug_path(asset_path, debug))
7878
else
7979
result = super
80-
if respond_to?(:public_asset_path)
80+
if respond_to?(:public_compute_asset_path)
8181
throw(:asset_not_found, result)
8282
else
8383
result
@@ -93,7 +93,7 @@ def asset_path(*args)
9393
end
9494

9595
result = catch_asset_not_found
96-
deprecate_invalid_asset_lookup(result, caller)
96+
deprecate_invalid_asset_lookup(result, respond_to?(:caller_locations) ? caller_locations : caller)
9797
result
9898
end
9999
alias_method :path_to_asset, :asset_path # aliased to avoid conflicts with an asset_path named route
@@ -266,43 +266,14 @@ def legacy_debug_path(path, debug)
266266
end
267267

268268
private
269-
# Attempts to extract a method name from a given caller line
270-
#
271-
# Example:
272-
#
273-
# extractd_method_from_call_frame('console.rb:65:in `start'') => 'start'
274-
def extract_method_from_call_frame(frame)
275-
frame.split("in ".freeze).last.gsub(/`|'/, ''.freeze)
276-
end
277-
278269
# Emits a deprecation warning when asset pipeline
279270
# is used with an asset that is not part of the pipeline.
280271
#
281272
# Attempts to determine proper method name based on caller.
282273
def deprecate_invalid_asset_lookup(name, call_stack)
283274
message = "The asset #{ name.inspect } you are looking for is not present in the asset pipeline.\n"
284275
message << "The public fallback behavior is being deprecated and will be removed.\n"
285-
286-
append = nil
287-
288-
# We need to figure out the actually deprecated method. This is complicated since
289-
# the deprecation always comes from `asset_path` but that might not be the API that
290-
# is being used. To solve this we look at the call stack methods backwards and figure out the first
291-
# `*_url`, `*_tag` or `*_path` method that is called. If that method also has a `public_*`
292-
# counterpart it is the first method that was called that could be deprecated.
293-
call_stack.reverse_each.detect.with_index do |call_frame, index|
294-
method_name = extract_method_from_call_frame(call_frame)
295-
next if !method_name.end_with?("_url".freeze) &&
296-
!method_name.end_with?("_tag".freeze) &&
297-
!method_name.end_with?("_path".freeze)
298-
299-
if self.respond_to?("public_#{ method_name }")
300-
append = "please use the `public_*` helper instead. For example `#{ "public_#{ method_name }" }`.\n"
301-
call_stack.shift(call_stack.length - index - 1)
302-
end
303-
end
304-
append ||= "please use the `public_*` helper instead for example `public_asset_path`.\n"
305-
message << append
276+
message << "pass in `public_folder: true` instead.\n"
306277

307278
ActiveSupport::Deprecation.warn(message, call_stack)
308279
end

test/test_helper.rb

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -829,36 +829,32 @@ def test_index_files
829829

830830
class DeprecationTest < HelperTest
831831
def test_deprecations_for_asset_path
832-
@view.send(:define_singleton_method, :public_asset_path, -> {})
833-
assert_deprecated(/public_asset_path/) do
832+
@view.send(:define_singleton_method, :public_compute_asset_path, -> {})
833+
assert_deprecated do
834834
@view.asset_path("does_not_exist.noextension")
835835
end
836836
ensure
837-
@view.instance_eval('undef :public_asset_path')
837+
@view.instance_eval('undef :public_compute_asset_path')
838838
end
839839

840840
def test_deprecations_for_asset_url
841-
@view.send(:define_singleton_method, :public_asset_path, -> {})
842-
@view.send(:define_singleton_method, :public_asset_url, -> {})
841+
@view.send(:define_singleton_method, :public_compute_asset_path, -> {})
843842

844-
assert_deprecated(/public_asset_url/) do
843+
assert_deprecated do
845844
@view.asset_url("does_not_exist.noextension")
846845
end
847846
ensure
848-
@view.instance_eval('undef :public_asset_path')
849-
@view.instance_eval('undef :public_asset_url')
847+
@view.instance_eval('undef :public_compute_asset_path')
850848
end
851849

852850
def test_deprecations_for_image_tag
853-
@view.send(:define_singleton_method, :public_asset_path, -> {})
854-
@view.send(:define_singleton_method, :public_image_tag, -> {})
851+
@view.send(:define_singleton_method, :public_compute_asset_path, -> {})
855852

856-
assert_deprecated(/public_image_tag/) do
853+
assert_deprecated do
857854
@view.image_tag("does_not_exist.noextension")
858855
end
859856
ensure
860-
@view.instance_eval('undef :public_asset_path')
861-
@view.instance_eval('undef :public_image_tag')
857+
@view.instance_eval('undef :public_compute_asset_path')
862858
end
863859
end
864860

0 commit comments

Comments
 (0)