Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tmaier
Created January 2, 2012 11:08
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save tmaier/1550309 to your computer and use it in GitHub Desktop.
Save tmaier/1550309 to your computer and use it in GitHub Desktop.
BootstrapTopbarList for simple-navigation and Twitter Bootstrap integration
# Renders an ItemContainer as a <ul> element and its containing items as <li> elements.
# Prepared to use inside the topbar of Twitter Bootstrap http://twitter.github.com/bootstrap/#navigation
#
# Register the renderer and use following code in your view:
# render_navigation(level: 1..2, renderer: :bootstrap_topbar_list, expand_all: true)
class BootstrapTopbarList < SimpleNavigation::Renderer::Base
def render(item_container)
if options[:is_subnavigation]
ul_class = "dropdown-menu"
else
ul_class = "nav"
end
list_content = item_container.items.inject([]) do |list, item|
li_options = item.html_options.reject {|k, v| k == :link}
if include_sub_navigation?(item)
li_options[:class] = [li_options[:class], "dropdown"].flatten.compact.join(' ')
end
li_content = tag_for(item)
if include_sub_navigation?(item)
li_content << render_sub_navigation_for(item)
end
list << content_tag(:li, li_content, li_options)
end.join
if skip_if_empty? && item_container.empty?
''
else
content_tag(:ul, list_content, { :id => item_container.dom_id, :class => [item_container.dom_class, ul_class].flatten.compact.join(' ') })
end
end
def render_sub_navigation_for(item)
item.sub_navigation.render(self.options.merge(:is_subnavigation => true))
end
protected
def tag_for(item)
if item.url.nil?
content_tag('span', item.name, link_options_for(item).except(:method))
else
link_to(item.name, item.url, link_options_for(item))
end
end
# Extracts the options relevant for the generated link
#
def link_options_for(item)
special_options = {:method => item.method}.reject {|k, v| v.nil? }
link_options = item.html_options[:link] || {}
opts = special_options.merge(link_options)
opts[:class] = [link_options[:class], item.selected_class, dropdown_link_class(item)].flatten.compact.join(' ')
opts.delete(:class) if opts[:class].nil? || opts[:class] == ''
opts
end
def dropdown_link_class(item)
if include_sub_navigation?(item) && !options[:is_subnavigation]
"dropdown-toggle"
end
end
end
@pusewicz
Copy link

Hey, could I include this helper in https://github.com/pusewicz/twitter-bootstrap-markup-rails?

@tmaier
Copy link
Author

tmaier commented Jan 28, 2012

Cześć Piotr, of course you can add it to your gem. I'm glad if someone finds it useful and this is all I want

@pusewicz
Copy link

pusewicz commented Feb 2, 2012

Thanks, added!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment