Skip to content

Commit

Permalink
allow navbar dropdown to receive list item and link paramters
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelmeurer committed Feb 18, 2015
1 parent fbd3050 commit 8951489
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
11 changes: 8 additions & 3 deletions lib/bootstrap-navbar/helpers/bootstrap3.rb
Expand Up @@ -123,10 +123,15 @@ def navbar_button(text, options = {})
HTML
end

def navbar_dropdown(text, &block)
def navbar_dropdown(text, list_item_options = {}, link_options = {}, &block)
list_item_options, link_options = list_item_options.dup, link_options.dup
list_item_options[:class] = [list_item_options[:class], 'dropdown'].compact.join(' ')
list_item_attributes = attributes_for_tag(list_item_options)
link_options[:class] = [link_options[:class], 'dropdown-toggle'].compact.join(' ')
link_attributes = attributes_for_tag(link_options)
prepare_html <<-HTML.chomp!
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">#{text} <b class="caret"></b></a>
<li#{list_item_attributes}>
<a href="#" data-toggle="dropdown"#{link_attributes}>#{text} <b class="caret"></b></a>
<ul class="dropdown-menu">
#{capture(&block) if block_given?}
</ul>
Expand Down
50 changes: 44 additions & 6 deletions spec/bootstrap-navbar/helpers/bootstrap3_spec.rb
Expand Up @@ -114,13 +114,51 @@
end

describe '#navbar_dropdown' do
it 'generates the correct HTML' do
expect(renderer.navbar_dropdown('foo') { 'bar' }).to have_tag(:li, with: { class: 'dropdown' }) do
with_tag :a, with: { href: '#', class: 'dropdown-toggle', :'data-toggle' => 'dropdown' } do
with_text 'foo '
with_tag :b, with: { class: 'caret' }
context 'without parameters' do
it 'generates the correct HTML' do
expect(renderer.navbar_dropdown('foo') { 'bar' }).to have_tag(:li, with: { class: 'dropdown' }) do
with_tag :a, with: { href: '#', class: 'dropdown-toggle', :'data-toggle' => 'dropdown' } do
with_text 'foo '
with_tag :b, with: { class: 'caret' }
end
with_tag :ul, with: { class: 'dropdown-menu' }, text: /bar/
end
end
end

context 'with list item parameters' do
it 'generates the correct HTML' do
expect(renderer.navbar_dropdown('foo', class: 'list-item-class', id: 'list-item-id') { 'bar' }).to have_tag(:li, with: { class: 'dropdown list-item-class', id: 'list-item-id' }) do
with_tag :a, with: { href: '#', class: 'dropdown-toggle', :'data-toggle' => 'dropdown' } do
with_text 'foo '
with_tag :b, with: { class: 'caret' }
end
with_tag :ul, with: { class: 'dropdown-menu' }, text: /bar/
end
end
end

context 'with link parameters' do
it 'generates the correct HTML' do
expect(renderer.navbar_dropdown('foo', {}, class: 'link-class', id: 'link-id') { 'bar' }).to have_tag(:li, with: { class: 'dropdown' }) do
with_tag :a, with: { href: '#', class: 'dropdown-toggle link-class', id: 'link-id', :'data-toggle' => 'dropdown' } do
with_text 'foo '
with_tag :b, with: { class: 'caret' }
end
with_tag :ul, with: { class: 'dropdown-menu' }, text: /bar/
end
end
end

context 'with list item parameters and link parameters' do
it 'generates the correct HTML' do
expect(renderer.navbar_dropdown('foo', { class: 'list-item-class', id: 'list-item-id' }, class: 'link-class', id: 'link-id') { 'bar' }).to have_tag(:li, with: { class: 'dropdown list-item-class', id: 'list-item-id' }) do
with_tag :a, with: { href: '#', class: 'dropdown-toggle link-class', id: 'link-id', :'data-toggle' => 'dropdown' } do
with_text 'foo '
with_tag :b, with: { class: 'caret' }
end
with_tag :ul, with: { class: 'dropdown-menu' }, text: /bar/
end
with_tag :ul, with: { class: 'dropdown-menu' }, text: /bar/
end
end
end
Expand Down

0 comments on commit 8951489

Please sign in to comment.