Skip to content
This repository
Browse code

Added the option to declare an asset_host as an object that responds …

…to call (see http://github.com/dhh/asset-hosting-with-minimum-ssl for an example) [DHH]
  • Loading branch information...
commit 229f959d15e451890db60dbb73f8565079977814 1 parent 3cc9d1c
David Heinemeier Hansson authored November 27, 2008
2  actionpack/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *2.3.0 [Edge]*
2 2
 
  3
+* Added the option to declare an asset_host as an object that responds to call (see http://github.com/dhh/asset-hosting-with-minimum-ssl for an example) [DHH]
  4
+
3 5
 * Added support for multiple routes.rb files (useful for plugin engines). This also means that draw will no longer clear the route set, you have to do that by hand (shouldn't make a difference to you unless you're doing some funky stuff) [DHH]
4 6
 
5 7
 * Dropped formatted_* routes in favor of just passing in :format as an option. This cuts resource routes generation in half #1359 [aaronbatalion]
14  actionpack/lib/action_view/helpers/asset_tag_helper.rb
@@ -80,6 +80,12 @@ module Helpers #:nodoc:
80 80
     #     end
81 81
     #   }
82 82
     #
  83
+    # You can also implement a custom asset host object that responds to the call method and tasks one or two parameters just like the proc.
  84
+    #
  85
+    #   config.action_controller.asset_host = AssetHostingWithMinimumSsl.new(
  86
+    #     "http://asset%d.example.com", "https://asset1.example.com"
  87
+    #   )
  88
+    #
83 89
     # === Using asset timestamps
84 90
     #
85 91
     # By default, Rails will append all asset paths with that asset's timestamp. This allows you to set a cache-expiration date for the
@@ -359,6 +365,7 @@ def stylesheet_path(source)
359 365
       # compressed by gzip (leading to faster transfers). Caching will only happen if ActionController::Base.perform_caching
360 366
       # is set to true (which is the case by default for the Rails production environment, but not for the development
361 367
       # environment). Examples:
  368
+      
362 369
       #
363 370
       # ==== Examples
364 371
       #   stylesheet_link_tag :all, :cache => true # when ActionController::Base.perform_caching is false =>
@@ -629,11 +636,12 @@ def prepend_asset_host(source)
629 636
             # Pick an asset host for this source. Returns +nil+ if no host is set,
630 637
             # the host if no wildcard is set, the host interpolated with the
631 638
             # numbers 0-3 if it contains <tt>%d</tt> (the number is the source hash mod 4),
632  
-            # or the value returned from invoking the proc if it's a proc.
  639
+            # or the value returned from invoking the proc if it's a proc or the value from
  640
+            # invoking call if it's an object responding to call.
633 641
             def compute_asset_host(source)
634 642
               if host = ActionController::Base.asset_host
635  
-                if host.is_a?(Proc)
636  
-                  case host.arity
  643
+                if host.is_a?(Proc) || host.respond_to?(:call)
  644
+                  case host.is_a?(Proc) ? host.arity : host.method(:call).arity
637 645
                   when 2
638 646
                     host.call(source, request)
639 647
                   else
40  actionpack/test/template/asset_tag_helper_test.rb
@@ -359,6 +359,46 @@ def ssl?() true end
359 359
     FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'secure.js'))
360 360
   end
361 361
 
  362
+  def test_caching_javascript_include_tag_when_caching_on_with_2_argument_object_asset_host
  363
+    ENV['RAILS_ASSET_ID'] = ''
  364
+    ActionController::Base.asset_host = Class.new do
  365
+      def call(source, request)
  366
+        if request.ssl?
  367
+          "#{request.protocol}#{request.host_with_port}"
  368
+        else
  369
+          "#{request.protocol}assets#{source.length}.example.com"
  370
+        end
  371
+      end
  372
+    end.new
  373
+
  374
+    ActionController::Base.perform_caching = true
  375
+
  376
+    assert_equal '/javascripts/vanilla.js'.length, 23
  377
+    assert_dom_equal(
  378
+      %(<script src="http://assets23.example.com/javascripts/vanilla.js" type="text/javascript"></script>),
  379
+      javascript_include_tag(:all, :cache => 'vanilla')
  380
+    )
  381
+
  382
+    assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'vanilla.js'))
  383
+
  384
+    class << @controller.request
  385
+      def protocol() 'https://' end
  386
+      def ssl?() true end
  387
+    end
  388
+
  389
+    assert_equal '/javascripts/secure.js'.length, 22
  390
+    assert_dom_equal(
  391
+      %(<script src="https://localhost/javascripts/secure.js" type="text/javascript"></script>),
  392
+      javascript_include_tag(:all, :cache => 'secure')
  393
+    )
  394
+
  395
+    assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'secure.js'))
  396
+
  397
+  ensure
  398
+    FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'vanilla.js'))
  399
+    FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'secure.js'))
  400
+  end
  401
+
362 402
   def test_caching_javascript_include_tag_when_caching_on_and_using_subdirectory
363 403
     ENV["RAILS_ASSET_ID"] = ""
364 404
     ActionController::Base.asset_host = 'http://a%d.example.com'

3 notes on commit 229f959

This is awesome

Tekin Suleyman

Nice stuff. Have you had any more thoughts on ActionMailer views lacking a request object causing compute_asset_host to bork?

Michael Koziarski
Owner

@tekin: I like your patches on the list, just running about a million years behind there. I’ll get to it :)

Please sign in to comment.
Something went wrong with that request. Please try again.