Skip to content

Instantly share code, notes, and snippets.

@anolson
Created November 16, 2010 21:22
Show Gist options
  • Save anolson/702537 to your computer and use it in GitHub Desktop.
Save anolson/702537 to your computer and use it in GitHub Desktop.
Rails navigation helpers
module ApplicationHelper
def navigation(collection)
content_tag :ul do
build_navigation_items(collection)
end
end
def build_navigation_items(collection)
content = ""
collection.each do |item|
content << navigation_item(item)
end
content.html_safe
end
def navigation_item(*args)
options = args.extract_options!
options[:name] = args.first if args.first.is_a?(Symbol)
content_tag :li, link_to(navigation_item_name(options), navigation_item_path(options)), :class => assign_current_class(options)
end
def navigation_item_name(options = {})
options[:name].to_s.capitalize
end
def navigation_item_path(options = {})
if(options[:path])
options[:path]
else
send(options[:name].to_s + '_path')
end
end
def assign_current_class(options = {})
if(options[:match])
current_controller_path?(options[:match]) && 'current' || nil
else
current_page?(options[:path]) && 'current' || nil
end
end
def current_controller_path?(regex)
controller.controller_path =~ regex
end
end
<%= navigation([
{:home => {:path => root_path}},
:about,
{:places => {:path => places_path, :match => /places/}}]) %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment