Sha256: d8b9424c506454891d759939c4ab97b3bc56c780c79eaea629c91c0bf4b41696

Contents?: true

Size: 1.45 KB

Versions: 2

Compression:

Stored size: 1.45 KB

Contents

module BootstrapSassExtras
  module BreadCrumbs
    def self.included(base)
      base.extend(ClassMethods)
      base.helper_method :breadcrumbs?,
                         :breadcrumb_names,
                         :last_breadcrumb_name
    end

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

    protected

    def add_breadcrumb(name, url = '', options = {})
      @breadcrumbs ||= []
      name = translate_breadcrumb(name, self.class.name) if name.is_a?(Symbol)
      url = send(url.to_s) if url =~ /_path|_url|@/
      @breadcrumbs << { name: name, url: url, options: options }
    end

    def translate_breadcrumb(name, class_name)
      scope = [:breadcrumbs]
      namespace = class_name.underscore.split('/')
      namespace.last.sub!('_controller', '')
      scope << namespace

      I18n.t name, scope: scope
    end

    def clear_breadcrumbs
      @breadcrumbs = nil
    end

    def breadcrumbs?
      Array(@breadcrumbs).any?
    end

    def breadcrumb_names
      Array(@breadcrumbs).map { |breadcrumb| breadcrumb[:name] }
    end

    def last_breadcrumb_name
      return unless crumb = Array(@breadcrumbs).last
      crumb[:name]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bootstrap-sass-extras-0.0.7 lib/bootstrap-sass-extras/breadcrumbs.rb
bootstrap-sass-extras-0.0.6 lib/bootstrap-sass-extras/breadcrumbs.rb