1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

modified current_page? to ignore extra parameters unless specified in options

Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#805 state:committed]
This commit is contained in:
Erik Andrejko 2008-10-26 11:46:17 -05:00 committed by Michael Koziarski
parent 2c7abe1b56
commit ef9b6b5cba
2 changed files with 25 additions and 4 deletions

View file

@ -499,7 +499,7 @@ module ActionView
# True if the current request URI was generated by the given +options+. # True if the current request URI was generated by the given +options+.
# #
# ==== Examples # ==== Examples
# Let's say we're in the <tt>/shop/checkout</tt> action. # Let's say we're in the <tt>/shop/checkout?order=desc</tt> action.
# #
# current_page?(:action => 'process') # current_page?(:action => 'process')
# # => false # # => false
@ -507,6 +507,9 @@ module ActionView
# current_page?(:controller => 'shop', :action => 'checkout') # current_page?(:controller => 'shop', :action => 'checkout')
# # => true # # => true
# #
# current_page?(:controller => 'shop', :action => 'checkout', :order => 'asc)
# # => false
#
# current_page?(:action => 'checkout') # current_page?(:action => 'checkout')
# # => true # # => true
# #
@ -515,10 +518,18 @@ module ActionView
def current_page?(options) def current_page?(options)
url_string = CGI.escapeHTML(url_for(options)) url_string = CGI.escapeHTML(url_for(options))
request = @controller.request request = @controller.request
if url_string =~ /^\w+:\/\// # We ignore any extra parameters in the request_uri if the
url_string == "#{request.protocol}#{request.host_with_port}#{request.request_uri}" # submitted url doesn't have any either. This lets the function
# work with things like ?order=asc
if url_string.index("?")
request_uri = request.request_uri
else else
url_string == request.request_uri request_uri = request.request_uri.split('?').first
end
if url_string =~ /^\w+:\/\//
url_string == "#{request.protocol}#{request.host_with_port}#{request_uri}"
else
url_string == request_uri
end end
end end

View file

@ -258,6 +258,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", { :action => "show", :controller => "weblog" })
assert_equal "Showing", link_to_unless_current("Showing", "http://www.example.com/weblog/show") 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 "<a href=\"http://www.example.com/weblog/show?order=asc\">Showing</a>", link_to_unless_current("Showing", { :action => "show", :controller => "weblog" })
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")
@controller.request = RequestMock.new("http://www.example.com/weblog/show") @controller.request = RequestMock.new("http://www.example.com/weblog/show")
@controller.url = "http://www.example.com/weblog/list" @controller.url = "http://www.example.com/weblog/list"
assert_equal "<a href=\"http://www.example.com/weblog/list\">Listing</a>", assert_equal "<a href=\"http://www.example.com/weblog/list\">Listing</a>",