diff options
-rw-r--r-- | actionpack/CHANGELOG | 2 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/url_helper.rb | 10 | ||||
-rw-r--r-- | actionpack/test/template/url_helper_test.rb | 69 |
3 files changed, 76 insertions, 5 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index a0d2015f20..bc1dd897e1 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* link_to_unless_current works with full URLs as well as paths. #6891 [Jarkko Laine, manfred, idrifter] + * Lookup the mime type for #auto_discovery_link_tag in the Mime::Type class. Closes #6941 [Josh Peek] * Fix bug where nested resources ignore a parent singleton parent's path prefix. Closes #6940 [Dan Kubb] diff --git a/actionpack/lib/action_view/helpers/url_helper.rb b/actionpack/lib/action_view/helpers/url_helper.rb index 21b5e9075c..6a66585387 100644 --- a/actionpack/lib/action_view/helpers/url_helper.rb +++ b/actionpack/lib/action_view/helpers/url_helper.rb @@ -310,9 +310,15 @@ module ActionView 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 diff --git a/actionpack/test/template/url_helper_test.rb b/actionpack/test/template/url_helper_test.rb index a2cae00489..4a772d5069 100644 --- a/actionpack/test/template/url_helper_test.rb +++ b/actionpack/test/template/url_helper_test.rb @@ -4,7 +4,7 @@ require File.dirname(__FILE__) + '/../../lib/action_view/helpers/url_helper' 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 @@ class UrlHelperTest < Test::Unit::TestCase @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 @@ class UrlHelperWithControllerTest < Test::Unit::TestCase 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 |