Permalink
Browse files

Added UrlHelper#link_to_if/link_to_unless to enable other conditions …

…that just link_to_unless_current #757 [mindel]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@852 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 9eb0496 commit fd9967bca2aa32dd4173881c4d8c55f5ebabe97e @dhh dhh committed Mar 6, 2005
View
@@ -1,5 +1,7 @@
*SVN*
+* Added UrlHelper#link_to_if/link_to_unless to enable other conditions that just link_to_unless_current #757 [mindel]
+
* Fixed that single quote was not escaped in a UrlHelper#link_to javascript confirm #549 [Scott Barron]
* Removed the default border on link_image_to (it broke xhtml strict) -- can be specified with :border => 0 #517 [?/caleb]
@@ -86,14 +86,28 @@ def link_image_to(src, options = {}, html_options = {}, *parameters_for_method_r
# request uri is the same as the link's, in which case only the name is returned (or the
# given block is yielded, if one exists). This is useful for creating link bars where you don't want to link
# to the page currently being viewed.
- def link_to_unless_current(name, options = {}, html_options = {}, *parameters_for_method_reference)
- if current_page?(options)
- block_given? ?
- yield(name, options, html_options, *parameters_for_method_reference) :
+ def link_to_unless_current(name, options = {}, html_options = {}, *parameters_for_method_reference, &block)
+ link_to_unless current_page?(options), name, options, html_options, *parameters_for_method_reference, &block
+ end
+
+ # Create a link tag of the given +name+ using an URL created by the set of +options+, unless +condition+
+ # is true, in which case only the name is returned (or the given block is yielded, if one exists).
+ def link_to_unless(condition, name, options = {}, html_options = {}, *parameters_for_method_reference, &block)
+ if condition
+ if block_given?
+ block.arity <= 1 ? yield(name) : yield(name, options, html_options, *parameters_for_method_reference)
+ else
html_escape(name)
+ end
else
link_to(name, options, html_options, *parameters_for_method_reference)
- end
+ end
+ end
+
+ # Create a link tag of the given +name+ using an URL created by the set of +options+, if +condition+
+ # is true, in which case only the name is returned (or the given block is yielded, if one exists).
+ def link_to_if(condition, name, options = {}, html_options = {}, *parameters_for_method_reference, &block)
+ link_to_unless !condition, name, options, html_options, *parameters_for_method_reference, &block
end
# Creates a link tag for starting an email to the specified <tt>email_address</tt>, which is also used as the name of the
@@ -50,6 +50,28 @@ def test_link_image_to
link_image_to("rss.gif", "http://www.example.com", :size => "30x45", :alt => "Feed", :class => "admin")
end
+ def test_link_to_unless
+ assert_equal "Showing", link_to_unless(true, "Showing", :action => "show", :controller => "weblog")
+ assert "<a href=\"http://www.example.com\">Listing</a>", link_to_unless(false, "Listing", :action => "list", :controller => "weblog")
+ assert_equal "Showing", link_to_unless(true, "Showing", :action => "show", :controller => "weblog", :id => 1)
+ assert_equal "<strong>Showing</strong>", link_to_unless(true, "Showing", :action => "show", :controller => "weblog", :id => 1) { |name, options, html_options, *parameters_for_method_reference|
+ "<strong>#{name}</strong>"
+ }
+ assert_equal "<strong>Showing</strong>", link_to_unless(true, "Showing", :action => "show", :controller => "weblog", :id => 1) { |name|
+ "<strong>#{name}</strong>"
+ }
+ assert_equal "test", link_to_unless(true, "Showing", :action => "show", :controller => "weblog", :id => 1) {
+ "test"
+ }
+ end
+
+ def test_link_to_if
+ assert_equal "Showing", link_to_if(false, "Showing", :action => "show", :controller => "weblog")
+ assert "<a href=\"http://www.example.com\">Listing</a>", link_to_if(true, "Listing", :action => "list", :controller => "weblog")
+ assert_equal "Showing", link_to_if(false, "Showing", :action => "show", :controller => "weblog", :id => 1)
+ end
+
+
def test_link_unless_current
@request = RequestMock.new("http://www.example.com")
assert_equal "Showing", link_to_unless_current("Showing", :action => "show", :controller => "weblog")

0 comments on commit fd9967b

Please sign in to comment.