public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
modified current_page? to ignore extra parameters unless specified in options

Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#805 state:committed]
eandrejko (author)
Sun Oct 26 09:46:17 -0700 2008
NZKoz (committer)
Sun Oct 26 11:14:45 -0700 2008
commit  ef9b6b5cba08f13dcbf7095226b78aaf22df13f7
tree    d9b332eba71925ceb524fbf638369bafe19d8786
parent  2c7abe1b5682d287b19dde5900087908c976109c
...
499
500
501
502
 
503
504
505
...
507
508
509
 
 
 
510
511
512
...
515
516
517
 
 
 
 
 
 
 
 
518
519
 
520
521
 
522
523
524
...
499
500
501
 
502
503
504
505
...
507
508
509
510
511
512
513
514
515
...
518
519
520
521
522
523
524
525
526
527
528
529
 
530
531
 
532
533
534
535
0
@@ -499,7 +499,7 @@ module ActionView
0
       # True if the current request URI was generated by the given +options+.
0
       #
0
       # ==== Examples
0
-      # Let's say we're in the <tt>/shop/checkout</tt> action.
0
+      # Let's say we're in the <tt>/shop/checkout?order=desc</tt> action.
0
       #
0
       #   current_page?(:action => 'process')
0
       #   # => false
0
@@ -507,6 +507,9 @@ module ActionView
0
       #   current_page?(:controller => 'shop', :action => 'checkout')
0
       #   # => true
0
       #
0
+      #   current_page?(:controller => 'shop', :action => 'checkout', :order => 'asc)
0
+      #   # => false
0
+      #
0
       #   current_page?(:action => 'checkout')
0
       #   # => true
0
       #
0
@@ -515,10 +518,18 @@ module ActionView
0
       def current_page?(options)
1
         url_string = CGI.escapeHTML(url_for(options))
0
         request = @controller.request
0
+        # We ignore any extra parameters in the request_uri if the 
0
+        # submitted url doesn't have any either.  This lets the function
0
+        # work with things like ?order=asc 
0
+        if url_string.index("?")
0
+          request_uri = request.request_uri
0
+        else
0
+          request_uri = request.request_uri.split('?').first
0
+        end
0
         if url_string =~ /^\w+:\/\//
0
-          url_string == "#{request.protocol}#{request.host_with_port}#{request.request_uri}"
0
+          url_string == "#{request.protocol}#{request.host_with_port}#{request_uri}"
0
         else
0
-          url_string == request.request_uri
0
+          url_string == request_uri
0
         end
0
       end
0
 
...
258
259
260
 
 
 
 
 
 
 
 
 
 
261
262
263
...
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
0
@@ -258,6 +258,16 @@ class UrlHelperTest < ActionView::TestCase
0
     assert_equal "Showing", link_to_unless_current("Showing", { :action => "show", :controller => "weblog" })
0
     assert_equal "Showing", link_to_unless_current("Showing", "http://www.example.com/weblog/show")
0
 
0
+    @controller.request = RequestMock.new("http://www.example.com/weblog/show?order=desc")
0
+    @controller.url = "http://www.example.com/weblog/show"
0
+    assert_equal "Showing", link_to_unless_current("Showing", { :action => "show", :controller => "weblog" })
0
+    assert_equal "Showing", link_to_unless_current("Showing", "http://www.example.com/weblog/show")
0
+
0
+    @controller.request = RequestMock.new("http://www.example.com/weblog/show?order=desc")
0
+    @controller.url = "http://www.example.com/weblog/show?order=asc"
0
+    assert_equal "<a href=\"http://www.example.com/weblog/show?order=asc\">Showing</a>", link_to_unless_current("Showing", { :action => "show", :controller => "weblog" })
0
+    assert_equal "<a href=\"http://www.example.com/weblog/show?order=asc\">Showing</a>", link_to_unless_current("Showing", "http://www.example.com/weblog/show?order=asc")
0
+
0
     @controller.request = RequestMock.new("http://www.example.com/weblog/show")
0
     @controller.url = "http://www.example.com/weblog/list"
0
     assert_equal "<a href=\"http://www.example.com/weblog/list\">Listing</a>",

Comments

cs Mon Oct 27 09:01:29 -0700 2008

Thank you very much! This is really useful ;)

I may be being dense here but shouldn’t this be CGI.unescapeHTML otherwise we’re double escaping the generated url and it stands no chance of being equal to the request uri if there’s an ampersand in the url.

NZKoz Fri Nov 14 01:09:00 -0800 2008

@pixeltrix: That code has been there a while, it’s not new with this changeset.

But anyway, the query-string is ignored now, so it shouldn’t matter right?

The source of it is:

http://github.com/rails/rails/commit/3dc7f7603785c1173df5c24f8b76323f7a795443

pixeltrix Sat Nov 15 22:29:52 -0800 2008

I think it’s been broken for a while. :-)

The query string is only ignored if the url being checked doesn’t have a query string of its own, so it fails it two circumstances – once when passing in a hash where the current request has multiple params and also when passing in explicit urls with multiple params.

I’ve created a ticket with a patch at:

http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/1385-fix-current_page-to-work-with-multiple-parameters-in-the-query-string