From 283e1e311986bd0b40b607132f61fff69365fb81 Mon Sep 17 00:00:00 2001 From: Erik Andrejko Date: Sun, 26 Oct 2008 11:46:17 -0500 Subject: [PATCH] modified current_page? to ignore extra parameters unless specified in options --- actionpack/lib/action_view/helpers/url_helper.rb | 15 ++++++++++++--- actionpack/test/template/url_helper_test.rb | 10 ++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/actionpack/lib/action_view/helpers/url_helper.rb b/actionpack/lib/action_view/helpers/url_helper.rb index beb6b7a..02128d6 100644 --- a/actionpack/lib/action_view/helpers/url_helper.rb +++ b/actionpack/lib/action_view/helpers/url_helper.rb @@ -468,7 +468,7 @@ module ActionView # True if the current request URI was generated by the given +options+. # # ==== Examples - # Let's say we're in the /shop/checkout action. + # Let's say we're in the /shop/checkout?order=desc action. # # current_page?(:action => 'process') # # => false @@ -476,6 +476,9 @@ module ActionView # current_page?(:controller => 'shop', :action => 'checkout') # # => true # + # current_page?(:controller => 'shop', :action => 'checkout', :order => 'asc) + # # => false + # # current_page?(:action => 'checkout') # # => true # @@ -484,10 +487,16 @@ module ActionView def current_page?(options) url_string = CGI.escapeHTML(url_for(options)) request = @controller.request + if url_string.index("?") + request_uri = request.request_uri + else + #ignore any extra parameters + request_uri = request.request_uri.split('?').first + end if url_string =~ /^\w+:\/\// - url_string == "#{request.protocol}#{request.host_with_port}#{request.request_uri}" + url_string == "#{request.protocol}#{request.host_with_port}#{request_uri}" else - url_string == request.request_uri + url_string == request_uri end end diff --git a/actionpack/test/template/url_helper_test.rb b/actionpack/test/template/url_helper_test.rb index 9c4a09b..5723063 100644 --- a/actionpack/test/template/url_helper_test.rb +++ b/actionpack/test/template/url_helper_test.rb @@ -239,6 +239,16 @@ class UrlHelperTest < ActionView::TestCase 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?order=desc") + @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?order=desc") + @controller.url = "http://www.example.com/weblog/show?order=asc" + 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?order=asc") + @controller.request = RequestMock.new("http://www.example.com/weblog/show") @controller.url = "http://www.example.com/weblog/list" assert_equal "Listing", -- 1.5.5.1