From a3a5c7eba39c64413abd0fb4766282c9f071d248 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 19 Apr 2011 18:07:14 +0200 Subject: All assets, including images, audio, and video, now uses the asset pipeline when its on --- actionpack/lib/action_view/helpers/sprockets_helper.rb | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'actionpack/lib/action_view/helpers/sprockets_helper.rb') diff --git a/actionpack/lib/action_view/helpers/sprockets_helper.rb b/actionpack/lib/action_view/helpers/sprockets_helper.rb index 408a2030ab..fee13be886 100644 --- a/actionpack/lib/action_view/helpers/sprockets_helper.rb +++ b/actionpack/lib/action_view/helpers/sprockets_helper.rb @@ -3,8 +3,12 @@ require 'uri' module ActionView module Helpers module SprocketsHelper + def sprockets_asset_path(source, default_ext = nil) + compute_sprockets_path(source, 'assets', default_ext) + end + def sprockets_javascript_path(source) - compute_sprockets_path source, 'assets', 'js' + sprockets_asset_path(source, 'js') end def sprockets_javascript_include_tag(source, options = {}) @@ -17,9 +21,9 @@ module ActionView end def sprockets_stylesheet_path(source) - compute_sprockets_path source, 'assets', 'css' + sprockets_asset_path(source, 'css') end - + def sprockets_stylesheet_link_tag(source, options = {}) options = { 'rel' => "stylesheet", @@ -31,13 +35,14 @@ module ActionView tag 'link', options end + private - def compute_sprockets_path(source, dir, default_ext) + def compute_sprockets_path(source, dir, default_ext = nil) source = source.to_s return source if URI.parse(source).host - # Add /javscripts to relative paths + # Add /assets to relative paths if source[0] != ?/ source = "/#{dir}/#{source}" end -- cgit v1.2.3 From d35c91225e8eea967358328ba618e6608222a615 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 19 Apr 2011 18:29:18 +0200 Subject: Cleanup compute_sprockets_path -- when you are reaching for code comments, the method could be simpler --- .../lib/action_view/helpers/sprockets_helper.rb | 51 +++++++++++++--------- 1 file changed, 30 insertions(+), 21 deletions(-) (limited to 'actionpack/lib/action_view/helpers/sprockets_helper.rb') diff --git a/actionpack/lib/action_view/helpers/sprockets_helper.rb b/actionpack/lib/action_view/helpers/sprockets_helper.rb index fee13be886..d2a31c02d4 100644 --- a/actionpack/lib/action_view/helpers/sprockets_helper.rb +++ b/actionpack/lib/action_view/helpers/sprockets_helper.rb @@ -11,6 +11,11 @@ module ActionView sprockets_asset_path(source, 'js') end + def sprockets_stylesheet_path(source) + sprockets_asset_path(source, 'css') + end + + def sprockets_javascript_include_tag(source, options = {}) options = { 'type' => "text/javascript", @@ -20,10 +25,6 @@ module ActionView content_tag 'script', "", options end - def sprockets_stylesheet_path(source) - sprockets_asset_path(source, 'css') - end - def sprockets_stylesheet_link_tag(source, options = {}) options = { 'rel' => "stylesheet", @@ -40,30 +41,38 @@ module ActionView def compute_sprockets_path(source, dir, default_ext = nil) source = source.to_s - return source if URI.parse(source).host - - # Add /assets to relative paths - if source[0] != ?/ - source = "/#{dir}/#{source}" + unless source_is_a_url?(source) + add_asset_directory(source, dir) + add_default_extension(source, default_ext) + add_fingerprint(source, dir) + add_asset_host(source) end - # Add default extension if there isn't one - if default_ext && File.extname(source).empty? - source = "#{source}.#{default_ext}" - end - - # Fingerprint url - if source =~ /^\/#{dir}\/(.+)/ - source = assets.path($1, config.perform_caching, dir) - end + source + end + + def add_asset_directory(source, dir) + source.replace("/#{dir}/#{source}") if source[0] != ?/ + end + + def add_default_extension(source, default_ext) + source.replace("#{source}.#{default_ext}") if default_ext && File.extname(source).empty? + end + + def add_fingerprint(source, dir) + source.replace(assets.path($1, config.perform_caching, dir)) if source =~ /^\/#{dir}\/(.+)/ + end + def add_asset_host(source) host = compute_asset_host(source) if controller.respond_to?(:request) && host && URI.parse(host).host - source = "#{controller.request.protocol}#{host}#{source}" + source.replace("#{controller.request.protocol}#{host}#{source}") end - - source + end + + def source_is_a_url?(source) + URI.parse(source).host.present? end def compute_asset_host(source) -- cgit v1.2.3 From 626bcc9bf415ca9872bbdaceac30a4df9aca86bb Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 19 Apr 2011 19:05:07 +0200 Subject: Switch to asset_path and make it available in the Sprockets::Context (now you can do asset_path("logo.png") in a stylesheet.css.erb file and get fingerprinting) --- .../lib/action_view/helpers/sprockets_helper.rb | 32 +++++++++++++--------- 1 file changed, 19 insertions(+), 13 deletions(-) (limited to 'actionpack/lib/action_view/helpers/sprockets_helper.rb') diff --git a/actionpack/lib/action_view/helpers/sprockets_helper.rb b/actionpack/lib/action_view/helpers/sprockets_helper.rb index d2a31c02d4..bf47af6e10 100644 --- a/actionpack/lib/action_view/helpers/sprockets_helper.rb +++ b/actionpack/lib/action_view/helpers/sprockets_helper.rb @@ -3,23 +3,14 @@ require 'uri' module ActionView module Helpers module SprocketsHelper - def sprockets_asset_path(source, default_ext = nil) + def asset_path(source, default_ext = nil) compute_sprockets_path(source, 'assets', default_ext) end - def sprockets_javascript_path(source) - sprockets_asset_path(source, 'js') - end - - def sprockets_stylesheet_path(source) - sprockets_asset_path(source, 'css') - end - - def sprockets_javascript_include_tag(source, options = {}) options = { 'type' => "text/javascript", - 'src' => sprockets_javascript_path(source) + 'src' => asset_path(source, 'js') }.merge(options.stringify_keys) content_tag 'script', "", options @@ -30,7 +21,7 @@ module ActionView 'rel' => "stylesheet", 'type' => "text/css", 'media' => "screen", - 'href' => sprockets_stylesheet_path(source) + 'href' => asset_path(source, 'css') }.merge(options.stringify_keys) tag 'link', options @@ -60,10 +51,15 @@ module ActionView end def add_fingerprint(source, dir) - source.replace(assets.path($1, config.perform_caching, dir)) if source =~ /^\/#{dir}\/(.+)/ + if source =~ /^\/#{dir}\/(.+)/ + source.replace(assets.path($1, performing_caching?, dir)) + end end def add_asset_host(source) + # When included in Sprockets::Context, there's no controller + return unless respond_to?(:controller) + host = compute_asset_host(source) if controller.respond_to?(:request) && host && URI.parse(host).host @@ -94,6 +90,16 @@ module ActionView def assets Rails.application.assets end + + def performing_caching? + # When included in Sprockets::Context, we need to ask the top-level config as the controller is not available + respond_to?(:config) ? config.perform_caching : Rails.application.config.action_controller.perform_caching + end end end end + +# FIXME: Temp hack for extending Sprockets::Context so +class Sprockets::Context + include ActionView::Helpers::SprocketsHelper +end if defined?(Sprockets) \ No newline at end of file -- cgit v1.2.3 From 914218ef302542f3f58ef7f8f46c0ff0b540ac82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 19 Apr 2011 21:14:55 +0200 Subject: Let's use inheritance here, shall we? --- .../lib/action_view/helpers/sprockets_helper.rb | 77 +++++++--------------- 1 file changed, 23 insertions(+), 54 deletions(-) (limited to 'actionpack/lib/action_view/helpers/sprockets_helper.rb') diff --git a/actionpack/lib/action_view/helpers/sprockets_helper.rb b/actionpack/lib/action_view/helpers/sprockets_helper.rb index bf47af6e10..e2e844c74d 100644 --- a/actionpack/lib/action_view/helpers/sprockets_helper.rb +++ b/actionpack/lib/action_view/helpers/sprockets_helper.rb @@ -1,10 +1,11 @@ require 'uri' +require 'action_view/helpers/asset_tag_helpers/asset_paths' module ActionView module Helpers module SprocketsHelper def asset_path(source, default_ext = nil) - compute_sprockets_path(source, 'assets', default_ext) + sprockets_asset_paths.compute_public_path(source, 'assets', default_ext, true) end def sprockets_javascript_include_tag(source, options = {}) @@ -27,74 +28,42 @@ module ActionView tag 'link', options end - private - def compute_sprockets_path(source, dir, default_ext = nil) - source = source.to_s - - unless source_is_a_url?(source) - add_asset_directory(source, dir) - add_default_extension(source, default_ext) - add_fingerprint(source, dir) - add_asset_host(source) - end - source - end - - def add_asset_directory(source, dir) - source.replace("/#{dir}/#{source}") if source[0] != ?/ - end - - def add_default_extension(source, default_ext) - source.replace("#{source}.#{default_ext}") if default_ext && File.extname(source).empty? - end - - def add_fingerprint(source, dir) - if source =~ /^\/#{dir}\/(.+)/ - source.replace(assets.path($1, performing_caching?, dir)) - end + def sprockets_asset_paths + @sprockets_asset_paths ||= begin + config = self.config if respond_to?(:config) + controller = self.controller if respond_to?(:controller) + SprocketsHelper::AssetPaths.new(config, controller) end + end - def add_asset_host(source) - # When included in Sprockets::Context, there's no controller - return unless respond_to?(:controller) - - host = compute_asset_host(source) - - if controller.respond_to?(:request) && host && URI.parse(host).host - source.replace("#{controller.request.protocol}#{host}#{source}") + class AssetPaths < ActionView::Helpers::AssetTagHelper::AssetPaths + def rewrite_asset_path(source, dir) + if source =~ /^\/#{dir}\/(.+)/ + assets.path($1, performing_caching?, dir) + else + source end end - - def source_is_a_url?(source) - URI.parse(source).host.present? - end - def compute_asset_host(source) - if host = config.asset_host - if host.is_a?(Proc) || host.respond_to?(:call) - case host.is_a?(Proc) ? host.arity : host.method(:call).arity - when 2 - request = controller.respond_to?(:request) && controller.request - host.call(source, request) - else - host.call(source) - end - else - (host =~ /%d/) ? host % (source.hash % 4) : host - end + def rewrite_extension(source, dir, ext) + if ext && File.extname(source).empty? + "#{source}.#{ext}" + else + source end end def assets Rails.application.assets end - + + # When included in Sprockets::Context, we need to ask the top-level config as the controller is not available def performing_caching? - # When included in Sprockets::Context, we need to ask the top-level config as the controller is not available - respond_to?(:config) ? config.perform_caching : Rails.application.config.action_controller.perform_caching + @config ? @config.perform_caching : Rails.application.config.action_controller.perform_caching end + end end end end -- cgit v1.2.3 From a19c260038a9b5b688a2e8d883b604983ac59eae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 19 Apr 2011 21:49:28 +0200 Subject: Include modules to the context in the railtie. --- actionpack/lib/action_view/helpers/sprockets_helper.rb | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'actionpack/lib/action_view/helpers/sprockets_helper.rb') diff --git a/actionpack/lib/action_view/helpers/sprockets_helper.rb b/actionpack/lib/action_view/helpers/sprockets_helper.rb index e2e844c74d..947c827f3c 100644 --- a/actionpack/lib/action_view/helpers/sprockets_helper.rb +++ b/actionpack/lib/action_view/helpers/sprockets_helper.rb @@ -66,9 +66,4 @@ module ActionView end end end -end - -# FIXME: Temp hack for extending Sprockets::Context so -class Sprockets::Context - include ActionView::Helpers::SprocketsHelper -end if defined?(Sprockets) \ No newline at end of file +end \ No newline at end of file -- cgit v1.2.3 From 22fcef90b185199563719fc511346bf4c2f5bbff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 19 Apr 2011 22:01:25 +0200 Subject: Actually add an abstract class, so it is easier to get rid of old asset paths in the future. --- actionpack/lib/action_view/helpers/sprockets_helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'actionpack/lib/action_view/helpers/sprockets_helper.rb') diff --git a/actionpack/lib/action_view/helpers/sprockets_helper.rb b/actionpack/lib/action_view/helpers/sprockets_helper.rb index 947c827f3c..b43b91178c 100644 --- a/actionpack/lib/action_view/helpers/sprockets_helper.rb +++ b/actionpack/lib/action_view/helpers/sprockets_helper.rb @@ -1,5 +1,5 @@ require 'uri' -require 'action_view/helpers/asset_tag_helpers/asset_paths' +require 'action_view/helpers/asset_paths' module ActionView module Helpers @@ -38,7 +38,7 @@ module ActionView end end - class AssetPaths < ActionView::Helpers::AssetTagHelper::AssetPaths + class AssetPaths < ActionView::Helpers::AssetPaths #:nodoc: def rewrite_asset_path(source, dir) if source =~ /^\/#{dir}\/(.+)/ assets.path($1, performing_caching?, dir) -- cgit v1.2.3 From f7c711baee9ccd25fb976d45cb9fca033878fd26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 3 May 2011 12:42:42 +0200 Subject: No need for a regexp here. --- actionpack/lib/action_view/helpers/sprockets_helper.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'actionpack/lib/action_view/helpers/sprockets_helper.rb') diff --git a/actionpack/lib/action_view/helpers/sprockets_helper.rb b/actionpack/lib/action_view/helpers/sprockets_helper.rb index b43b91178c..ab98da9624 100644 --- a/actionpack/lib/action_view/helpers/sprockets_helper.rb +++ b/actionpack/lib/action_view/helpers/sprockets_helper.rb @@ -40,10 +40,10 @@ module ActionView class AssetPaths < ActionView::Helpers::AssetPaths #:nodoc: def rewrite_asset_path(source, dir) - if source =~ /^\/#{dir}\/(.+)/ - assets.path($1, performing_caching?, dir) - else + if source[0] == ?/ source + else + assets.path(source, performing_caching?, dir) end end -- cgit v1.2.3