Add link_to option to toolbar title

pull/4063/head
Oliver Günther 9 years ago
parent 5eee07aa27
commit c67f8cd34a
  1. 14
      app/helpers/toolbar_helper.rb
  2. 34
      spec/helpers/toolbar_helper_spec.rb

@ -1,9 +1,11 @@
module ToolbarHelper
def toolbar(title:, subtitle: '', html: {})
include ERB::Util
def toolbar(title:, subtitle: '', link_to: nil, html: {})
classes = ['toolbar-container', html[:class]].compact.join(' ')
content_tag :div, class: classes do
toolbar = content_tag :div, class: 'toolbar' do
dom_title(title) + dom_toolbar {
dom_title(title, link_to) + dom_toolbar {
yield if block_given?
}
end
@ -14,9 +16,13 @@ module ToolbarHelper
protected
def dom_title(title)
def dom_title(title, link_to = nil)
content_tag :div, class: 'title-container' do
content_tag(:h2, title)
if link_to.present?
content_tag(:h2, "#{h(title)}: #{link_to}".html_safe)
else
content_tag(:h2, title)
end
end
end

@ -37,7 +37,7 @@ describe ToolbarHelper, type: :helper do
<div class="toolbar-container">
<div class="toolbar">
<div class="title-container">
<h2 title="Title">Title</h2>
<h2>Title</h2>
</div>
<ul class="toolbar-items"></ul>
</div>
@ -51,7 +51,7 @@ describe ToolbarHelper, type: :helper do
<div class="toolbar-container">
<div class="toolbar">
<div class="title-container">
<h2 title="Title">Title</h2>
<h2>Title</h2>
</div>
<ul class="toolbar-items"></ul>
</div>
@ -60,6 +60,34 @@ describe ToolbarHelper, type: :helper do
}
end
it 'should be able to add a link_to' do
result = toolbar title: 'Title', link_to: link_to('foobar', user_path('1234'))
expect(result).to be_html_eql %{
<div class="toolbar-container">
<div class="toolbar">
<div class="title-container">
<h2>Title: <a href="/users/1234">foobar</a></h2>
</div>
<ul class="toolbar-items"></ul>
</div>
</div>
}
end
it 'should escape the title' do
result = toolbar title: '</h2><script>alert("foobar!");</script>'
expect(result).to be_html_eql %{
<div class="toolbar-container">
<div class="toolbar">
<div class="title-container">
<h2>&lt;/h2&gt;&lt;script&gt;alert(&quot;foobar!&quot;);&lt;/script&gt;</h2>
</div>
<ul class="toolbar-items"></ul>
</div>
</div>
}
end
it 'should include capsulate html' do
result = toolbar title: 'Title' do
content_tag :li do
@ -70,7 +98,7 @@ describe ToolbarHelper, type: :helper do
<div class="toolbar-container">
<div class="toolbar">
<div class="title-container">
<h2 title="Title">Title</h2>
<h2>Title</h2>
</div>
<ul class="toolbar-items">
<li>

Loading…
Cancel
Save