Skip to content
Browse files

Let's use inheritance here, shall we?

  • Loading branch information...
1 parent c96bf84 commit 914218ef302542f3f58ef7f8f46c0ff0b540ac82 @josevalim josevalim committed
View
5 actionpack/lib/action_view/helpers/asset_tag_helpers/asset_include_tag.rb
@@ -8,9 +8,11 @@ module Helpers
module AssetTagHelper
class AssetIncludeTag
- attr_reader :config, :asset_paths
+ include TagHelper
+ attr_reader :config, :asset_paths
class_attribute :expansions
+
def self.inherited(base)
base.expansions = { }
end
@@ -56,7 +58,6 @@ def include_tag(*sources)
end
end
-
private
def path_to_asset(source, include_host = true)
View
13 actionpack/lib/action_view/helpers/asset_tag_helpers/asset_paths.rb
@@ -26,15 +26,18 @@ def initialize(config, controller)
# roots. Rewrite the asset path for cache-busting asset ids. Include
# asset host, if configured, with the correct request protocol.
def compute_public_path(source, dir, ext = nil, include_host = true)
+ source = source.to_s
return source if is_uri?(source)
source = rewrite_extension(source, dir, ext) if ext
source = "/#{dir}/#{source}" unless source[0] == ?/
- source = rewrite_asset_path(source, config.asset_path)
+ source = rewrite_asset_path(source, dir)
- has_request = controller.respond_to?(:request)
- source = rewrite_relative_url_root(source, controller.config.relative_url_root) if has_request && include_host
- source = rewrite_host_and_protocol(source, has_request) if include_host
+ if controller && include_host
+ has_request = controller.respond_to?(:request)
+ source = rewrite_relative_url_root(source, controller.config.relative_url_root) if has_request
+ source = rewrite_host_and_protocol(source, has_request)
+ end
source
end
@@ -70,6 +73,8 @@ def rewrite_extension(source, dir, ext)
# Break out the asset path rewrite in case plugins wish to put the asset id
# someplace other than the query string.
def rewrite_asset_path(source, path = nil)
+ path = config.asset_path
+
if path && path.respond_to?(:call)
return path.call(source)
elsif path && path.is_a?(String)
View
3 actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb
@@ -1,6 +1,5 @@
require 'active_support/concern'
require 'active_support/core_ext/file'
-require 'action_view/helpers/tag_helper'
require 'action_view/helpers/asset_tag_helpers/asset_include_tag'
module ActionView
@@ -8,8 +7,6 @@ module Helpers
module AssetTagHelper
class JavascriptIncludeTag < AssetIncludeTag
- include TagHelper
-
def asset_name
'javascript'
end
View
3 actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb
@@ -1,6 +1,5 @@
require 'active_support/concern'
require 'active_support/core_ext/file'
-require 'action_view/helpers/tag_helper'
require 'action_view/helpers/asset_tag_helpers/asset_include_tag'
module ActionView
@@ -8,8 +7,6 @@ module Helpers
module AssetTagHelper
class StylesheetIncludeTag < AssetIncludeTag
- include TagHelper
-
def asset_name
'stylesheet'
end
View
77 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 @@ def sprockets_stylesheet_link_tag(source, options = {})
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
View
8 actionpack/test/template/sprockets_helper_test.rb
@@ -1,5 +1,8 @@
require 'abstract_unit'
require 'sprockets'
+require 'mocha'
+
+module Rails; end
class SprocketsHelperTest < ActionView::TestCase
tests ActionView::Helpers::SprocketsHelper
@@ -24,6 +27,11 @@ def host_with_port() 'localhost' end
@assets.paths << FIXTURES.join("sprockets/app/stylesheets")
@assets.paths << FIXTURES.join("sprockets/app/images")
+ application = Object.new
+ Rails.stubs(:application).returns(application)
+ application.stubs(:config).returns(config)
+ application.stubs(:assets).returns(@assets)
+
config.perform_caching = true
end

0 comments on commit 914218e

Please sign in to comment.
Something went wrong with that request. Please try again.