Skip to content

Instantly share code, notes, and snippets.

@placek
Created March 24, 2015 12:07
Show Gist options
  • Save placek/bed8b27a5d0a58be3292 to your computer and use it in GitHub Desktop.
Save placek/bed8b27a5d0a58be3292 to your computer and use it in GitHub Desktop.
simple breadcrumbs
module Breadcrumbs
module InstanceMethods
# Append a breadcrumb to the end of the trail
def add_breadcrumb(name, url = nil)
@breadcrumbs ||= []
url = send(url) if url.is_a?(Symbol)
@breadcrumbs << [name, url]
end
end
module ClassMethods
# Append a breadcrumb to the end of the trail by deferring evaluation
# until the filter processing.
def add_breadcrumb(name, url = nil, options = {})
before_filter(options) do |controller|
controller.send(:add_breadcrumb, name, url)
end
end
end
module HelperMethods
# Returns HTML markup for the breadcrumbs
def breadcrumbs(*args)
safe_join(@breadcrumbs.map do |name, url|
url.blank? ? name : link_to_unless_current(h(name), url)
end, ' » ')
end
end
end
ActionController::Base.send(:include, Breadcrumbs::InstanceMethods)
ActionController::Base.send(:helper_method, :add_breadcrumb)
ActionController::Base.send(:extend, Breadcrumbs::ClassMethods)
ActionView::Base.send(:include, Breadcrumbs::HelperMethods)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment