Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #10521: allow_procs_for_asset_host.2.diff

File allow_procs_for_asset_host.2.diff, 7.0 kB (added by chuyeow, 9 months ago)
  • test/template/asset_tag_helper_test.rb

    old new  
    223223    assert_equal copy, source 
    224224  end 
    225225 
    226  
    227226  def test_caching_javascript_include_tag_when_caching_on 
    228227    ENV["RAILS_ASSET_ID"] = "" 
    229228    ActionController::Base.asset_host = 'http://a%d.example.com' 
     
    247246    File.delete(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'all.js')) 
    248247    File.delete(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'money.js')) 
    249248  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 
    251267  def test_caching_javascript_include_tag_when_caching_on_and_using_subdirectory 
    252268    ENV["RAILS_ASSET_ID"] = "" 
    253269    ActionController::Base.asset_host = 'http://a%d.example.com' 
     
    304320    File.delete(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'all.css')) 
    305321    File.delete(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'money.css')) 
    306322  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 
    308341  def test_caching_stylesheet_include_tag_when_caching_off 
    309342    ENV["RAILS_ASSET_ID"] = "" 
    310343    ActionController::Base.perform_caching = false 
  • lib/action_view/helpers/asset_tag_helper.rb

    old new  
    3131    #   stylesheet_include_tag("application") 
    3232    #     => <link href="http://assets3.example.com/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" /> 
    3333    # 
    34     # To do this, you can either setup four actual hosts, or you can use wildcard DNS to CNAME  
     34    # To do this, you can either setup 4 actual hosts, or you can use wildcard DNS to CNAME  
    3535    # the wildcard to a single asset host.  You can read more about setting up your DNS CNAME records from 
    3636    # your ISP. 
    3737    # 
     
    3939    # for server load balancing. See http://www.die.net/musings/page_load_time/ 
    4040    # for background. 
    4141    # 
     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" /> 
     67    # 
    4268    # === Using asset timestamps 
    4369    # 
    4470    # By default, Rails will append all asset paths with that asset's timestamp. This allows you to set a cache-expiration date for the 
     
    385411        # Add the .ext if not present. Return full URLs otherwise untouched. 
    386412        # Prefix with /dir/ if lacking a leading /. Account for relative URL 
    387413        # 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. 
    390415        def compute_public_path(source, dir, ext = nil, include_host = true) 
    391416          has_request = @controller.respond_to?(:request) 
    392417 
    393418          cache_key = 
    394419            if has_request 
    395420              [ @controller.request.protocol, 
    396                 ActionController::Base.asset_host
     421                ActionController::Base.asset_host.to_s
    397422                @controller.request.relative_url_root, 
    398423                dir, source, ext, include_host ].join 
    399424            else 
    400               [ ActionController::Base.asset_host
     425              [ ActionController::Base.asset_host.to_s
    401426                dir, source, ext, include_host ].join 
    402427            end 
    403428 
     
    430455        end 
    431456 
    432457        # 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. 
    435461        def compute_asset_host(source) 
    436462          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 
    438468          end 
    439469        end 
    440470