Skip to content

Instantly share code, notes, and snippets.

@samrayner
Last active December 16, 2015 14:32
Show Gist options
  • Save samrayner/28f0c8c3f3b84d8a9837 to your computer and use it in GitHub Desktop.
Save samrayner/28f0c8c3f3b84d8a9837 to your computer and use it in GitHub Desktop.
def bootstrap_nav_dropdown(text, url="#", active=nil, &block)
toggle_content = "#{text} #{content_tag(:b, '', class: 'caret')}"
toggle = link_to toggle_content.html_safe, url, class: "dropdown-toggle", "data-toggle" => "dropdown"
list = content_tag(:ul, class: "dropdown-menu", &block)
active = list.include?('li class="active"') if active.nil?
content_tag(:li, toggle + list, class: "dropdown#{' active' if active}")
end
def bootstrap_btn_dropdown(text, style="default", &block)
toggle_content = "#{text} #{content_tag(:span, '', class: 'caret')}"
toggle = content_tag(:button, toggle_content.html_safe, class: "dropdown-toggle btn btn-#{style}", "data-toggle" => "dropdown")
list = content_tag(:ul, class: "dropdown-menu", role: "menu", &block)
content_tag(:div, toggle + list, class: "btn-group")
end
#rspec
describe "#bootstrap_nav_dropdown" do
it "wraps content in correct dropdown markup" do
expect(bootstrap_nav_dropdown("Home", "/"){ '<li>...</li>'.html_safe }).
to eq('<li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="/">'+
'Home <b class="caret"></b></a><ul class="dropdown-menu"><li>...</li></ul></li>')
end
it "activates if there is an active child" do
expect(bootstrap_nav_dropdown("Home", "/"){ '<li class="active">...</li>'.html_safe }).
to eq('<li class="dropdown active"><a class="dropdown-toggle" data-toggle="dropdown" href="/">'+
'Home <b class="caret"></b></a><ul class="dropdown-menu"><li class="active">...</li>'+
'</ul></li>')
end
it "activates according to active argument" do
expect(bootstrap_nav_dropdown("Home", "/", true){ '<li>...</li>'.html_safe }).
to eq('<li class="dropdown active"><a class="dropdown-toggle" data-toggle="dropdown" href="/">'+
'Home <b class="caret"></b></a><ul class="dropdown-menu"><li>...</li></ul></li>')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment