Last Updated: February 25, 2016
·
1.276K
· ifightcrime

Rails Breadcrumbs

module Breadcrumbs

  def self.included(base)
    base.extend ClassMethods
  end

  def add_breadcrumb(name, path)
    @breadcrumbs ||= []
    @breadcrumbs << Breadcrumb.new(name, path)
  end

  module ClassMethods
    def add_breadcrumb(name, path = nil, options = {})
      before_filter options do |controller|
        path = controller.send(path) if path.is_a? Symbol
        controller.send(:add_breadcrumb, name, path)
      end
    end
  end

  class Breadcrumb
    attr_reader :name, :path
    def initialize(name, path)
      @name = name
      @path = path
    end
  end

end