aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--actionpack/CHANGELOG10
-rw-r--r--actionpack/lib/action_view/helpers/asset_tag_helper.rb46
-rw-r--r--actionpack/test/template/asset_tag_helper_test.rb39
3 files changed, 84 insertions, 11 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index bf8b538f99..cbf066228c 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,15 @@
*2.0.2* (December 16th, 2007)
+* Added option to pass proc to ActionController::Base.asset_host for maximum configurability #10521 [chuyeow]. Example:
+
+ ActionController::Base.asset_host = Proc.new { |source|
+ if source.starts_with?('/images')
+ "http://images.example.com"
+ else
+ "http://assets.example.com"
+ end
+ }
+
* Fixed that ActionView#file_exists? would be incorrect if @first_render is set #10569 [dbussink]
* Added that Array#to_param calls to_param on all it's elements #10473 [brandon]
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
diff --git a/actionpack/test/template/asset_tag_helper_test.rb b/actionpack/test/template/asset_tag_helper_test.rb
index 502c54e57f..ac21bc2a33 100644
--- a/actionpack/test/template/asset_tag_helper_test.rb
+++ b/actionpack/test/template/asset_tag_helper_test.rb
@@ -223,7 +223,6 @@ class AssetTagHelperTest < Test::Unit::TestCase
assert_equal copy, source
end
-
def test_caching_javascript_include_tag_when_caching_on
ENV["RAILS_ASSET_ID"] = ""
ActionController::Base.asset_host = 'http://a%d.example.com'
@@ -247,7 +246,24 @@ class AssetTagHelperTest < Test::Unit::TestCase
File.delete(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'all.js'))
File.delete(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'money.js'))
end
-
+
+ def test_caching_javascript_include_tag_when_caching_on_with_proc_asset_host
+ ENV["RAILS_ASSET_ID"] = ""
+ ActionController::Base.asset_host = Proc.new { |source| "http://a#{source.length}.example.com" }
+ ActionController::Base.perform_caching = true
+
+ assert_equal '/javascripts/scripts.js'.length, 23
+ assert_dom_equal(
+ %(<script src="http://a23.example.com/javascripts/scripts.js" type="text/javascript"></script>),
+ javascript_include_tag(:all, :cache => 'scripts')
+ )
+
+ assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'scripts.js'))
+
+ ensure
+ File.delete(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'scripts.js'))
+ end
+
def test_caching_javascript_include_tag_when_caching_on_and_using_subdirectory
ENV["RAILS_ASSET_ID"] = ""
ActionController::Base.asset_host = 'http://a%d.example.com'
@@ -304,7 +320,24 @@ class AssetTagHelperTest < Test::Unit::TestCase
File.delete(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'all.css'))
File.delete(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'money.css'))
end
-
+
+ def test_caching_stylesheet_link_tag_when_caching_on_with_proc_asset_host
+ ENV["RAILS_ASSET_ID"] = ""
+ ActionController::Base.asset_host = Proc.new { |source| "http://a#{source.length}.example.com" }
+ ActionController::Base.perform_caching = true
+
+ assert_equal '/stylesheets/styles.css'.length, 23
+ assert_dom_equal(
+ %(<link href="http://a23.example.com/stylesheets/styles.css" media="screen" rel="stylesheet" type="text/css" />),
+ stylesheet_link_tag(:all, :cache => 'styles')
+ )
+
+ assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'styles.css'))
+
+ ensure
+ File.delete(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'styles.css'))
+ end
+
def test_caching_stylesheet_include_tag_when_caching_off
ENV["RAILS_ASSET_ID"] = ""
ActionController::Base.perform_caching = false