Skip to content

Commit

Permalink
Merge pull request #52 from hanami/allow-links-to-be-concat
Browse files Browse the repository at this point in the history
Allow links to be concat
  • Loading branch information
jodosha committed Feb 4, 2016
2 parents 23a7df4 + 7f36682 commit a6c9aea
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/hanami/helpers/link_to_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def link_to(content, url = nil, options = {}, &blk)
raise ArgumentError
end

html.a(blk || content, options).to_s
html.a(blk || content, options)
end
end
end
Expand Down
19 changes: 19 additions & 0 deletions test/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -569,3 +569,22 @@ def link_to_with_content_html_content_id_and_class
end

end

class HtmlAndLinkTo
include Hanami::Helpers::HtmlHelper
include Hanami::Helpers::LinkToHelper

def two_links_to_in_div
html.div do
link_to('Comments', '/comments') +
link_to('Posts', '/posts')
end
end

def span_and_link_to_in_div
html.div do
span('hello') +
link_to('Comments', '/comments')
end
end
end
14 changes: 14 additions & 0 deletions test/html_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,18 @@
it 'autoescapes nested blocks' do
@view.evil_nested_block_content.to_s.must_equal %(<div>\n<p>&lt;script&gt;alert(&apos;xss&apos;)&lt;&#x2F;script&gt;</p>\n</div>)
end

describe 'with link_to helper' do
before do
@view = HtmlAndLinkTo.new
end

it 'returns two links in div' do
@view.two_links_to_in_div.to_s.must_equal %(<div>\n<a href=\"/comments\">Comments</a>\n<a href=\"/posts\">Posts</a>\n</div>)
end

it 'returns span and link in div' do
@view.span_and_link_to_in_div.to_s.must_equal %(<div>\n<span>hello</span>\n<a href=\"/comments\">Comments</a>\n</div>)
end
end
end
14 changes: 7 additions & 7 deletions test/link_to_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@
end

it 'returns a link to posts' do
@view.link_to_posts.must_equal %(<a href="/posts/">Posts</a>)
@view.link_to_posts.to_s.must_equal %(<a href="/posts/">Posts</a>)
end

it 'returns a link to a post' do
@view.link_to_post.must_equal %(<a href="/post/1">Post</a>)
@view.link_to_post.to_s.must_equal %(<a href="/post/1">Post</a>)
end

it 'returns a link with a class' do
@view.link_to_with_class.must_equal %(<a class="first" href="/posts/">Post</a>)
@view.link_to_with_class.to_s.must_equal %(<a class="first" href="/posts/">Post</a>)
end

it 'returns a link with id' do
@view.link_to_with_id.must_equal %(<a id="posts__link" href="/posts/">Post</a>)
@view.link_to_with_id.to_s.must_equal %(<a id="posts__link" href="/posts/">Post</a>)
end

it 'returns a link relative link' do
@view.link_to_relative_posts.must_equal %(<a href="posts">Posts</a>)
@view.link_to_relative_posts.to_s.must_equal %(<a href="posts">Posts</a>)
end

it 'returns a link with html content' do
@view.link_to_with_html_content.must_equal %(<a href="/posts/">\n<strong>Post</strong>\n</a>)
@view.link_to_with_html_content.to_s.must_equal %(<a href="/posts/">\n<strong>Post</strong>\n</a>)
end

it 'returns a link with html content, id and class' do
@view.link_to_with_html_content_id_and_class.must_equal %(<a id="posts__link" class="first" href="/posts/">\n<strong>Post</strong>\n</a>)
@view.link_to_with_html_content_id_and_class.to_s.must_equal %(<a id="posts__link" class="first" href="/posts/">\n<strong>Post</strong>\n</a>)
end

it 'raises an exception link with content and html content' do
Expand Down

0 comments on commit a6c9aea

Please sign in to comment.