Skip to content

Instantly share code, notes, and snippets.

@mikew
Created March 4, 2011 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikew/855882 to your computer and use it in GitHub Desktop.
Save mikew/855882 to your computer and use it in GitHub Desktop.
Example:
<%= link_to_named_routes :hello, [ :world, :foo, :bar ], :base_class => 'tabs' %>
Will give you:
<ul class="tabs depth-0">
<li class="active">
<a href="/hello">Hello!</a>
</li>
<li>
<a href="/world">World</a>
<ul class="tabs depth-1">
<li>
<a href="/foo">Foo</a></li>
<li>
<a href="/bar">Baz</a>
</li>
</ul>
</li>
</ul>
module ApplicationHelper
def link_to_named_routes(*named_routes, &painter)
options = named_routes.extract_options!
options.reverse_merge! :depth => 0, :base_class => 'navigation'
content_tag('ul', :class => [ options[:base_class], "depth-#{options[:depth]}"]) do
named_routes.inject('') do |buffer, named|
is_current = controller_name == named.to_s
li_classes = 'active' if is_current
actor = named
if named.is_a? Array
actor = named.shift
subsequent = link_to_named_routes(*named, :depth => options[:depth] + 1, :base_class => options[:base_class], &painter)
end
scopes = [ :navigation ]
scopes << :active if is_current
handle = I18n.t actor, :scope => scopes, :default => actor.to_s.titleize
link = block_given? ? capture(actor, handle, is_current, &painter) : link_to(handle, actor)
buffer << content_tag('li', :class => li_classes) do
link << subsequent.to_s
end
end.html_safe
end
end
end
en:
navigation:
bar: Baz
active:
hello: 'Hello!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment