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

Changeset 8616

Show
Ignore:
Timestamp:
01/10/08 02:51:09 (1 year ago)
Author:
bitsweat
Message:

UrlWriter respects relative_url_root. Closes #10748.

Files:

Legend:

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

    r8578 r8616  
    11*SVN* 
     2 
     3* UrlWriter respects relative_url_root.  #10748 [Cheah Chu Yeow] 
    24 
    35* 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] 
  • trunk/actionpack/lib/action_controller/url_rewriter.rb

    r7673 r8616  
    1 module ActionController   
     1module ActionController 
    22  # Write URLs from arbitrary places in your codebase, such as your mailers. 
    3   #  
     3  # 
    44  # Example: 
    5   #  
     5  # 
    66  #   class MyMailer 
    77  #     include ActionController::UrlWriter 
    88  #     default_url_options[:host] = 'www.basecamphq.com' 
    9   #      
     9  # 
    1010  #     def signup_url(token) 
    1111  #       url_for(:controller => 'signup', action => 'index', :token => token) 
    1212  #     end 
    1313  #  end 
    14   #  
     14  # 
    1515  # In addition to providing +url_for+, named routes are also accessible after 
    1616  # including UrlWriter. 
     
    2020    mattr_accessor :default_url_options 
    2121    self.default_url_options = {} 
    22      
     22 
    2323    def self.included(base) #:nodoc: 
    24       ActionController::Routing::Routes.install_helpers base  
     24      ActionController::Routing::Routes.install_helpers(base) 
    2525      base.mattr_accessor :default_url_options 
    2626      base.default_url_options ||= default_url_options 
    2727    end 
    28      
    29     # Generate a url based on the options provided, default_url_options and the  
     28 
     29    # Generate a url based on the options provided, default_url_options and the 
    3030    # routes defined in routes.rb.  The following options are supported: 
    31     #  
     31    # 
    3232    # * <tt>:only_path</tt> If true, the relative url is returned. Defaults to false. 
    3333    # * <tt>:protocol</tt> The protocol to connect to. Defaults to 'http'. 
    34     # * <tt>:host</tt> Specifies the host the link should be targetted at. If <tt>:only_path</tt> is false, this option must be  
    35     #   provided either explicitly, or via default_url_options.  
     34    # * <tt>:host</tt> Specifies the host the link should be targetted at. If <tt>:only_path</tt> is false, this option must be 
     35    #   provided either explicitly, or via default_url_options. 
    3636    # * <tt>:port</tt> Optionally specify the port to connect to. 
    3737    # * <tt>:anchor</tt> An anchor name to be appended to the path. 
    38     #  
     38    # * <tt>:skip_relative_url_root</tt> If true, the url is not constructed using the relative_url_root set in <tt>ActionController::AbstractRequest.relative_url_root</tt>. 
     39    # 
    3940    # Any other key(:controller, :action, etc...) given to <tt>url_for</tt> is forwarded to the Routes module. 
    40     #  
     41    # 
    4142    # Examples: 
    42     #  
     43    # 
    4344    #    url_for :controller => 'tasks', :action => 'testing', :host=>'somehost.org', :port=>'8080'    # => 'http://somehost.org:8080/tasks/testing' 
    4445    #    url_for :controller => 'tasks', :action => 'testing', :host=>'somehost.org', :anchor => 'ok', :only_path => true    # => '/tasks/testing#ok' 
    4546    #    url_for :controller => 'tasks', :action => 'testing', :host=>'somehost.org', :number => '33'  # => 'http://somehost.org/tasks/testing?number=33' 
    46     #  
    4747    def url_for(options) 
    4848      options = self.class.default_url_options.merge(options) 
    49        
     49 
    5050      url = '' 
    5151 
    52       unless options.delete :only_path 
     52      unless options.delete(:only_path) 
    5353        url << (options.delete(:protocol) || 'http') 
    54         url << '://' unless url.match("://") #dont add separator if its already been specified in :protocol  
    55          
     54        url << '://' unless url.match("://") 
     55 
    5656        raise "Missing host to link to! Please provide :host parameter or set default_url_options[:host]" unless options[:host] 
    5757 
     
    5959        url << ":#{options.delete(:port)}" if options.key?(:port) 
    6060      else 
    61         # Delete the unused options to prevent their appearance in the query string 
    62         [:protocol, :host, :port].each { |k| options.delete k
     61        # Delete the unused options to prevent their appearance in the query string. 
     62        [:protocol, :host, :port, :skip_relative_url_root].each { |k| options.delete(k)
    6363      end 
    6464 
    65       anchor = "##{CGI.escape options.delete(:anchor).to_param.to_s}" if options.key?(:anchor) 
     65      url << ActionController::AbstractRequest.relative_url_root.to_s unless options[:skip_relative_url_root] 
     66      anchor = "##{CGI.escape options.delete(:anchor).to_param.to_s}" if options[:anchor] 
    6667      url << Routing::Routes.generate(options, {}) 
    6768      url << anchor if anchor 
    6869 
    69       return url 
    70     end     
     70      url 
     71    end 
    7172  end 
    72    
     73 
    7374  # Rewrites URLs for Base.redirect_to and Base.url_for in the controller. 
    7475  class UrlRewriter #:nodoc: 
     
    7778      @request, @parameters = request, parameters 
    7879    end 
    79      
     80 
    8081    def rewrite(options = {}) 
    8182      rewrite_url(options) 
     
    124125        Routing::Routes.generate(options, @request.symbolized_path_parameters) 
    125126      end 
    126        
     127 
    127128      def rewrite_authentication(options) 
    128129        if options[:user] && options[:password] 
  • trunk/actionpack/test/controller/url_rewriter_test.rb

    r8564 r8616  
    150150  end 
    151151 
    152   def test_named_route 
     152  def test_relative_url_root_is_respected 
     153    orig_relative_url_root = ActionController::AbstractRequest.relative_url_root 
     154    ActionController::AbstractRequest.relative_url_root = '/subdir' 
     155 
     156    add_host! 
     157    assert_equal('https://www.basecamphq.com/subdir/c/a/i', 
     158      W.new.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => 'https') 
     159    ) 
     160  ensure 
     161    ActionController::AbstractRequest.relative_url_root = orig_relative_url_root 
     162  end 
     163 
     164  def test_named_routes 
    153165    ActionController::Routing::Routes.draw do |map| 
    154166      map.no_args '/this/is/verbose', :controller => 'home', :action => 'index' 
     
    156168      map.connect ':controller/:action/:id' 
    157169    end 
    158      
     170 
    159171    # We need to create a new class in order to install the new named route. 
    160172    kls = Class.new { include ActionController::UrlWriter } 
     
    163175    assert_equal 'http://www.basecamphq.com/home/sweet/home/again', 
    164176      controller.send(:home_url, :host => 'www.basecamphq.com', :user => 'again') 
    165        
     177 
    166178    assert_equal("/home/sweet/home/alabama", controller.send(:home_path, :user => 'alabama', :host => 'unused')) 
    167179    assert_equal("http://www.basecamphq.com/home/sweet/home/alabama", controller.send(:home_url, :user => 'alabama', :host => 'www.basecamphq.com')) 
     
    170182    ActionController::Routing::Routes.load! 
    171183  end 
    172    
     184 
     185  def test_relative_url_root_is_respected_for_named_routes 
     186    orig_relative_url_root = ActionController::AbstractRequest.relative_url_root 
     187    ActionController::AbstractRequest.relative_url_root = '/subdir' 
     188 
     189    ActionController::Routing::Routes.draw do |map| 
     190      map.home '/home/sweet/home/:user', :controller => 'home', :action => 'index' 
     191    end 
     192 
     193    kls = Class.new { include ActionController::UrlWriter } 
     194    controller = kls.new 
     195 
     196    assert_equal 'http://www.basecamphq.com/subdir/home/sweet/home/again', 
     197      controller.send(:home_url, :host => 'www.basecamphq.com', :user => 'again') 
     198  ensure 
     199    ActionController::Routing::Routes.load! 
     200    ActionController::AbstractRequest.relative_url_root = orig_relative_url_root 
     201  end 
     202 
    173203  def test_only_path 
    174204    ActionController::Routing::Routes.draw do |map|