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

Changeset 8578

Show
Ignore:
Timestamp:
01/06/08 20:53:23 (1 year ago)
Author:
bitsweat
Message:

The asset_host block takes the controller request as an optional second argument. Example: use a single asset host for SSL requests. Closes #10549.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r8577 r8578  
    11*SVN* 
     2 
     3* The asset_host block takes the controller request as an optional second argument. Example: use a single asset host for SSL requests.  #10549 [Cheah Chu Yeow, Peter B, Tom Taylor] 
    24 
    35* Support render :text => nil.  #6684 [tjennings, PotatoSalad, Cheah Chu Yeow] 
  • trunk/actionpack/lib/action_controller/cgi_process.rb

    r8176 r8578  
    44module ActionController #:nodoc: 
    55  class Base 
    6     # Process a request extracted from an CGI object and return a response. Pass false as <tt>session_options</tt> to disable 
     6    # Process a request extracted from a CGI object and return a response. Pass false as <tt>session_options</tt> to disable 
    77    # sessions (large performance increase if sessions are not needed). The <tt>session_options</tt> are the same as for CGI::Session: 
    88    # 
     
    1818    # * <tt>:session_expires</tt> - the time the current session expires, as a +Time+ object.  If not set, the session will continue 
    1919    #   indefinitely. 
    20     # * <tt>:session_domain</tt> - the hostname domain for which this session is valid. If not set, defaults to the hostname of the 
     20    # * <tt>:session_domain</tt> - the hostname domain for which this session is valid. If not set, defaults to the hostname of the 
    2121    #   server. 
    2222    # * <tt>:session_secure</tt> - if +true+, this session will only work over HTTPS. 
     
    3535  class CgiRequest < AbstractRequest #:nodoc: 
    3636    attr_accessor :cgi, :session_options 
    37     class SessionFixationAttempt < StandardError; end #:nodoc: 
     37    class SessionFixationAttempt < StandardError #:nodoc: 
     38    end 
    3839 
    3940    DEFAULT_SESSION_OPTIONS = { 
  • trunk/actionpack/lib/action_view/helpers/asset_tag_helper.rb

    r8421 r8578  
    5151    #     => <link href="http://assets1.example.com/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" /> 
    5252    # 
    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. 
     53    # The proc takes a <tt>source</tt> parameter (which is the path of the source asset) and an optional 
     54    # <tt>request</tt> parameter (which is an entire instance of an <tt>ActionController::AbstractRequest</tt> 
     55    # subclass). This can be used to generate a particular asset host depending on the asset path and the particular 
     56    # request. 
    5557    # 
    5658    #    ActionController::Base.asset_host = Proc.new { |source| 
     
    6567    #   stylesheet_include_tag("application") 
    6668    #     => <link href="http://assets.example.com/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" /> 
     69    # 
     70    # The optional <tt>request</tt> parameter to the proc is useful in particular for serving assets from an 
     71    # SSL-protected page. The example proc below disables asset hosting for HTTPS connections, while still sending 
     72    # assets for plain HTTP requests from asset hosts. This is useful for avoiding mixed media warnings when serving 
     73    # non-HTTP assets from HTTPS web pages when you don't have an SSL certificate for each of the asset hosts. 
     74    # 
     75    #   ActionController::Base.asset_host = Proc.new { |source, request| 
     76    #     if request.ssl? 
     77    #       "#{request.protocol}#{request.host_with_port}" 
     78    #     else 
     79    #       "#{request.protocol}assets.example.com" 
     80    #     end 
     81    #   } 
    6782    # 
    6883    # === Using asset timestamps 
     
    462477          if host = ActionController::Base.asset_host 
    463478            if host.is_a?(Proc) 
    464               host.call(source) 
     479              case host.arity 
     480              when 2: 
     481                host.call(source, @controller.request) 
     482              else 
     483                host.call(source) 
     484              end 
    465485            else 
    466486              host % (source.hash % 4) 
  • trunk/actionpack/lib/action_view/template_handlers/erb.rb

    r8422 r8578  
    55    HTML_ESCAPE = { '&' => '&amp;', '"' => '&quot;', '>' => '&gt;', '<' => '&lt;' } 
    66 
     7    # A utility method for escaping HTML tag characters. 
     8    # This method is also aliased as <tt>h</tt>. 
     9    # 
     10    # In your ERb templates, use this method to escape any unsafe content. For example: 
     11    #   <%=h @person.name %> 
     12    # 
     13    # ==== Example: 
     14    #   puts html_escape("is a > 0 & a < 10?") 
     15    #   # => is a &gt; 0 &amp; a &lt; 10? 
    716    def html_escape(s) 
    817      s.to_s.gsub(/[&"><]/) { |special| HTML_ESCAPE[special] } 
  • trunk/actionpack/test/template/asset_tag_helper_test.rb

    r8564 r8578  
    3535      def relative_url_root() "" end 
    3636      def protocol() 'http://' end 
     37      def ssl?() false end 
     38      def host_with_port() 'localhost' end 
    3739    end.new 
    3840 
     
    249251 
    250252  def test_caching_javascript_include_tag_when_caching_on_with_proc_asset_host 
    251     ENV["RAILS_ASSET_ID"] = "" 
     253    ENV['RAILS_ASSET_ID'] = '' 
    252254    ActionController::Base.asset_host = Proc.new { |source| "http://a#{source.length}.example.com" } 
    253255    ActionController::Base.perform_caching = true 
     
    263265  ensure 
    264266    FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'scripts.js')) 
     267  end 
     268 
     269  def test_caching_javascript_include_tag_when_caching_on_with_2_argument_proc_asset_host 
     270    ENV['RAILS_ASSET_ID'] = '' 
     271    ActionController::Base.asset_host = Proc.new { |source, request| 
     272      if request.ssl? 
     273        "#{request.protocol}#{request.host_with_port}" 
     274      else 
     275        "#{request.protocol}assets#{source.length}.example.com" 
     276      end 
     277    } 
     278    ActionController::Base.perform_caching = true 
     279 
     280    assert_equal '/javascripts/vanilla.js'.length, 23 
     281    assert_dom_equal( 
     282      %(<script src="http://assets23.example.com/javascripts/vanilla.js" type="text/javascript"></script>), 
     283      javascript_include_tag(:all, :cache => 'vanilla') 
     284    ) 
     285 
     286    assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'vanilla.js')) 
     287 
     288    class << @controller.request 
     289      def protocol() 'https://' end 
     290      def ssl?() true end 
     291    end 
     292 
     293    assert_equal '/javascripts/secure.js'.length, 22 
     294    assert_dom_equal( 
     295      %(<script src="https://localhost/javascripts/secure.js" type="text/javascript"></script>), 
     296      javascript_include_tag(:all, :cache => 'secure') 
     297    ) 
     298 
     299    assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'secure.js')) 
     300 
     301  ensure 
     302    FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'vanilla.js')) 
     303    FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'secure.js')) 
    265304  end 
    266305