From 06eb12c980dfa110ba0f943188f8bb600199a49c Mon Sep 17 00:00:00 2001 From: Ary Djmal Date: Wed, 11 Jun 2008 18:22:18 -0400 Subject: [PATCH] link_to_tagged_current --- actionpack/lib/action_view/helpers/url_helper.rb | 45 ++++++++++++++++++++++ actionpack/test/template/url_helper_test.rb | 16 ++++++++ 2 files changed, 61 insertions(+), 0 deletions(-) diff --git a/actionpack/lib/action_view/helpers/url_helper.rb b/actionpack/lib/action_view/helpers/url_helper.rb index 4b12adf..afb2191 100644 --- a/actionpack/lib/action_view/helpers/url_helper.rb +++ b/actionpack/lib/action_view/helpers/url_helper.rb @@ -283,6 +283,51 @@ module ActionView method_tag + tag("input", html_options) + request_token_tag + "" end + # Creates a link tag of the given +name+ using a URL created by the set of + # +options+ and adds the class current if the current page is the URL or if + # the current page is in the html_options[:highlights_on] array. + # + # ==== Examples + # Let's say you have a navigation menu... + # + # + # + # If in the "about" action, it will render... + # + # + # + # ...but if in the "index" action, it will render: + # + # + # + def link_to_tagged_current(name, options = {}, html_options = {}, &block) + current = false + highlights = html_options.delete(:highlights_on).to_a + [options] + + highlights.each do |h| + if (h[:controller] == @controller.controller_name and !h[:action]) or + (h[:controller] == @controller.controller_name and h[:action] == @controller.action_name and !h[:id]) or + (!h.kind_of?(Hash) and current_page?(h)) + current = true + break + end + end + + if current + html_options[:class] = "#{html_options[:class].to_s} current".strip + end + + link_to name, options, html_options, &block + end # Creates a link tag of the given +name+ using a URL created by the set of # +options+ unless the current request URI is the same as the links, in diff --git a/actionpack/test/template/url_helper_test.rb b/actionpack/test/template/url_helper_test.rb index d45ea08..c34175f 100644 --- a/actionpack/test/template/url_helper_test.rb +++ b/actionpack/test/template/url_helper_test.rb @@ -233,6 +233,22 @@ class UrlHelperTest < ActionView::TestCase assert_equal "Showing", link_to_if(false, "Showing", :action => "show", :controller => "weblog", :id => 1) end + def test_link_tagged_current + @controller.request = RequestMock.new("http://www.example.com/weblog/show") + @controller.url = "http://www.example.com/weblog/show" + assert_equal "Showing", + link_to_tagged_current("Showing", { :action => "show", :controller => "weblog" }) + assert_equal "Showing", + link_to_tagged_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 "Listing", + link_to_tagged_current("Listing", :action => "list", :controller => "weblog") + assert_equal "Listing", + link_to_tagged_current("Listing", "http://www.example.com/weblog/list") + end + def test_link_unless_current @controller.request = RequestMock.new("http://www.example.com/weblog/show") @controller.url = "http://www.example.com/weblog/show" -- 1.5.4.5