Permalink
Browse files

Merge [5895], [5896] from trunk. References #6891.

git-svn-id: http://svn-commit.rubyonrails.org/rails/branches/1-2-pre-release@5897 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
jeremy committed Jan 12, 2007
1 parent 34603e7 commit 1a7ce06e90b83d10fb57f30086a9ebec726febfd
View
@@ -1,5 +1,7 @@
*SVN*
+* link_to_unless_current works with full URLs as well as paths. #6891 [Jarkko Laine, manfred, idrifter]
+
* Fix HTML::Node to output double quotes instead of single quotes. Closes #6845 [mitreandy]
* Fix no method error with error_messages_on. Closes #6935 [nik.wakelin Koz]
@@ -14,6 +16,7 @@
* [DOCS] fix reference to ActionController::Macros::AutoComplete for #text_field_with_auto_complete. Closes #2578 [Jan Prill]
+
*1.13.0 RC2* (r5847, January 4th, 2007)
* Make sure html_document is reset between integration test requests. [ctm]
@@ -135,19 +135,21 @@ def raw_post
@env['RAW_POST_DATA']
end
- # Returns the request URI correctly, taking into account the idiosyncracies
- # of the various servers.
+ # Return the request URI, accounting for server idiosyncracies.
+ # WEBrick includes the full URL. IIS leaves REQUEST_URI blank.
def request_uri
if uri = @env['REQUEST_URI']
- (%r{^\w+\://[^/]+(/.*|$)$} =~ uri) ? $1 : uri # Remove domain, which webrick puts into the request_uri.
- else # REQUEST_URI is blank under IIS - get this from PATH_INFO and SCRIPT_NAME
+ # Remove domain, which webrick puts into the request_uri.
+ (%r{^\w+\://[^/]+(/.*|$)$} =~ uri) ? $1 : uri
+ else
+ # Construct IIS missing REQUEST_URI from SCRIPT_NAME and PATH_INFO.
script_filename = @env['SCRIPT_NAME'].to_s.match(%r{[^/]+$})
uri = @env['PATH_INFO']
uri = uri.sub(/#{script_filename}\//, '') unless script_filename.nil?
unless (env_qs = @env['QUERY_STRING']).nil? || env_qs.empty?
uri << '?' << env_qs
end
- uri
+ @env['REQUEST_URI'] = uri
end
end
@@ -310,9 +310,15 @@ def mail_to(email_address, name = nil, html_options = {})
end
end
- # Returns true if the current page uri is generated by the +options+ passed.
+ # True if the current request uri was generated by the given +options+.
def current_page?(options)
- CGI.escapeHTML(self.url_for(options)) == @controller.request.request_uri
+ url_string = CGI.escapeHTML(url_for(options))
+ request = @controller.request
+ if url_string =~ /^\w+:\/\//
+ url_string == "#{request.protocol}#{request.host_with_port}#{request.request_uri}"
+ else
+ url_string == request.request_uri
+ end
end
private
@@ -174,48 +174,54 @@ def test_request_uri
assert_equal "/path/of/some/uri?mapped=1", @request.request_uri
assert_equal "/path/of/some/uri", @request.path
+ @request.set_REQUEST_URI nil
@request.relative_url_root = nil
@request.env['PATH_INFO'] = "/path/of/some/uri?mapped=1"
@request.env['SCRIPT_NAME'] = "/path/dispatch.rb"
assert_equal "/path/of/some/uri?mapped=1", @request.request_uri
assert_equal "/of/some/uri", @request.path
+ @request.set_REQUEST_URI nil
@request.relative_url_root = nil
@request.env['PATH_INFO'] = "/path/of/some/uri"
@request.env['SCRIPT_NAME'] = nil
assert_equal "/path/of/some/uri", @request.request_uri
assert_equal "/path/of/some/uri", @request.path
+ @request.set_REQUEST_URI nil
@request.relative_url_root = nil
@request.env['PATH_INFO'] = "/"
assert_equal "/", @request.request_uri
assert_equal "/", @request.path
+ @request.set_REQUEST_URI nil
@request.relative_url_root = nil
@request.env['PATH_INFO'] = "/?m=b"
assert_equal "/?m=b", @request.request_uri
assert_equal "/", @request.path
-
+
+ @request.set_REQUEST_URI nil
@request.relative_url_root = nil
@request.env['PATH_INFO'] = "/"
@request.env['SCRIPT_NAME'] = "/dispatch.cgi"
assert_equal "/", @request.request_uri
- assert_equal "/", @request.path
+ assert_equal "/", @request.path
+ @request.set_REQUEST_URI nil
@request.relative_url_root = nil
@request.env['PATH_INFO'] = "/hieraki/"
@request.env['SCRIPT_NAME'] = "/hieraki/dispatch.cgi"
assert_equal "/hieraki/", @request.request_uri
- assert_equal "/", @request.path
-
+ assert_equal "/", @request.path
+
@request.set_REQUEST_URI '/hieraki/dispatch.cgi'
@request.relative_url_root = '/hieraki'
- assert_equal "/dispatch.cgi", @request.path
+ assert_equal "/dispatch.cgi", @request.path
@request.relative_url_root = nil
@request.set_REQUEST_URI '/hieraki/dispatch.cgi'
@request.relative_url_root = '/foo'
- assert_equal "/hieraki/dispatch.cgi", @request.path
+ assert_equal "/hieraki/dispatch.cgi", @request.path
@request.relative_url_root = nil
# This test ensures that Rails uses REQUEST_URI over PATH_INFO
@@ -226,7 +232,7 @@ def test_request_uri
assert_equal "/some/path", @request.request_uri
assert_equal "/some/path", @request.path
end
-
+
def test_host_with_port
@request.host = "rubyonrails.org"
@@ -4,7 +4,7 @@
require File.dirname(__FILE__) + '/../../lib/action_view/helpers/asset_tag_helper'
require File.dirname(__FILE__) + '/../../lib/action_view/helpers/tag_helper'
-RequestMock = Struct.new("Request", :request_uri)
+RequestMock = Struct.new("Request", :request_uri, :protocol, :host_with_port)
class UrlHelperTest < Test::Unit::TestCase
include ActionView::Helpers::AssetTagHelper
@@ -202,12 +202,16 @@ def test_link_unless_current
@controller.request = RequestMock.new("http://www.example.com/weblog/show")
@controller.url = "http://www.example.com/weblog/show"
assert_equal "Showing", link_to_unless_current("Showing", { :action => "show", :controller => "weblog" })
+ assert_equal "Showing", link_to_unless_current("Showing", "http://www.example.com/weblog/show")
@controller.request = RequestMock.new("http://www.example.com/weblog/show")
@controller.url = "http://www.example.com/weblog/list"
- assert_equal "<a href=\"http://www.example.com/weblog/list\">Listing</a>", link_to_unless_current("Listing", :action => "list", :controller => "weblog")
+ assert_equal "<a href=\"http://www.example.com/weblog/list\">Listing</a>",
+ link_to_unless_current("Listing", :action => "list", :controller => "weblog")
+ assert_equal "<a href=\"http://www.example.com/weblog/list\">Listing</a>",
+ link_to_unless_current("Listing", "http://www.example.com/weblog/list")
end
-
+
def test_mail_to
assert_dom_equal "<a href=\"mailto:david@loudthinking.com\">david@loudthinking.com</a>", mail_to("david@loudthinking.com")
assert_dom_equal "<a href=\"mailto:david@loudthinking.com\">David Heinemeier Hansson</a>", mail_to("david@loudthinking.com", "David Heinemeier Hansson")
@@ -301,3 +305,62 @@ def with_url_helper_routing
end
end
end
+
+class LinkToUnlessCurrentWithControllerTest < Test::Unit::TestCase
+ class TasksController < ActionController::Base
+ self.template_root = "#{File.dirname(__FILE__)}/../fixtures/"
+
+ def self.controller_path; 'tasks' end
+
+ def index
+ render_default
+ end
+
+ def show
+ render_default
+ end
+
+ def rescue_action(e) raise e end
+
+ protected
+ def render_default
+ render :inline =>
+ "<%= link_to_unless_current(\"tasks\", tasks_path) %>\n" +
+ "<%= link_to_unless_current(\"tasks\", tasks_url) %>"
+ end
+ end
+
+ include ActionView::Helpers::UrlHelper
+
+ def setup
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+ @controller = TasksController.new
+ end
+
+ def test_link_to_unless_current_to_current
+ with_restful_routing do
+ get :index
+ assert_equal "tasks\ntasks", @response.body
+ end
+ end
+
+ def test_link_to_unless_current_shows_link
+ with_restful_routing do
+ get :show, :id => 1
+ assert_equal "<a href=\"/tasks\">tasks</a>\n" +
+ "<a href=\"#{@request.protocol}#{@request.host_with_port}/tasks\">tasks</a>",
+ @response.body
+ end
+ end
+
+ protected
+ def with_restful_routing
+ with_routing do |set|
+ set.draw do |map|
+ map.resources :tasks
+ end
+ yield
+ end
+ end
+end

0 comments on commit 1a7ce06

Please sign in to comment.