Sha256: 12a91eb6829aa3283531f4bb352a3b27baaaad747ca3bc76a2c0690b6e67accb

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

module Breadcrumble
  module ActionController
    extend ActiveSupport::Concern

    included do
      helper_method :add_breadcrumb, :breadcrumbs, :breadcrumb_trails
    end

    module ClassMethods
      def add_breadcrumb name, url = nil
        before_filter do |controller|
          controller.send :add_breadcrumb, name, url
        end
      end

      def add_breadcrumb_to name, url, trail_index
        before_filter do |controller|
          controller.send :add_breadcrumb_to, name, url, trail_index
        end
      end

      def add_breadcrumbs *args
        before_filter do |controller|
          args.each do |arg|
            controller.send :add_breadcrumb, arg[0], arg[1]
          end
        end
      end
    end

    protected

    def add_breadcrumb name, url = nil
      add_breadcrumb_to name, url, 0
    end

    def add_breadcrumb_to name, url, trail_index
      @breadcrumb_trails ||= [[]]
      @breadcrumb_trails[trail_index] ||= []
      @breadcrumb_trails[trail_index] << {
        name: case name
              when Proc then name.call(self)
              else name
              end,
        url:  case url
              when Proc then url.call(self)
              else url ? url_for(url) : nil
              end
      }
    end

    def add_breadcrumbs *args
      args.each do |arg|
        add_breadcrumb arg[0], arg[1]
      end
    end

    def breadcrumb_trails
      @breadcrumb_trails
    end

    def breadcrumbs
      @breadcrumb_trails.first
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
breadcrumble-3.0.3 lib/breadcrumble/action_controller.rb