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

Ticket #10549: asset_hosts_proc_with_2nd_request_parameter.diff

File asset_hosts_proc_with_2nd_request_parameter.diff, 7.8 kB (added by chuyeow, 6 months ago)
  • actionpack/test/template/asset_tag_helper_test.rb

    old new  
    3434    @request = Class.new do 
    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 
    3941    @controller.request = @request 
     
    248250  end 
    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 
    254256 
     
    264266    FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'scripts.js')) 
    265267  end 
    266268 
     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')) 
     304  end 
     305 
    267306  def test_caching_javascript_include_tag_when_caching_on_and_using_subdirectory 
    268307    ENV["RAILS_ASSET_ID"] = "" 
    269308    ActionController::Base.asset_host = 'http://a%d.example.com' 
  • actionpack/lib/action_controller/cgi_process.rb

    old new  
    33 
    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    # 
    99    # * <tt>:database_manager</tt> - standard options are CGI::Session::FileStore, CGI::Session::MemoryStore, and CGI::Session::PStore 
     
    1717    #   an ArgumentError is raised. 
    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. 
    2323    # * <tt>:session_path</tt> - the path for which this session applies.  Defaults to the directory of the CGI script. 
     
    3434 
    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 = { 
    4041      :database_manager => CGI::Session::CookieStore, # store data in cookie 
  • actionpack/lib/action_view/helpers/asset_tag_helper.rb

    old new  
    5050    #   stylesheet_include_tag("application") 
    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| 
    5759    #      if source.starts_with?('/images') 
     
    6567    #   stylesheet_include_tag("application") 
    6668    #     => <link href="http://assets.example.com/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" /> 
    6769    # 
     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    #   } 
     82    # 
    6883    # === Using asset timestamps 
    6984    # 
    7085    # By default, Rails will append all asset paths with that asset's timestamp. This allows you to set a cache-expiration date for the 
     
    461476        def compute_asset_host(source) 
    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) 
    467487            end 
  • actionpack/lib/action_view/template_handlers/erb.rb

    old new  
    44  module Util 
    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] } 
    918    end