lib/crumbs/action_controller/base.rb in crumbs-1.0.9 vs lib/crumbs/action_controller/base.rb in crumbs-1.1.0
- old
+ new
@@ -8,55 +8,48 @@
end
protected
def define_crumbs
- update_referers
- parts = request.path.split('/')
- parts.pop unless Rails.application.config.crumbs.show_last
+ paths = [request.path]
+ paths.unshift File.dirname(paths.first) until paths.first == '/'
+ if referers.empty? or referers.last[:base_url] != request.base_url
+ self.referers = [build_referer]
+ elsif request.path.starts_with? "#{referers.last[:path]}/".squeeze('/')
+ self.referers << build_referer
+ elsif index = find_referer_index(paths)
+ self.referers = referers[0...index] + [build_referer]
+ elsif
+ self.referers = [build_referer]
+ end
+ paths.pop unless Rails.application.config.crumbs.show_last
@crumbs = []
- while parts.size > 0
- if crumb = find_crumb(parts.size == 1 ? '/' : parts.join('/'))
- @crumbs << crumb
+ paths.each do |path|
+ params = Rails.application.routes.recognize_path("#{request.base_url}#{path}") rescue next
+ if name = Crumbs::Definitions.find(params[:controller], params[:action], params)
+ if index = find_referer_index(path)
+ path = referers[index][:fullpath]
+ end
+ @crumbs << { name: name, path: path }
end
- parts.pop
end
- @crumbs.reverse!
end
- def find_crumb(path)
- params = Rails.application.routes.recognize_path(request.base_url + path) rescue return
- if name = Crumbs::Definitions.find(params[:controller], params[:action], params)
- if index = find_referer_index(path)
- path = referers[index][:fullpath]
- end
- { name: name, path: path }
- end
- end
-
def referers
session[:referers] ||= []
end
def referers=(value)
session[:referers] = value
end
-
- def find_referer_index(path)
- referers.index { |referer| referer[:base_url] == request.base_url and referer[:path] == path }
- end
- def update_referers
- if referers.empty? or index = find_referer_index(request.path) or (referers.last[:path].count('/') == 1 ? '/' : request.path[0...request.path.rindex('/')])
- if index == 0
- self.referers = []
- elsif index
- self.referers = referers[0...index]
- end
- referers << { base_url: request.base_url, path: request.path, fullpath: request.fullpath }
- else
- self.referers = [{ base_url: request.base_url, path: request.path, fullpath: request.fullpath }]
- end
+ def build_referer
+ { base_url: request.base_url, path: request.path, fullpath: request.fullpath }
+ end
+
+ def find_referer_index(paths)
+ paths = [paths] unless paths.is_a? Array
+ referers.rindex { |referer| paths.include? referer[:path] }
end
module ClassMethods
protected