Flexible Ruby on Rails breadcrumbs plugin.
Switch branches/tags
Clone or download
Pull request Compare This branch is 300 commits behind lassebunk:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
test
MIT-LICENSE
README.rdoc
Rakefile
gretel.gemspec
init.rb

README.rdoc

Gretel is a Ruby on Rails plugin that makes it easy yet flexible to create breadcrumbs.

Installation

In your Gemfile:

gem 'gretel'

And run bundle install.

Example

Start by generating initializer:

$ rails generate gretel breadcrumbs

In config/initializers/breadcrumbs.rb:

Gretel::Crumbs.layout do

  # root crumb
  crumb :root do
    link "Home", root_path
  end

  # custom styles
  crumb :projects do
    link "Projects", projects_path, :class => "breadcrumb", :style => "font-weight: bold;"
  end

  # lambdas
  crumb :project do |project|
    link lambda { |project| "#{project.name} (#{project.id.to_s})" }, project_path(project)
    parent :projects
  end

  # parent crumbs
  crumb :project_issues do |project|
    link "Issues", project_issues_path(project)
    parent :project, project
  end

  # child 
  crumb :issue do |issue|
    link issue.name, issue_path(issue)
    parent :project_issues, issue.project
  end

  # multiple links per crumb (recursive links for parent categories)
  crumb :category do |category|
    parents = [category]

    parent_category = category
    while parent_category = parent_category.parent_category
      parents.unshift parent_category
    end

    parents.each do |category|
      link category.name, category
    end

    parent :categories
  end

  # product crumb with recursive parent categories
  crumb :product do |product|
    link product.name, product
    parent :category, product.category
  end
end

In app/views/xx/xx.html.erb:

<% breadcrumb :issue, @issue %>

In app/views/layouts/application.html.erb:

<%= breadcrumb :pretext => "You are here: ",
               :posttext => " &laquo; that was the breadcrumbs!",
               :separator => " &rsaquo; ",
               :autoroot => true,
               :show_root_alone => true,
               :link_current => false,
               :semantic => true
               %>

Or, if you want to customize your breadcrumbs:

<% breadcrumbs(:autoroot => true, :show_root_alone => false).each_with_index do |crumb, index| %>
  <% if index > 0 %> &gt;<% end %>
  <% if crumb.current? %>
    <span class="current"><%= crumb.text %></span>
  <% else %>
    <%= link_to crumb.text, crumb.url %>
  <% end %>
<% end %>

This could also be done in the controller, if you prefer:

def show
  @project = Project.find(params[:id])
  breadcrumb :project, @project
end

Options for <%= breadcrumb %>:

:pretext          Text to be rendered before breadcrumb, if any. Default: none
:separator        Separator between links. Default: &gt;
:autoroot         Whether it should automatically link to :root if no root parent is given. Default: false
:show_root_alone  Whether it should show :root if this is the only link. Default: false
:link_current     Whether the current crumb should be linked to. Default: false

Todo's

  • Do it so that you in your development environment can update breadcrumbs without having to restart the server – can anybody please help me fix this?

  • Add I18n t and l helper methods so you can use them in the breadcrumbs layout definition

Contributors

  • Lasse Bunk (creator)

  • Alan Larkin

  • David Doan

  • Carl Johnson

  • Tom Canham

Copyright © 2010 Lasse Bunk, released under the MIT license