Changeset 8616
- Timestamp:
- 01/10/08 02:51:09 (1 year ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (1 diff)
- trunk/actionpack/lib/action_controller/url_rewriter.rb (modified) (5 diffs)
- trunk/actionpack/test/controller/url_rewriter_test.rb (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r8578 r8616 1 1 *SVN* 2 3 * UrlWriter respects relative_url_root. #10748 [Cheah Chu Yeow] 2 4 3 5 * 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 1 module ActionController 2 2 # Write URLs from arbitrary places in your codebase, such as your mailers. 3 # 3 # 4 4 # Example: 5 # 5 # 6 6 # class MyMailer 7 7 # include ActionController::UrlWriter 8 8 # default_url_options[:host] = 'www.basecamphq.com' 9 # 9 # 10 10 # def signup_url(token) 11 11 # url_for(:controller => 'signup', action => 'index', :token => token) 12 12 # end 13 13 # end 14 # 14 # 15 15 # In addition to providing +url_for+, named routes are also accessible after 16 16 # including UrlWriter. … … 20 20 mattr_accessor :default_url_options 21 21 self.default_url_options = {} 22 22 23 23 def self.included(base) #:nodoc: 24 ActionController::Routing::Routes.install_helpers base24 ActionController::Routing::Routes.install_helpers(base) 25 25 base.mattr_accessor :default_url_options 26 26 base.default_url_options ||= default_url_options 27 27 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 30 30 # routes defined in routes.rb. The following options are supported: 31 # 31 # 32 32 # * <tt>:only_path</tt> If true, the relative url is returned. Defaults to false. 33 33 # * <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. 36 36 # * <tt>:port</tt> Optionally specify the port to connect to. 37 37 # * <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 # 39 40 # Any other key(:controller, :action, etc...) given to <tt>url_for</tt> is forwarded to the Routes module. 40 # 41 # 41 42 # Examples: 42 # 43 # 43 44 # url_for :controller => 'tasks', :action => 'testing', :host=>'somehost.org', :port=>'8080' # => 'http://somehost.org:8080/tasks/testing' 44 45 # url_for :controller => 'tasks', :action => 'testing', :host=>'somehost.org', :anchor => 'ok', :only_path => true # => '/tasks/testing#ok' 45 46 # url_for :controller => 'tasks', :action => 'testing', :host=>'somehost.org', :number => '33' # => 'http://somehost.org/tasks/testing?number=33' 46 #47 47 def url_for(options) 48 48 options = self.class.default_url_options.merge(options) 49 49 50 50 url = '' 51 51 52 unless options.delete :only_path52 unless options.delete(:only_path) 53 53 url << (options.delete(:protocol) || 'http') 54 url << '://' unless url.match("://") #dont add separator if its already been specified in :protocol55 54 url << '://' unless url.match("://") 55 56 56 raise "Missing host to link to! Please provide :host parameter or set default_url_options[:host]" unless options[:host] 57 57 … … 59 59 url << ":#{options.delete(:port)}" if options.key?(:port) 60 60 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) } 63 63 end 64 64 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] 66 67 url << Routing::Routes.generate(options, {}) 67 68 url << anchor if anchor 68 69 69 returnurl70 end 70 url 71 end 71 72 end 72 73 73 74 # Rewrites URLs for Base.redirect_to and Base.url_for in the controller. 74 75 class UrlRewriter #:nodoc: … … 77 78 @request, @parameters = request, parameters 78 79 end 79 80 80 81 def rewrite(options = {}) 81 82 rewrite_url(options) … … 124 125 Routing::Routes.generate(options, @request.symbolized_path_parameters) 125 126 end 126 127 127 128 def rewrite_authentication(options) 128 129 if options[:user] && options[:password] trunk/actionpack/test/controller/url_rewriter_test.rb
r8564 r8616 150 150 end 151 151 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 153 165 ActionController::Routing::Routes.draw do |map| 154 166 map.no_args '/this/is/verbose', :controller => 'home', :action => 'index' … … 156 168 map.connect ':controller/:action/:id' 157 169 end 158 170 159 171 # We need to create a new class in order to install the new named route. 160 172 kls = Class.new { include ActionController::UrlWriter } … … 163 175 assert_equal 'http://www.basecamphq.com/home/sweet/home/again', 164 176 controller.send(:home_url, :host => 'www.basecamphq.com', :user => 'again') 165 177 166 178 assert_equal("/home/sweet/home/alabama", controller.send(:home_path, :user => 'alabama', :host => 'unused')) 167 179 assert_equal("http://www.basecamphq.com/home/sweet/home/alabama", controller.send(:home_url, :user => 'alabama', :host => 'www.basecamphq.com')) … … 170 182 ActionController::Routing::Routes.load! 171 183 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 173 203 def test_only_path 174 204 ActionController::Routing::Routes.draw do |map|