From 56e646296af1929f07f0e2981deddba95cdbf5d0 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 16 Dec 2007 23:50:02 +0000 Subject: Added option to pass proc to ActionController::Base.asset_host for maximum configurability (closes #10521) [chuyeow] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8421 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../lib/action_view/helpers/asset_tag_helper.rb | 46 ++++++++++++++++++---- 1 file changed, 38 insertions(+), 8 deletions(-) (limited to 'actionpack/lib/action_view/helpers') diff --git a/actionpack/lib/action_view/helpers/asset_tag_helper.rb b/actionpack/lib/action_view/helpers/asset_tag_helper.rb index 6aaf147f94..ba474a8ffe 100644 --- a/actionpack/lib/action_view/helpers/asset_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/asset_tag_helper.rb @@ -31,7 +31,7 @@ module ActionView # stylesheet_include_tag("application") # => # - # To do this, you can either setup four actual hosts, or you can use wildcard DNS to CNAME + # To do this, you can either setup 4 actual hosts, or you can use wildcard DNS to CNAME # the wildcard to a single asset host. You can read more about setting up your DNS CNAME records from # your ISP. # @@ -39,6 +39,32 @@ module ActionView # for server load balancing. See http://www.die.net/musings/page_load_time/ # for background. # + # Alternatively, you can exert more control over the asset host by setting asset_host to a proc + # that takes a single source argument. This is useful if you are unable to setup 4 actual hosts or have + # fewer/more than 4 hosts. The example proc below generates http://assets1.example.com and + # http://assets2.example.com randomly. + # + # ActionController::Base.asset_host = Proc.new { |source| "http://assets#{rand(2) + 1}.example.com" } + # image_tag("rails.png") + # => Rails + # stylesheet_include_tag("application") + # => + # + # The proc takes a single source parameter which is the path of the source asset. This can be used to + # generate a particular asset host depending on the asset path. + # + # ActionController::Base.asset_host = Proc.new { |source| + # if source.starts_with?('/images') + # "http://images.example.com" + # else + # "http://assets.example.com" + # end + # } + # image_tag("rails.png") + # => Rails + # stylesheet_include_tag("application") + # => + # # === Using asset timestamps # # By default, Rails will append all asset paths with that asset's timestamp. This allows you to set a cache-expiration date for the @@ -385,19 +411,18 @@ module ActionView # Add the .ext if not present. Return full URLs otherwise untouched. # Prefix with /dir/ if lacking a leading /. Account for relative URL # roots. Rewrite the asset path for cache-busting asset ids. Include - # a single or wildcarded asset host, if configured, with the correct - # request protocol. + # asset host, if configured, with the correct request protocol. def compute_public_path(source, dir, ext = nil, include_host = true) has_request = @controller.respond_to?(:request) cache_key = if has_request [ @controller.request.protocol, - ActionController::Base.asset_host, + ActionController::Base.asset_host.to_s, @controller.request.relative_url_root, dir, source, ext, include_host ].join else - [ ActionController::Base.asset_host, + [ ActionController::Base.asset_host.to_s, dir, source, ext, include_host ].join end @@ -430,11 +455,16 @@ module ActionView end # Pick an asset host for this source. Returns nil if no host is set, - # the host if no wildcard is set, or the host interpolated with the - # numbers 0-3 if it contains %d. The number is the source hash mod 4. + # the host if no wildcard is set, the host interpolated with the + # numbers 0-3 if it contains %d (the number is the source hash mod 4), + # or the value returned from invoking the proc if it's a proc. def compute_asset_host(source) if host = ActionController::Base.asset_host - host % (source.hash % 4) + if host.is_a?(Proc) + host.call(source) + else + host % (source.hash % 4) + end end end -- cgit v1.2.3