From 5de741c4cd4528fd86cae1091cdea984449cc926 Mon Sep 17 00:00:00 2001 From: schneems Date: Wed, 15 Jun 2016 11:05:04 -0500 Subject: 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. --- .../lib/action_view/helpers/asset_url_helper.rb | 39 ++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/actionview/lib/action_view/helpers/asset_url_helper.rb b/actionview/lib/action_view/helpers/asset_url_helper.rb index 76a4893f2e..e925a8184c 100644 --- a/actionview/lib/action_view/helpers/asset_url_helper.rb +++ b/actionview/lib/action_view/helpers/asset_url_helper.rb @@ -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 -- cgit v1.2.3 From 4b84ef6d8bdad08a4b9860c6545a08f26a963d34 Mon Sep 17 00:00:00 2001 From: schneems Date: Wed, 15 Jun 2016 11:36:42 -0500 Subject: Make sure routes don't interfere with paths --- actionview/lib/action_view/helpers/asset_url_helper.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/actionview/lib/action_view/helpers/asset_url_helper.rb b/actionview/lib/action_view/helpers/asset_url_helper.rb index e925a8184c..61e87e6f78 100644 --- a/actionview/lib/action_view/helpers/asset_url_helper.rb +++ b/actionview/lib/action_view/helpers/asset_url_helper.rb @@ -171,6 +171,7 @@ module ActionView def public_asset_path(source, options = {}) path_to_asset(source, {public_folder: true}.merge!(options)) end + alias_method :path_to_public_asset, :public_asset_path # Computes the full URL to an asset in the public directory. This # will use +asset_path+ internally, so most of their behaviors @@ -276,6 +277,8 @@ module ActionView def public_javascript_path(source, options = {}) path_to_javascript(source, {public_folder: true}.merge!(options)) end + alias_method :path_to_public_javascript, :public_javascript_path + # 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 @@ -306,6 +309,7 @@ module ActionView def public_stylesheet_path(source, options) path_to_stylesheet(source, {public_folder: true}.merge!(options)) end + alias_method :path_to_public_stylesheet, :public_stylesheet_path # 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. @@ -340,6 +344,8 @@ module ActionView def public_image_path(source, options) path_to_image(source, {public_folder: true}.merge!(options)) end + alias_method :path_to_public_image, :public_image_path + # 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 @@ -369,6 +375,7 @@ module ActionView def public_video_path(source, options) path_to_video(source, {public_folder: true}.merge!(options)) end + alias_method :path_to_public_video, :public_video_path # 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. @@ -399,6 +406,8 @@ module ActionView def public_audio_path(source, options = {}) path_to_audio(source, {public_folder: true}.merge!(options)) end + alias_method :path_to_public_audio, :public_audio_path + # 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 @@ -427,6 +436,8 @@ module ActionView def public_font_path(source, options = {}) path_to_font(source, {public_folder: true}.merge!(options)) end + alias_method :path_to_public_font, :public_font_path + # 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 -- cgit v1.2.3 From 5b576af97efe7ff5375572ad325b18429e9a75a8 Mon Sep 17 00:00:00 2001 From: schneems Date: Wed, 15 Jun 2016 11:39:55 -0500 Subject: Add descriptive comment --- actionview/lib/action_view/helpers/asset_url_helper.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/actionview/lib/action_view/helpers/asset_url_helper.rb b/actionview/lib/action_view/helpers/asset_url_helper.rb index 61e87e6f78..8db65adc5d 100644 --- a/actionview/lib/action_view/helpers/asset_url_helper.rb +++ b/actionview/lib/action_view/helpers/asset_url_helper.rb @@ -171,7 +171,7 @@ module ActionView def public_asset_path(source, options = {}) path_to_asset(source, {public_folder: true}.merge!(options)) end - alias_method :path_to_public_asset, :public_asset_path + alias_method :path_to_public_asset, :public_asset_path # aliased to avoid conflicts with an font_path named route # Computes the full URL to an asset in the public directory. This # will use +asset_path+ internally, so most of their behaviors @@ -277,7 +277,7 @@ module ActionView def public_javascript_path(source, options = {}) path_to_javascript(source, {public_folder: true}.merge!(options)) end - alias_method :path_to_public_javascript, :public_javascript_path + alias_method :path_to_public_javascript, :public_javascript_path # aliased to avoid conflicts with an font_path named route # 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. @@ -309,7 +309,7 @@ module ActionView def public_stylesheet_path(source, options) path_to_stylesheet(source, {public_folder: true}.merge!(options)) end - alias_method :path_to_public_stylesheet, :public_stylesheet_path + alias_method :path_to_public_stylesheet, :public_stylesheet_path # aliased to avoid conflicts with an font_path named route # 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. @@ -344,7 +344,7 @@ module ActionView def public_image_path(source, options) path_to_image(source, {public_folder: true}.merge!(options)) end - alias_method :path_to_public_image, :public_image_path + alias_method :path_to_public_image, :public_image_path # aliased to avoid conflicts with an font_path named route # Computes the full URL to an image asset. # This will use +image_path+ internally, so most of their behaviors will be the same. @@ -375,7 +375,7 @@ module ActionView def public_video_path(source, options) path_to_video(source, {public_folder: true}.merge!(options)) end - alias_method :path_to_public_video, :public_video_path + alias_method :path_to_public_video, :public_video_path # aliased to avoid conflicts with an font_path named route # 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. @@ -406,7 +406,7 @@ module ActionView def public_audio_path(source, options = {}) path_to_audio(source, {public_folder: true}.merge!(options)) end - alias_method :path_to_public_audio, :public_audio_path + alias_method :path_to_public_audio, :public_audio_path # aliased to avoid conflicts with an font_path named route # 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. @@ -436,7 +436,7 @@ module ActionView def public_font_path(source, options = {}) path_to_font(source, {public_folder: true}.merge!(options)) end - alias_method :path_to_public_font, :public_font_path + alias_method :path_to_public_font, :public_font_path # aliased to avoid conflicts with an font_path named route # Computes the full URL to a font asset. # This will use +font_path+ internally, so most of their behaviors will be the same. -- cgit v1.2.3 From e4b233a2523960b604d8829a5a23d329fba52d23 Mon Sep 17 00:00:00 2001 From: schneems Date: Wed, 15 Jun 2016 12:14:32 -0500 Subject: Add public URL helpers --- .../lib/action_view/helpers/asset_url_helper.rb | 56 ++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/actionview/lib/action_view/helpers/asset_url_helper.rb b/actionview/lib/action_view/helpers/asset_url_helper.rb index 8db65adc5d..bd91ace5c8 100644 --- a/actionview/lib/action_view/helpers/asset_url_helper.rb +++ b/actionview/lib/action_view/helpers/asset_url_helper.rb @@ -188,6 +188,14 @@ module ActionView end alias_method :url_to_asset, :asset_url # aliased to avoid conflicts with an asset_url named route + # Computes the full URL to a asset in the public folder. + # This uses +asset_url+ and skips any asset lookups by assuming the asset is in the + # `public` folder. + def public_asset_url(source, options = {}) + url_to_asset(source, {public_folder: true}.merge!(options)) + end + alias_method :path_to_public_asset, :public_asset_path # aliased to avoid conflicts with an asset_path named route + ASSET_EXTENSIONS = { javascript: ".js", stylesheet: ".css" @@ -291,6 +299,14 @@ module ActionView end alias_method :url_to_javascript, :javascript_url # aliased to avoid conflicts with a javascript_url named route + # Computes the full URL to a javascript asset in the public folder. + # This uses +javascript_url+ and skips any asset lookups by assuming the asset is in the + # `public` folder. + def public_javascript_url(source, options = {}) + url_to_javascript(source, {public_folder: true}.merge!(options)) + end + alias_method :path_to_public_javascript, :public_javascript_path # aliased to avoid conflicts with an javascript_path named route + # Computes the path to a stylesheet asset in the public stylesheets directory. # If the +source+ filename has no extension, .css will be appended (except for explicit URIs). # Full paths from the document root will be passed through. @@ -323,6 +339,14 @@ module ActionView end alias_method :url_to_stylesheet, :stylesheet_url # aliased to avoid conflicts with a stylesheet_url named route + # Computes the full URL to a stylesheet asset in the public folder. + # This uses +stylesheet_url+ and skips any asset lookups by assuming the asset is in the + # `public` folder. + def public_stylesheet_url(source, options = {}) + url_to_stylesheet(source, {public_folder: true}.merge!(options)) + end + alias_method :path_to_public_stylesheet, :public_stylesheet_path # aliased to avoid conflicts with an stylesheet_path named route + # Computes the path to an image asset. # Full paths from the document root will be passed through. # Used internally by +image_tag+ to build the image path: @@ -358,6 +382,14 @@ module ActionView end alias_method :url_to_image, :image_url # aliased to avoid conflicts with an image_url named route + # Computes the full URL to a image asset in the public folder. + # This uses +image_url+ and skips any asset lookups by assuming the asset is in the + # `public` folder. + def public_image_url(source, options = {}) + url_to_image(source, {public_folder: true}.merge!(options)) + end + alias_method :path_to_public_image, :public_image_path # aliased to avoid conflicts with an image_path named route + # Computes the path to a video asset in the public videos directory. # Full paths from the document root will be passed through. # Used internally by +video_tag+ to build the video path. @@ -389,6 +421,14 @@ module ActionView end alias_method :url_to_video, :video_url # aliased to avoid conflicts with an video_url named route + # Computes the full URL to a video asset in the public folder. + # This uses +video_url+ and skips any asset lookups by assuming the asset is in the + # `public` folder. + def public_video_url(source, options = {}) + url_to_video(source, {public_folder: true}.merge!(options)) + end + alias_method :path_to_public_video, :public_video_path # aliased to avoid conflicts with an video_path named route + # Computes the path to an audio asset in the public audios directory. # Full paths from the document root will be passed through. # Used internally by +audio_tag+ to build the audio path. @@ -420,6 +460,14 @@ module ActionView end alias_method :url_to_audio, :audio_url # aliased to avoid conflicts with an audio_url named route + # Computes the full URL to a audio asset in the public folder. + # This uses +audio_url+ and skips any asset lookups by assuming the asset is in the + # `public` folder. + def public_audio_url(source, options = {}) + url_to_audio(source, {public_folder: true}.merge!(options)) + end + alias_method :path_to_public_audio, :public_audio_path # aliased to avoid conflicts with an audio_path named route + # Computes the path to a font asset. # Full paths from the document root will be passed through. # @@ -449,6 +497,14 @@ module ActionView url_to_asset(source, { type: :font }.merge!(options)) end alias_method :url_to_font, :font_url # aliased to avoid conflicts with an font_url named route + + # Computes the full URL to a font asset in the public folder. + # This uses +font_url+ and skips any asset lookups by assuming the asset is in the + # `public` folder. + def public_font_url(source, options = {}) + url_to_font(source, {public_folder: true}.merge!(options)) + end + alias_method :path_to_public_font, :public_font_path # aliased to avoid conflicts with an public_font_url named route end end end -- cgit v1.2.3 From b8905f3c85e0bdd96c01ca181eda91705db0bc74 Mon Sep 17 00:00:00 2001 From: schneems Date: Wed, 15 Jun 2016 12:21:19 -0500 Subject: Add docs to `public_*_path` methods & fix comments --- .../lib/action_view/helpers/asset_url_helper.rb | 51 +++++++++++++++------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/actionview/lib/action_view/helpers/asset_url_helper.rb b/actionview/lib/action_view/helpers/asset_url_helper.rb index bd91ace5c8..844826386a 100644 --- a/actionview/lib/action_view/helpers/asset_url_helper.rb +++ b/actionview/lib/action_view/helpers/asset_url_helper.rb @@ -168,10 +168,13 @@ module ActionView alias_method :path_to_asset, :asset_path # aliased to avoid conflicts with an asset_path named route + # Computes the path to an asset in the public folder. + # This uses +asset_path+ and skips any asset lookups by assuming the asset is in the + # `public` folder. def public_asset_path(source, options = {}) path_to_asset(source, {public_folder: true}.merge!(options)) end - alias_method :path_to_public_asset, :public_asset_path # aliased to avoid conflicts with an font_path named route + alias_method :path_to_public_asset, :public_asset_path # aliased to avoid conflicts with an public_asset_path named route # Computes the full URL to an asset in the public directory. This # will use +asset_path+ internally, so most of their behaviors @@ -188,13 +191,13 @@ module ActionView end alias_method :url_to_asset, :asset_url # aliased to avoid conflicts with an asset_url named route - # Computes the full URL to a asset in the public folder. + # Computes the full URL to an asset in the public folder. # This uses +asset_url+ and skips any asset lookups by assuming the asset is in the # `public` folder. def public_asset_url(source, options = {}) url_to_asset(source, {public_folder: true}.merge!(options)) end - alias_method :path_to_public_asset, :public_asset_path # aliased to avoid conflicts with an asset_path named route + alias_method :path_to_public_asset, :public_asset_path # aliased to avoid conflicts with a public_asset_path named route ASSET_EXTENSIONS = { javascript: ".js", @@ -282,10 +285,13 @@ module ActionView end alias_method :path_to_javascript, :javascript_path # aliased to avoid conflicts with a javascript_path named route + # Computes the path to a javascript asset in the public folder. + # This uses +javascript_path+ and skips any asset lookups by assuming the asset is in the + # `public` folder. def public_javascript_path(source, options = {}) path_to_javascript(source, {public_folder: true}.merge!(options)) end - alias_method :path_to_public_javascript, :public_javascript_path # aliased to avoid conflicts with an font_path named route + alias_method :path_to_public_javascript, :public_javascript_path # aliased to avoid conflicts with a public_javascript_path named route # 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. @@ -305,7 +311,7 @@ module ActionView def public_javascript_url(source, options = {}) url_to_javascript(source, {public_folder: true}.merge!(options)) end - alias_method :path_to_public_javascript, :public_javascript_path # aliased to avoid conflicts with an javascript_path named route + alias_method :path_to_public_javascript, :public_javascript_path # aliased to avoid conflicts with a public_javascript_path named route # Computes the path to a stylesheet asset in the public stylesheets directory. # If the +source+ filename has no extension, .css will be appended (except for explicit URIs). @@ -322,10 +328,13 @@ module ActionView end alias_method :path_to_stylesheet, :stylesheet_path # aliased to avoid conflicts with a stylesheet_path named route + # Computes the path to a stylesheet asset in the public folder. + # This uses +stylesheet_path+ and skips any asset lookups by assuming the asset is in the + # `public` folder. def public_stylesheet_path(source, options) path_to_stylesheet(source, {public_folder: true}.merge!(options)) end - alias_method :path_to_public_stylesheet, :public_stylesheet_path # aliased to avoid conflicts with an font_path named route + alias_method :path_to_public_stylesheet, :public_stylesheet_path # aliased to avoid conflicts with a public_stylesheet_path named route # 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. @@ -345,7 +354,7 @@ module ActionView def public_stylesheet_url(source, options = {}) url_to_stylesheet(source, {public_folder: true}.merge!(options)) end - alias_method :path_to_public_stylesheet, :public_stylesheet_path # aliased to avoid conflicts with an stylesheet_path named route + alias_method :path_to_public_stylesheet, :public_stylesheet_path # aliased to avoid conflicts with a public_stylesheet_path named route # Computes the path to an image asset. # Full paths from the document root will be passed through. @@ -365,10 +374,13 @@ module ActionView end alias_method :path_to_image, :image_path # aliased to avoid conflicts with an image_path named route + # Computes the path to a image asset in the public folder. + # This uses +image_path+ and skips any asset lookups by assuming the asset is in the + # `public` folder. def public_image_path(source, options) path_to_image(source, {public_folder: true}.merge!(options)) end - alias_method :path_to_public_image, :public_image_path # aliased to avoid conflicts with an font_path named route + alias_method :path_to_public_image, :public_image_path # aliased to avoid conflicts with a public_image_path named route # Computes the full URL to an image asset. # This will use +image_path+ internally, so most of their behaviors will be the same. @@ -388,7 +400,7 @@ module ActionView def public_image_url(source, options = {}) url_to_image(source, {public_folder: true}.merge!(options)) end - alias_method :path_to_public_image, :public_image_path # aliased to avoid conflicts with an image_path named route + alias_method :path_to_public_image, :public_image_path # aliased to avoid conflicts with a public_image_path named route # Computes the path to a video asset in the public videos directory. # Full paths from the document root will be passed through. @@ -404,10 +416,13 @@ module ActionView end alias_method :path_to_video, :video_path # aliased to avoid conflicts with a video_path named route + # Computes the path to a video asset in the public folder. + # This uses +video_path+ and skips any asset lookups by assuming the asset is in the + # `public` folder. def public_video_path(source, options) path_to_video(source, {public_folder: true}.merge!(options)) end - alias_method :path_to_public_video, :public_video_path # aliased to avoid conflicts with an font_path named route + alias_method :path_to_public_video, :public_video_path # aliased to avoid conflicts with a public_video_path named route # 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. @@ -427,7 +442,7 @@ module ActionView def public_video_url(source, options = {}) url_to_video(source, {public_folder: true}.merge!(options)) end - alias_method :path_to_public_video, :public_video_path # aliased to avoid conflicts with an video_path named route + alias_method :path_to_public_video, :public_video_path # aliased to avoid conflicts with a public_video_path named route # Computes the path to an audio asset in the public audios directory. # Full paths from the document root will be passed through. @@ -443,10 +458,13 @@ module ActionView end alias_method :path_to_audio, :audio_path # aliased to avoid conflicts with an audio_path named route + # Computes the path to a audio asset in the public folder. + # This uses +audio_path+ and skips any asset lookups by assuming the asset is in the + # `public` folder. def public_audio_path(source, options = {}) path_to_audio(source, {public_folder: true}.merge!(options)) end - alias_method :path_to_public_audio, :public_audio_path # aliased to avoid conflicts with an font_path named route + alias_method :path_to_public_audio, :public_audio_path # aliased to avoid conflicts with a public_audio_path named route # 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. @@ -466,7 +484,7 @@ module ActionView def public_audio_url(source, options = {}) url_to_audio(source, {public_folder: true}.merge!(options)) end - alias_method :path_to_public_audio, :public_audio_path # aliased to avoid conflicts with an audio_path named route + alias_method :path_to_public_audio, :public_audio_path # aliased to avoid conflicts with a public_audio_path named route # Computes the path to a font asset. # Full paths from the document root will be passed through. @@ -481,10 +499,13 @@ module ActionView end alias_method :path_to_font, :font_path # aliased to avoid conflicts with an font_path named route + # Computes the path to a font asset in the public folder. + # This uses +font_path+ and skips any asset lookups by assuming the asset is in the + # `public` folder. def public_font_path(source, options = {}) path_to_font(source, {public_folder: true}.merge!(options)) end - alias_method :path_to_public_font, :public_font_path # aliased to avoid conflicts with an font_path named route + alias_method :path_to_public_font, :public_font_path # aliased to avoid conflicts with a public_font_path named route # Computes the full URL to a font asset. # This will use +font_path+ internally, so most of their behaviors will be the same. @@ -504,7 +525,7 @@ module ActionView def public_font_url(source, options = {}) url_to_font(source, {public_folder: true}.merge!(options)) end - alias_method :path_to_public_font, :public_font_path # aliased to avoid conflicts with an public_font_url named route + alias_method :path_to_public_font, :public_font_path # aliased to avoid conflicts with a public_font_url named route end end end -- cgit v1.2.3 From e8b081133a6780b37d1143becf4cfc945c41cb49 Mon Sep 17 00:00:00 2001 From: schneems Date: Wed, 15 Jun 2016 14:16:10 -0500 Subject: Match method signature --- actionview/lib/action_view/helpers/asset_url_helper.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/actionview/lib/action_view/helpers/asset_url_helper.rb b/actionview/lib/action_view/helpers/asset_url_helper.rb index 844826386a..26ba955f07 100644 --- a/actionview/lib/action_view/helpers/asset_url_helper.rb +++ b/actionview/lib/action_view/helpers/asset_url_helper.rb @@ -331,7 +331,7 @@ module ActionView # Computes the path to a stylesheet asset in the public folder. # This uses +stylesheet_path+ and skips any asset lookups by assuming the asset is in the # `public` folder. - def public_stylesheet_path(source, options) + def public_stylesheet_path(source, options = {}) path_to_stylesheet(source, {public_folder: true}.merge!(options)) end alias_method :path_to_public_stylesheet, :public_stylesheet_path # aliased to avoid conflicts with a public_stylesheet_path named route @@ -377,7 +377,7 @@ module ActionView # Computes the path to a image asset in the public folder. # This uses +image_path+ and skips any asset lookups by assuming the asset is in the # `public` folder. - def public_image_path(source, options) + def public_image_path(source, options = {}) path_to_image(source, {public_folder: true}.merge!(options)) end alias_method :path_to_public_image, :public_image_path # aliased to avoid conflicts with a public_image_path named route @@ -419,7 +419,7 @@ module ActionView # Computes the path to a video asset in the public folder. # This uses +video_path+ and skips any asset lookups by assuming the asset is in the # `public` folder. - def public_video_path(source, options) + def public_video_path(source, options = {}) path_to_video(source, {public_folder: true}.merge!(options)) end alias_method :path_to_public_video, :public_video_path # aliased to avoid conflicts with a public_video_path named route -- cgit v1.2.3 From 0a633b324eb8239a467fa7fbb9fa18abcd5759d3 Mon Sep 17 00:00:00 2001 From: schneems Date: Wed, 15 Jun 2016 14:16:21 -0500 Subject: Test `public_` methods. --- railties/test/application/asset_debugging_test.rb | 72 +++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/railties/test/application/asset_debugging_test.rb b/railties/test/application/asset_debugging_test.rb index a732869d62..92d10ec827 100644 --- a/railties/test/application/asset_debugging_test.rb +++ b/railties/test/application/asset_debugging_test.rb @@ -68,5 +68,77 @@ module ApplicationTests assert_match(/ def javascript_include_tag(*sources) options = sources.extract_options!.stringify_keys - path_options = options.extract!("protocol", "extname", "host").symbolize_keys + path_options = options.extract!("protocol", "extname", "host", "public_folder").symbolize_keys sources.uniq.map { |source| tag_options = { "src" => path_to_javascript(source, path_options) @@ -65,6 +65,14 @@ module ActionView }.join("\n").html_safe end + # Computes the path to a series of javascript assets in the public + # folder. This uses +javascript_include_tag+ and skips any asset + # lookups by assuming any assets are in the `public` folder. + def public_javascript_include_tag(*sources) + options = sources.extract_options! + javascript_include_tag(*sources, { public_folder: true }.merge!(options)) + end + # Returns a stylesheet link tag for the sources specified as arguments. If # you don't specify an extension, .css will be appended automatically. # You can modify the link attributes by passing a hash as the last argument. @@ -92,8 +100,7 @@ module ActionView # # def stylesheet_link_tag(*sources) options = sources.extract_options!.stringify_keys - path_options = options.extract!("protocol", "host").symbolize_keys - + path_options = options.extract!("protocol", "host", "public_folder").symbolize_keys sources.uniq.map { |source| tag_options = { "rel" => "stylesheet", @@ -104,6 +111,14 @@ module ActionView }.join("\n").html_safe end + # Computes the path to a series of stylesheet assets in the public + # folder. This uses +stylesheet_link_tag+ and skips any asset + # lookups by assuming any assets are in the `public` folder. + def public_stylesheet_link_tag(*sources) + options = sources.extract_options! + stylesheet_link_tag(*sources, { public_folder: true }.merge!(options)) + end + # Returns a link tag that browsers and feed readers can use to auto-detect # an RSS or Atom feed. The +type+ can either be :rss (default) or # :atom. Control the link options in url_for format using the @@ -171,13 +186,20 @@ module ActionView # favicon_link_tag 'mb-icon.png', rel: 'apple-touch-icon', type: 'image/png' # # => def favicon_link_tag(source="favicon.ico", options={}) - tag("link", { + tag('link', { rel: "shortcut icon", type: "image/x-icon", - href: path_to_image(source) + href: path_to_image(source, { public_folder: options.delete(:public_folder) }) }.merge!(options.symbolize_keys)) end + # Returns a link tag for a favicon asset in the public + # folder. This uses +favicon_link_tag+ and skips any asset + # lookups by assuming any assets are in the `public` folder. + def public_favicon_link_tag(source='favicon.ico', options={}) + favicon_link_tag(source, { public_folder: true }.merge!(options)) + end + # Returns an HTML image tag for the +source+. The +source+ can be a full # path or a file. # @@ -212,7 +234,7 @@ module ActionView options = options.symbolize_keys check_for_image_tag_errors(options) - src = options[:src] = path_to_image(source) + src = options[:src] = path_to_image(source, { public_folder: options.delete(:public_folder) }) unless src.start_with?("cid:") || src.start_with?("data:") || src.blank? options[:alt] = options.fetch(:alt) { image_alt(src) } @@ -222,6 +244,13 @@ module ActionView tag("img", options) end + # Returns an HTML image tag for the source asset in the public + # folder. This uses +image_tag+ and skips any asset + # lookups by assuming any assets are in the `public` folder. + def public_image_tag(source, options={}) + image_tag(source, { public_folder: true }.merge!(options)) + end + # Returns a string suitable for an HTML image tag alt attribute. # The +src+ argument is meant to be an image file path. # The method removes the basename of the file path and the digest, @@ -282,12 +311,23 @@ module ActionView # video_tag(["trailer.ogg", "trailer.flv"], size: "160x120") # # => def video_tag(*sources) + options = sources.extract_options!.symbolize_keys + public_poster_folder = options.delete(:public_poster_folder) + sources << options multiple_sources_tag("video", sources) do |options| - options[:poster] = path_to_image(options[:poster]) if options[:poster] + options[:poster] = path_to_image(options[:poster], { public_folder: public_poster_folder }) if options[:poster] options[:width], options[:height] = extract_dimensions(options.delete(:size)) if options[:size] end end + # Returns an HTML video tag for the source asset in the public + # folder. This uses +video_tag+ and skips any asset + # lookups by assuming any assets are in the `public` folder. + def public_video_tag(*sources) + options = sources.extract_options! + video_tag(*sources, { public_folder: true , public_poster_folder: true}.merge!(options)) + end + # Returns an HTML audio tag for the +source+. # The +source+ can be full path or file that exists in # your public audios directory. @@ -304,19 +344,27 @@ module ActionView multiple_sources_tag("audio", sources) end + # Returns an HTML audio tag for the source asset in the public + # folder. This uses +audio_tag+ and skips any asset + def public_audio_tag(*sources) + options = sources.extract_options! + audio_tag(*sources, { public_folder: true }.merge!(options)) + end + private def multiple_sources_tag(type, sources) - options = sources.extract_options!.symbolize_keys + options = sources.extract_options!.symbolize_keys + public_folder = options.delete(:public_folder) sources.flatten! yield options if block_given? if sources.size > 1 content_tag(type, options) do - safe_join sources.map { |source| tag("source", src: send("path_to_#{type}", source)) } + safe_join sources.map { |source| tag("source", src: send("path_to_#{type}", source, { public_folder: public_folder })) } end else - options[:src] = send("path_to_#{type}", sources.first) + options[:src] = send("path_to_#{type}", sources.first, { public_folder: public_folder }) content_tag(type, nil, options) end end diff --git a/railties/test/application/asset_debugging_test.rb b/railties/test/application/asset_debugging_test.rb index 92d10ec827..bbaabdb077 100644 --- a/railties/test/application/asset_debugging_test.rb +++ b/railties/test/application/asset_debugging_test.rb @@ -69,16 +69,22 @@ module ApplicationTests assert_match(/ def javascript_include_tag(*sources) options = sources.extract_options!.stringify_keys - path_options = options.extract!("protocol", "extname", "host", "public_folder").symbolize_keys + path_options = options.extract!("protocol", "extname", "host", "skip_pipeline").symbolize_keys sources.uniq.map { |source| tag_options = { "src" => path_to_javascript(source, path_options) @@ -92,7 +92,7 @@ module ActionView # # def stylesheet_link_tag(*sources) options = sources.extract_options!.stringify_keys - path_options = options.extract!("protocol", "host", "public_folder").symbolize_keys + path_options = options.extract!("protocol", "host", "skip_pipeline").symbolize_keys sources.uniq.map { |source| tag_options = { "rel" => "stylesheet", @@ -173,7 +173,7 @@ module ActionView tag("link", { rel: "shortcut icon", type: "image/x-icon", - href: path_to_image(source, public_folder: options.delete(:public_folder)) + href: path_to_image(source, skip_pipeline: options.delete(:skip_pipeline)) }.merge!(options.symbolize_keys)) end @@ -211,7 +211,7 @@ module ActionView options = options.symbolize_keys check_for_image_tag_errors(options) - src = options[:src] = path_to_image(source, public_folder: options.delete(:public_folder)) + src = options[:src] = path_to_image(source, skip_pipeline: options.delete(:skip_pipeline)) unless src.start_with?("cid:") || src.start_with?("data:") || src.blank? options[:alt] = options.fetch(:alt) { image_alt(src) } @@ -289,7 +289,7 @@ module ActionView public_poster_folder = options.delete(:public_poster_folder) sources << options multiple_sources_tag_builder("video", sources) do |options| - options[:poster] = path_to_image(options[:poster], public_folder: public_poster_folder) if options[:poster] + options[:poster] = path_to_image(options[:poster], skip_pipeline: public_poster_folder) if options[:poster] options[:width], options[:height] = extract_dimensions(options.delete(:size)) if options[:size] end end @@ -313,17 +313,17 @@ module ActionView private def multiple_sources_tag_builder(type, sources) options = sources.extract_options!.symbolize_keys - public_folder = options.delete(:public_folder) + skip_pipeline = options.delete(:skip_pipeline) sources.flatten! yield options if block_given? if sources.size > 1 content_tag(type, options) do - safe_join sources.map { |source| tag("source", src: send("path_to_#{type}", source, public_folder: public_folder)) } + safe_join sources.map { |source| tag("source", src: send("path_to_#{type}", source, skip_pipeline: skip_pipeline)) } end else - options[:src] = send("path_to_#{type}", sources.first, public_folder: public_folder) + options[:src] = send("path_to_#{type}", sources.first, skip_pipeline: skip_pipeline) content_tag(type, nil, options) end end diff --git a/actionview/lib/action_view/helpers/asset_url_helper.rb b/actionview/lib/action_view/helpers/asset_url_helper.rb index dc46cad410..d1f85d3319 100644 --- a/actionview/lib/action_view/helpers/asset_url_helper.rb +++ b/actionview/lib/action_view/helpers/asset_url_helper.rb @@ -121,7 +121,7 @@ module ActionView # This is the entry point for all assets. # When using the asset pipeline (i.e. sprockets and sprockets-rails), the # behavior is "enhanced". You can bypass the asset pipeline by passing in - # public_folder: true to the options. + # skip_pipeline: true to the options. # # All other asset *_path helpers delegate through this method. # @@ -132,16 +132,16 @@ module ActionView # # asset_path("application.js") # => "/assets/application-60aa4fdc5cea14baf5400fba1abf4f2a46a5166bad4772b1effe341570f07de9.js" # - # === Without the asset pipeline (public_folder: true) + # === Without the asset pipeline (skip_pipeline: true) # # Accepts a type option that can specify the asset's extension. No error # checking is done to verify the source passed into +asset_path+ is valid # and that the file exists on disk. # - # asset_path("application.js", public_folder: true) # => "application.js" - # asset_path("filedoesnotexist.png", public_folder: true) # => "filedoesnotexist.png" - # asset_path("application", type: :javascript, public_folder: true) # => "/javascripts/application.js" - # asset_path("application", type: :stylesheet, public_folder: true) # => "/stylesheets/application.css" + # asset_path("application.js", skip_pipeline: true) # => "application.js" + # asset_path("filedoesnotexist.png", skip_pipeline: true) # => "filedoesnotexist.png" + # asset_path("application", type: :javascript, skip_pipeline: true) # => "/javascripts/application.js" + # asset_path("application", type: :stylesheet, skip_pipeline: true) # => "/stylesheets/application.css" # # === Options applying to all assets # @@ -167,18 +167,18 @@ module ActionView # root prepended. # # Rails.application.config.relative_url_root = "bar" - # asset_path("foo.js", public_folder: true) # => "bar/foo.js" + # asset_path("foo.js", skip_pipeline: true) # => "bar/foo.js" # # - A different asset host can be specified via config.action_controller.asset_host # this is commonly used in conjunction with a CDN. # # Rails.application.config.action_controller.asset_host = "assets.example.com" - # asset_path("foo.js", public_folder: true) # => "http://assets.example.com/foo.js" + # asset_path("foo.js", skip_pipeline: true) # => "http://assets.example.com/foo.js" # # - An extension name can be specified manually with extname. # - # asset_path("foo", public_folder: true, extname: ".js") # => "/foo.js" - # asset_path("foo.css", public_folder: true, extname: ".js") # => "/foo.css.js" + # asset_path("foo", skip_pipeline: true, extname: ".js") # => "/foo.js" + # asset_path("foo.css", skip_pipeline: true, extname: ".js") # => "/foo.css.js" def asset_path(source, options = {}) raise ArgumentError, "nil is not a valid asset source" if source.nil? @@ -193,7 +193,7 @@ module ActionView end if source[0] != ?/ - if options[:public_folder] + if options[:skip_pipeline] source = public_compute_asset_path(source, options) else source = compute_asset_path(source, options) diff --git a/railties/test/application/asset_debugging_test.rb b/railties/test/application/asset_debugging_test.rb index ac42cbcec9..06d170fa4a 100644 --- a/railties/test/application/asset_debugging_test.rb +++ b/railties/test/application/asset_debugging_test.rb @@ -88,7 +88,7 @@ module ApplicationTests } cases.each do |(view_method, tag_match)| - app_file "app/views/posts/index.html.erb", "<%= #{ view_method } '#{contents}', public_folder: true %>" + app_file "app/views/posts/index.html.erb", "<%= #{ view_method } '#{contents}', skip_pipeline: true %>" app "development" @@ -114,7 +114,7 @@ module ApplicationTests } cases.each do |(view_method, tag_match)| - app_file "app/views/posts/index.html.erb", "<%= #{ view_method } '#{contents}', public_folder: true %>" + app_file "app/views/posts/index.html.erb", "<%= #{ view_method } '#{contents}', skip_pipeline: true %>" app "development" @@ -127,10 +127,10 @@ module ApplicationTests end end - test "{ public_folder: true } does not use the asset pipeline" do + test "{ skip_pipeline: true } does not use the asset pipeline" do cases = { /\/assets\/application-.*.\.js/ => {}, - /application.js/ => { public_folder: true }, + /application.js/ => { skip_pipeline: true }, } cases.each do |(tag_match, options_hash)| app_file "app/views/posts/index.html.erb", "<%= asset_path('application.js', #{ options_hash }) %>" -- cgit v1.2.3 From ab5857a53a50cf9de0a8b869c5f43b1acdb90aa5 Mon Sep 17 00:00:00 2001 From: schneems Date: Tue, 30 Aug 2016 15:02:56 -0500 Subject: Better keyword argument name --- actionview/lib/action_view/helpers/asset_tag_helper.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/actionview/lib/action_view/helpers/asset_tag_helper.rb b/actionview/lib/action_view/helpers/asset_tag_helper.rb index 9c22fb0bde..2b9c1a8ceb 100644 --- a/actionview/lib/action_view/helpers/asset_tag_helper.rb +++ b/actionview/lib/action_view/helpers/asset_tag_helper.rb @@ -257,7 +257,7 @@ module ActionView # * :size - Supplied as "{Width}x{Height}" or "{Number}", so "30x45" becomes # width="30" and height="45", and "50" becomes width="50" and height="50". # :size will be ignored if the value is not in the correct format. - # * :public_poster_folder will bypass the asset pipeline when using + # * :poster_skip_pipeline will bypass the asset pipeline when using # the :poster option instead using an asset in the public folder. # # ==== Examples @@ -270,7 +270,7 @@ module ActionView # # => # video_tag("trailer.m4v", size: "16x10", poster: "screenshot.png") # # => - # video_tag("trailer.m4v", size: "16x10", poster: "screenshot.png", public_poster_folder: true) + # video_tag("trailer.m4v", size: "16x10", poster: "screenshot.png", poster_skip_pipeline: true) # # => # video_tag("/trailers/hd.avi", size: "16x16") # # => @@ -286,7 +286,7 @@ module ActionView # # => def video_tag(*sources) options = sources.extract_options!.symbolize_keys - public_poster_folder = options.delete(:public_poster_folder) + public_poster_folder = options.delete(:poster_skip_pipeline) sources << options multiple_sources_tag_builder("video", sources) do |options| options[:poster] = path_to_image(options[:poster], skip_pipeline: public_poster_folder) if options[:poster] -- cgit v1.2.3 From 70dd041f354dfc71b7e516d5ba5725c769ef06d9 Mon Sep 17 00:00:00 2001 From: schneems Date: Tue, 30 Aug 2016 15:04:23 -0500 Subject: Fix style --- .../lib/action_view/helpers/asset_url_helper.rb | 10 ++--- railties/test/application/asset_debugging_test.rb | 52 +++++++++++----------- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/actionview/lib/action_view/helpers/asset_url_helper.rb b/actionview/lib/action_view/helpers/asset_url_helper.rb index d1f85d3319..0967245855 100644 --- a/actionview/lib/action_view/helpers/asset_url_helper.rb +++ b/actionview/lib/action_view/helpers/asset_url_helper.rb @@ -130,7 +130,7 @@ module ActionView # All options passed to +asset_path+ will be passed to +compute_asset_path+ # which is implemented by sprockets-rails. # - # asset_path("application.js") # => "/assets/application-60aa4fdc5cea14baf5400fba1abf4f2a46a5166bad4772b1effe341570f07de9.js" + # asset_path("application.js") # => "/assets/application-60aa4fdc5cea14baf5400fba1abf4f2a46a5166bad4772b1effe341570f07de9.js" # # === Without the asset pipeline (skip_pipeline: true) # @@ -138,10 +138,10 @@ module ActionView # checking is done to verify the source passed into +asset_path+ is valid # and that the file exists on disk. # - # asset_path("application.js", skip_pipeline: true) # => "application.js" - # asset_path("filedoesnotexist.png", skip_pipeline: true) # => "filedoesnotexist.png" - # asset_path("application", type: :javascript, skip_pipeline: true) # => "/javascripts/application.js" - # asset_path("application", type: :stylesheet, skip_pipeline: true) # => "/stylesheets/application.css" + # asset_path("application.js", skip_pipeline: true) # => "application.js" + # asset_path("filedoesnotexist.png", skip_pipeline: true) # => "filedoesnotexist.png" + # asset_path("application", type: :javascript, skip_pipeline: true) # => "/javascripts/application.js" + # asset_path("application", type: :stylesheet, skip_pipeline: true) # => "/stylesheets/application.css" # # === Options applying to all assets # diff --git a/railties/test/application/asset_debugging_test.rb b/railties/test/application/asset_debugging_test.rb index 06d170fa4a..3e17a1efa5 100644 --- a/railties/test/application/asset_debugging_test.rb +++ b/railties/test/application/asset_debugging_test.rb @@ -72,23 +72,23 @@ module ApplicationTests test "public path and tag methods are not over-written by the asset pipeline" do contents = "doesnotexist" cases = { - asset_path: %r{/#{ contents }}, - image_path: %r{/images/#{ contents }}, - video_path: %r{/videos/#{ contents }}, - audio_path: %r{/audios/#{ contents }}, - font_path: %r{/fonts/#{ contents }}, - javascript_path: %r{/javascripts/#{ contents }}, - stylesheet_path: %r{/stylesheets/#{ contents }}, - image_tag: %r{}, - stylesheet_link_tag: %r{}, - javascript_include_tag: %r{