Just a quick post, I’ve been tweaking my breadcrumbs mixin, and I think I’ve got it as good as it’s gonna get (hah!)..
I’ll just give you guys the code:
module BreadcrumbHelper
def self.included receiver
receiver.extend ClassMethods
end
module ClassMethods
def breadcrumb name, options = {}
before_filter options do |controller|
controller.send(:breadcrumb, name, options)
end
end
end
def breadcrumb name, options = {}
@breadcrumbs ||= []
@breadcrumbs << { :name => name, :options => options }
end
def breadcrumbs delimeter = " / "
links = []
@breadcrumbs.each do |breadcrumb|
name = format_name(breadcrumb)
unless breadcrumb == @breadcrumbs.last
if breadcrumb[:options][:url]
url = breadcrumb[:options][:url]
else
url = "#{name.downcase}_path" # for the lazy
end
url = eval(url) if url =~ /^@|([a-z0-9]+(_path|_url)(\([^ ]+\))*)$/i
end
links << @template.link_to_if(url, @template.html_escape(name), url)
end
links.join("#{delimeter}")
end
def format_name breadcrumb
if breadcrumb[:options][:eval] and breadcrumb[:options][:eval] == true
eval("\"#{breadcrumb[:name]}\"")
elsif breadcrumb[:name].class == Symbol
breadcrumb[:name].to_s.capitalize
else
breadcrumb[:name]
end
end
def breadcrumbs_title delimiter = " / ", max_length = 2
titles = []
@breadcrumbs[@breadcrumbs.length-max_length..-1].each do |breadcrumb|
titles << @template.html_escape(format_name(breadcrumb))
end
titles.join(delimiter)
end
end
Usage is similar to before, but without the need for :link => [boolean].
eval(not-evil)-d urls work better than before, and accept params now!
You can also eval the title, by using :eval => true when you define the breadcrumb.
breadcrumb 'Order: #{@purchase.id_token}', :eval => true, :except => [:new, :create]
The titles helper now accepts two params: delimiter & max_crumbs. This means you can pick and delimit breadcrumbs from the end of the trail to form your title.
Enjoy :)