Created
March 4, 2011 23:08
-
-
Save mikew/855882 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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