aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2007-12-16 23:50:02 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2007-12-16 23:50:02 +0000
commit56e646296af1929f07f0e2981deddba95cdbf5d0 (patch)
tree1b95c2fe403cba5be1e7a6872cff32adcc55d426 /actionpack/lib/action_view/helpers
parentd7e6df43a9d41f1380d79b93881ef0abacb8973b (diff)
downloadrails-56e646296af1929f07f0e2981deddba95cdbf5d0.tar.gz
rails-56e646296af1929f07f0e2981deddba95cdbf5d0.tar.bz2
rails-56e646296af1929f07f0e2981deddba95cdbf5d0.zip
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
Diffstat (limited to 'actionpack/lib/action_view/helpers')
-rw-r--r--actionpack/lib/action_view/helpers/asset_tag_helper.rb46
1 files changed, 38 insertions, 8 deletions
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")
# => <link href="http://assets3.example.com/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" />
#
- # 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 <tt>asset_host</tt> 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")
+ # => <img src="http://assets2.example.com/images/rails.png" alt="Rails" />
+ # stylesheet_include_tag("application")
+ # => <link href="http://assets1.example.com/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" />
+ #
+ # The proc takes a single <tt>source</tt> 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")
+ # => <img src="http://images.example.com/images/rails.png" alt="Rails" />
+ # stylesheet_include_tag("application")
+ # => <link href="http://assets.example.com/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" />
+ #
# === 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