Sha256: 5e8ce07de76cbe29c92705ed05feaf52ac3bd722e570bd9613fdec647a6a00a1

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

module Crumbs
  module ActionController
    module Base
      extend ActiveSupport::Concern

      included do
        append_before_action :set_crumbs
      end

      protected

      def set_crumbs
        paths = [request.path]
        paths.unshift File.dirname(paths.first) until paths.first == '/'
        referer = { base_url: request.base_url, path: request.path, fullpath: request.fullpath }
        if session[:referers].nil? or session[:referers].last[:base_url] != request.base_url
          session[:referers] = [referer]
        elsif request.path.starts_with? "#{session[:referers].last[:path]}/".squeeze('/')
          session[:referers] << referer
        elsif index = find_referer_index(paths)
          session[:referers] = session[:referers][0...index] + [referer]
        elsif
          session[:referers] = [referer]
        end
        paths.pop unless Crumbs.config.show_last
        @crumbs = []
        paths.each do |path|
          params = Rails.application.routes.recognize_path("#{request.base_url}#{path}") rescue next
          if name = Crumbs.find(params[:controller], params[:action], params)
            if index = find_referer_index(path)
              path = session[:referers][index][:fullpath]
            end
            @crumbs << { name: name, path: path }
          end
        end
      end

      def find_referer_index(paths)
        paths = [paths] unless paths.is_a? Array
        session[:referers].rindex do |referer|
          paths.include? referer[:path]
        end
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
crumbs-2.1.2 lib/crumbs/action_controller/base.rb
crumbs-2.1.1 lib/crumbs/action_controller/base.rb