Changeset 8421
- Timestamp:
- 12/16/07 23:50:02 (5 months ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (1 diff)
- trunk/actionpack/lib/action_view/helpers/asset_tag_helper.rb (modified) (5 diffs)
- trunk/actionpack/test/template/asset_tag_helper_test.rb (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r8419 r8421 1 1 *2.0.2* (December 16th, 2007) 2 3 * Added option to pass proc to ActionController::Base.asset_host for maximum configurability #10521 [chuyeow]. Example: 4 5 ActionController::Base.asset_host = Proc.new { |source| 6 if source.starts_with?('/images') 7 "http://images.example.com" 8 else 9 "http://assets.example.com" 10 end 11 } 2 12 3 13 * Fixed that ActionView#file_exists? would be incorrect if @first_render is set #10569 [dbussink] trunk/actionpack/lib/action_view/helpers/asset_tag_helper.rb
r8368 r8421 32 32 # => <link href="http://assets3.example.com/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" /> 33 33 # 34 # To do this, you can either setup fouractual hosts, or you can use wildcard DNS to CNAME34 # To do this, you can either setup 4 actual hosts, or you can use wildcard DNS to CNAME 35 35 # the wildcard to a single asset host. You can read more about setting up your DNS CNAME records from 36 36 # your ISP. … … 39 39 # for server load balancing. See http://www.die.net/musings/page_load_time/ 40 40 # for background. 41 # 42 # Alternatively, you can exert more control over the asset host by setting <tt>asset_host</tt> to a proc 43 # that takes a single source argument. This is useful if you are unable to setup 4 actual hosts or have 44 # fewer/more than 4 hosts. The example proc below generates http://assets1.example.com and 45 # http://assets2.example.com randomly. 46 # 47 # ActionController::Base.asset_host = Proc.new { |source| "http://assets#{rand(2) + 1}.example.com" } 48 # image_tag("rails.png") 49 # => <img src="http://assets2.example.com/images/rails.png" alt="Rails" /> 50 # stylesheet_include_tag("application") 51 # => <link href="http://assets1.example.com/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" /> 52 # 53 # The proc takes a single <tt>source</tt> parameter which is the path of the source asset. This can be used to 54 # generate a particular asset host depending on the asset path. 55 # 56 # ActionController::Base.asset_host = Proc.new { |source| 57 # if source.starts_with?('/images') 58 # "http://images.example.com" 59 # else 60 # "http://assets.example.com" 61 # end 62 # } 63 # image_tag("rails.png") 64 # => <img src="http://images.example.com/images/rails.png" alt="Rails" /> 65 # stylesheet_include_tag("application") 66 # => <link href="http://assets.example.com/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" /> 41 67 # 42 68 # === Using asset timestamps … … 386 412 # Prefix with /dir/ if lacking a leading /. Account for relative URL 387 413 # roots. Rewrite the asset path for cache-busting asset ids. Include 388 # a single or wildcarded asset host, if configured, with the correct 389 # request protocol. 414 # asset host, if configured, with the correct request protocol. 390 415 def compute_public_path(source, dir, ext = nil, include_host = true) 391 416 has_request = @controller.respond_to?(:request) … … 394 419 if has_request 395 420 [ @controller.request.protocol, 396 ActionController::Base.asset_host ,421 ActionController::Base.asset_host.to_s, 397 422 @controller.request.relative_url_root, 398 423 dir, source, ext, include_host ].join 399 424 else 400 [ ActionController::Base.asset_host ,425 [ ActionController::Base.asset_host.to_s, 401 426 dir, source, ext, include_host ].join 402 427 end … … 431 456 432 457 # Pick an asset host for this source. Returns nil if no host is set, 433 # the host if no wildcard is set, or the host interpolated with the 434 # numbers 0-3 if it contains %d. The number is the source hash mod 4. 458 # the host if no wildcard is set, the host interpolated with the 459 # numbers 0-3 if it contains %d (the number is the source hash mod 4), 460 # or the value returned from invoking the proc if it's a proc. 435 461 def compute_asset_host(source) 436 462 if host = ActionController::Base.asset_host 437 host % (source.hash % 4) 463 if host.is_a?(Proc) 464 host.call(source) 465 else 466 host % (source.hash % 4) 467 end 438 468 end 439 469 end trunk/actionpack/test/template/asset_tag_helper_test.rb
r8365 r8421 224 224 end 225 225 226 227 226 def test_caching_javascript_include_tag_when_caching_on 228 227 ENV["RAILS_ASSET_ID"] = "" … … 248 247 File.delete(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'money.js')) 249 248 end 250 249 250 def test_caching_javascript_include_tag_when_caching_on_with_proc_asset_host 251 ENV["RAILS_ASSET_ID"] = "" 252 ActionController::Base.asset_host = Proc.new { |source| "http://a#{source.length}.example.com" } 253 ActionController::Base.perform_caching = true 254 255 assert_equal '/javascripts/scripts.js'.length, 23 256 assert_dom_equal( 257 %(<script src="http://a23.example.com/javascripts/scripts.js" type="text/javascript"></script>), 258 javascript_include_tag(:all, :cache => 'scripts') 259 ) 260 261 assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'scripts.js')) 262 263 ensure 264 File.delete(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'scripts.js')) 265 end 266 251 267 def test_caching_javascript_include_tag_when_caching_on_and_using_subdirectory 252 268 ENV["RAILS_ASSET_ID"] = "" … … 305 321 File.delete(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'money.css')) 306 322 end 307 323 324 def test_caching_stylesheet_link_tag_when_caching_on_with_proc_asset_host 325 ENV["RAILS_ASSET_ID"] = "" 326 ActionController::Base.asset_host = Proc.new { |source| "http://a#{source.length}.example.com" } 327 ActionController::Base.perform_caching = true 328 329 assert_equal '/stylesheets/styles.css'.length, 23 330 assert_dom_equal( 331 %(<link href="http://a23.example.com/stylesheets/styles.css" media="screen" rel="stylesheet" type="text/css" />), 332 stylesheet_link_tag(:all, :cache => 'styles') 333 ) 334 335 assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'styles.css')) 336 337 ensure 338 File.delete(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'styles.css')) 339 end 340 308 341 def test_caching_stylesheet_include_tag_when_caching_off 309 342 ENV["RAILS_ASSET_ID"] = ""