Skip to content

Instantly share code, notes, and snippets.

@xwmx
Created April 22, 2009 23:34
Show Gist options
  • Save xwmx/100167 to your computer and use it in GitHub Desktop.
Save xwmx/100167 to your computer and use it in GitHub Desktop.
module BreadcrumbsHelper
def breadcrumbs(*args, &block)
yield(BreadcrumbBuilder.new(self))
end
class BreadcrumbBuilder
include ActionView::Helpers::TagHelper
include ActionView::Helpers::CaptureHelper
def initialize(controller)
@controller = controller
@position = 0
end
def crumb(name, url = nil, options = {})
options = { :separator => "»" }.merge(options)
string = if url
"#{prefix(options[:separator])}#{@controller.link_to name, url}"
else
"#{prefix(options[:separator])}#{name}"
end
@position += 1
string
end
def prefix(separator)
@position > 0 ? " #{separator} " : ""
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment