Created
April 15, 2011 03:47
-
-
Save Rio517/921107 to your computer and use it in GitHub Desktop.
This is from the activo-rails gem
This file contains 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 Activo | |
module Rails | |
module Helper | |
# Displays a breadcrumb trail | |
# | |
# options - A hash of attributes to apply to the wrapping div tag | |
# | |
# Example: | |
# <div class="block"> | |
# <div class="content"> | |
# <h2><%= @news_item.title %></h2> | |
# <p><%= @news_item.content %></p> | |
# </div> | |
# <%= breadcrumbs do |b| | |
# b.item "Home", root_path | |
# b.item "News", news_path | |
# b.item "Awesome New Things", news_path(@news_item), :active => true | |
# %> | |
# </div> | |
# | |
# Returns the breadcrumb trail. | |
def breadcrumbs(options = {}) | |
items = NavigationBuilder.new | |
yield items if block_given? | |
options[:class] ||= "".html_safe | |
options[:class] << " breadcrumb".html_safe | |
options[:class].strip! | |
content_tag("div", options) do | |
content_tag("ul") do | |
items.collect { |item| | |
content_tag("li", :class => item[:class]) do | |
if item[:active] | |
item[:label] | |
else | |
link_to(item[:label], item[:href]) | |
end | |
end | |
}.join("").html_safe | |
end | |
end | |
end | |
# Assists in the creation of navigation menus | |
class NavigationBuilder | |
attr_reader :item_list | |
include Enumerable | |
def initialize | |
@item_list = [] | |
end | |
def each(&blk) | |
item_list.each(&blk) | |
end | |
def item(label, path, options = {}) | |
options[:class] ||= "" | |
options[:class] << " first" if item_list.empty? | |
options[:class] << " active" if options[:active] | |
options[:link_options] ||= {} | |
options[:link_options].merge!(:method => options[:method]) if options[:method] | |
item_list << { | |
:label => label, | |
:href => path, | |
:class => options[:class].strip, | |
:link_options => options[:link_options], | |
:icon => options[:icon], | |
:active => !!options[:active] | |
} | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment