mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Allow a more explicit public behavior
We want to make it more explicit when a user wants to avoid the asset pipeline to do this we will add `public_*` methods to all path helpers. So if someone wants to use an asset that isn't maintained by the asset pipeline they can use `public_asset_path` instead of `asset_path` and letting it fall through. The main reason for this change is the desire to raise helpful errors in the future. Right now if you typo an asset name, then we assume you intended an asset in the `public/` folder and not in the pipeline so nothing fails and the error sits silently until you realize the page didn't render correctly. We have to deprecate today so we can raise meaningful error messages in the future.
This commit is contained in:
parent
3fc414a019
commit
5de741c4cd
1 changed files with 37 additions and 2 deletions
|
@ -141,8 +141,17 @@ module ActionView
|
|||
source = "#{source}#{extname}"
|
||||
end
|
||||
|
||||
if source[0] != ?/
|
||||
source = compute_asset_path(source, options)
|
||||
if options[:public_folder]
|
||||
source = public_compute_asset_path(source, options) unless options[:raw]
|
||||
else
|
||||
if source[0] != ?/
|
||||
source = compute_asset_path(source, options) unless options[:raw]
|
||||
elsif !options[:raw]
|
||||
message = "Skipping computing asset path since asset #{ source.inspect } starts with a slash `/`.\n"
|
||||
message << "This behavior is deprecated and will be removed. Instead explicitly\n"
|
||||
message << "use a `public_*` helper instead. Optionally pass in `raw: true` to get the exact same behavior."
|
||||
ActiveSupport::Deprecation.warn(message)
|
||||
end
|
||||
end
|
||||
|
||||
relative_url_root = defined?(config.relative_url_root) && config.relative_url_root
|
||||
|
@ -158,6 +167,11 @@ module ActionView
|
|||
end
|
||||
alias_method :path_to_asset, :asset_path # aliased to avoid conflicts with an asset_path named route
|
||||
|
||||
|
||||
def public_asset_path(source, options = {})
|
||||
path_to_asset(source, {public_folder: true}.merge!(options))
|
||||
end
|
||||
|
||||
# Computes the full URL to an asset in the public directory. This
|
||||
# will use +asset_path+ internally, so most of their behaviors
|
||||
# will be the same. If :host options is set, it overwrites global
|
||||
|
@ -203,6 +217,7 @@ module ActionView
|
|||
dir = ASSET_PUBLIC_DIRECTORIES[options[:type]] || ""
|
||||
File.join(dir, source)
|
||||
end
|
||||
alias :public_compute_asset_path :compute_asset_path
|
||||
|
||||
# Pick an asset host for this source. Returns +nil+ if no host is set,
|
||||
# the host if no wildcard is set, the host interpolated with the
|
||||
|
@ -258,6 +273,9 @@ module ActionView
|
|||
end
|
||||
alias_method :path_to_javascript, :javascript_path # aliased to avoid conflicts with a javascript_path named route
|
||||
|
||||
def public_javascript_path(source, options = {})
|
||||
path_to_javascript(source, {public_folder: true}.merge!(options))
|
||||
end
|
||||
# Computes the full URL to a JavaScript asset in the public javascripts directory.
|
||||
# This will use +javascript_path+ internally, so most of their behaviors will be the same.
|
||||
# Since +javascript_url+ is based on +asset_url+ method you can set :host options. If :host
|
||||
|
@ -285,6 +303,10 @@ module ActionView
|
|||
end
|
||||
alias_method :path_to_stylesheet, :stylesheet_path # aliased to avoid conflicts with a stylesheet_path named route
|
||||
|
||||
def public_stylesheet_path(source, options)
|
||||
path_to_stylesheet(source, {public_folder: true}.merge!(options))
|
||||
end
|
||||
|
||||
# Computes the full URL to a stylesheet asset in the public stylesheets directory.
|
||||
# This will use +stylesheet_path+ internally, so most of their behaviors will be the same.
|
||||
# Since +stylesheet_url+ is based on +asset_url+ method you can set :host options. If :host
|
||||
|
@ -315,6 +337,9 @@ module ActionView
|
|||
end
|
||||
alias_method :path_to_image, :image_path # aliased to avoid conflicts with an image_path named route
|
||||
|
||||
def public_image_path(source, options)
|
||||
path_to_image(source, {public_folder: true}.merge!(options))
|
||||
end
|
||||
# Computes the full URL to an image asset.
|
||||
# This will use +image_path+ internally, so most of their behaviors will be the same.
|
||||
# Since +image_url+ is based on +asset_url+ method you can set :host options. If :host
|
||||
|
@ -341,6 +366,10 @@ module ActionView
|
|||
end
|
||||
alias_method :path_to_video, :video_path # aliased to avoid conflicts with a video_path named route
|
||||
|
||||
def public_video_path(source, options)
|
||||
path_to_video(source, {public_folder: true}.merge!(options))
|
||||
end
|
||||
|
||||
# Computes the full URL to a video asset in the public videos directory.
|
||||
# This will use +video_path+ internally, so most of their behaviors will be the same.
|
||||
# Since +video_url+ is based on +asset_url+ method you can set :host options. If :host
|
||||
|
@ -367,6 +396,9 @@ module ActionView
|
|||
end
|
||||
alias_method :path_to_audio, :audio_path # aliased to avoid conflicts with an audio_path named route
|
||||
|
||||
def public_audio_path(source, options = {})
|
||||
path_to_audio(source, {public_folder: true}.merge!(options))
|
||||
end
|
||||
# Computes the full URL to an audio asset in the public audios directory.
|
||||
# This will use +audio_path+ internally, so most of their behaviors will be the same.
|
||||
# Since +audio_url+ is based on +asset_url+ method you can set :host options. If :host
|
||||
|
@ -392,6 +424,9 @@ module ActionView
|
|||
end
|
||||
alias_method :path_to_font, :font_path # aliased to avoid conflicts with an font_path named route
|
||||
|
||||
def public_font_path(source, options = {})
|
||||
path_to_font(source, {public_folder: true}.merge!(options))
|
||||
end
|
||||
# Computes the full URL to a font asset.
|
||||
# This will use +font_path+ internally, so most of their behaviors will be the same.
|
||||
# Since +font_url+ is based on +asset_url+ method you can set :host options. If :host
|
||||
|
|
Loading…
Reference in a new issue