Skip to content

Commit

Permalink
View: link_to_if + link_to_unless helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
ysbaddaden committed Feb 1, 2016
1 parent 2197db1 commit 07fba31
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/view/helpers/url_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,42 @@ module Frost
link_to(capture { yield }, url, attributes)
end

# Formats an anchor HTML tag if +test+ if true, otherwise prints the
# anchor title only.
def link_to_if(test, title, url, attributes = nil)
if test
link_to(title, url, attributes)
else
title
end
end

def link_to_if(test, url, attributes = nil)
if test
link_to(url, attributes) { yield }
else
yield
end
end

# Formats an anchor HTML tag unless +test+ if true, otherwise prints the
# anchor title only.
def link_to_unless(test, title, url, attributes = nil)
if test
title
else
link_to(title, url, attributes)
end
end

def link_to_unless(test, url, attributes = nil)
if test
yield
else
link_to(url, attributes) { yield }
end
end

# Formats a single button HTML form.
# ```
# button_to "remove", tag_path("name"), method: "delete", attributes: { "class" => "btn-danger" }
Expand Down
22 changes: 22 additions & 0 deletions test/view/helpers/url_helper_test.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,28 @@ class Frost::View::UrlHelperTest < Minitest::Test
assert_equal %(<a class="edit" href="/user/edit">profile</a>), link_to("profile", "/user/edit", { class: "edit" })
end

def test_link_to_if
assert_equal %(somewhere), link_to_if(false, "somewhere", "/")
assert_equal %(<a href="/">somewhere</a>), link_to_if(true, "somewhere", "/")

assert_equal %(article), link_to_if(false, "/article/1") { "article" }
assert_equal %(<a href="/article/1">article</a>), link_to_if(true, "/article/1") { "article" }

assert_equal %(profile), link_to_if(false, "profile", "/user/edit", { class: "edit" })
assert_equal %(<a class="edit" href="/user/edit">profile</a>), link_to_if(true, "profile", "/user/edit", { class: "edit" })
end

def test_link_to_unless
assert_equal %(somewhere), link_to_unless(true, "somewhere", "/")
assert_equal %(<a href="/">somewhere</a>), link_to_unless(false, "somewhere", "/")

assert_equal %(article), link_to_unless(true, "/article/1") { "article" }
assert_equal %(<a href="/article/1">article</a>), link_to_unless(false, "/article/1") { "article" }

assert_equal %(profile), link_to_unless(true, "profile", "/user/edit", { class: "edit" })
assert_equal %(<a class="edit" href="/user/edit">profile</a>), link_to_unless(false, "profile", "/user/edit", { class: "edit" })
end

def test_button_to
assert_equal %(<form action="/url" class="button_to" method="post">#{utf8_enforcer_tag}<button>btn</button></form>),
button_to("btn", "/url")
Expand Down

0 comments on commit 07fba31

Please sign in to comment.