crumbs 1.0.8 → 1.0.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 65c23c3a5031cc7aee4ea4f9345d356a920ae45a
4
- data.tar.gz: 1eac4ee8c700f05faaa1686802121c7d4a6099c7
3
+ metadata.gz: 23f731609ef462cb58727f9e67be6eac84f2475e
4
+ data.tar.gz: 954a7f6708787612add650639e15176c389aea0e
5
5
  SHA512:
6
- metadata.gz: 11c1db3e6708873d5b70bdb8c852626e8db9ec0348b5e75d0e9c730fe5654fbd96f2037304451826bf63664f67eec1b1133c438d243f3c9530487e38908e9487
7
- data.tar.gz: 15c226389376609305b7d1a26890df5788a67d4af6fbec7ad9e1a5e97b1e003072b5e3e6cd883594dfd2b0540d43c883b61e3918b42dd0a4b28e333b86a1dba0
6
+ metadata.gz: 89edb12b5f957aed754ff382564ce7f90f754f63179f53021720ce6615820bb33a39df9ce961b583d41cc49e26c438b4195f9b500a62e64a703f3bebd9e44a08
7
+ data.tar.gz: c6b1b02a804178b9e813059873da429532298834a4862667f315d1effd18f43899fa91ad0701bfd1e33621acf3123b33a07caa2f5e031bac531dc31262867595
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2013 Mattways
1
+ Copyright 2013 Museways
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.rdoc CHANGED
@@ -1,4 +1,4 @@
1
- {<img src="https://codeclimate.com/github/mattways/crumbs.png" />}[https://codeclimate.com/github/mattways/crumbs] {<img src="https://travis-ci.org/mattways/crumbs.png?branch=master" alt="Build Status" />}[https://travis-ci.org/mattways/crumbs] {<img src="https://gemnasium.com/mattways/crumbs.png" alt="Dependency Status" />}[https://gemnasium.com/mattways/crumbs]
1
+ {<img src="https://badge.fury.io/rb/crumbs.png" alt="Gem Version" />}[http://badge.fury.io/rb/crumbs] {<img src="https://codeclimate.com/github/museways/crumbs.png" />}[https://codeclimate.com/github/museways/crumbs] {<img src="https://travis-ci.org/museways/crumbs.png?branch=master" alt="Build Status" />}[https://travis-ci.org/museways/crumbs] {<img src="https://gemnasium.com/museways/crumbs.png" alt="Dependency Status" />}[https://gemnasium.com/museways/crumbs]
2
2
 
3
3
  = Crumbs
4
4
 
@@ -7,114 +7,67 @@ module Crumbs
7
7
  before_filter :define_crumbs
8
8
  end
9
9
 
10
- protected
10
+ protected
11
11
 
12
- def define_crumbs
13
- check_referer
12
+ def define_crumbs
13
+ update_referers
14
14
  parts = request.path.split('/')
15
15
  parts.pop unless Rails.application.config.crumbs.show_last
16
16
  @crumbs = []
17
17
  while parts.size > 0
18
- path = join_parts(parts)
19
- if params = find_path_params(path)
20
- if name = History.get_name(params[:controller], params[:action], params)
21
- if index = in_referer?(path)
22
- path = session[:referer][index][:fullpath]
23
- end
24
- @crumbs << { name: name, path: path }
25
- end
26
- end
27
- parts.pop
28
- end
29
- @crumbs.reverse!
30
- end
31
-
32
- def find_path_params(path)
33
- begin
34
- Rails.application.routes.recognize_path(request.base_url + path)
35
- rescue
36
- false
18
+ if crumb = find_crumb(parts.size == 1 ? '/' : parts.join('/'))
19
+ @crumbs << crumb
20
+ end
21
+ parts.pop
37
22
  end
23
+ @crumbs.reverse!
38
24
  end
39
25
 
40
- def check_referer
41
- if session[:referer].nil?
42
- reset_referer
43
- elsif is_last_referer?
44
- last_index = session[:referer].size - 1
45
- session[:referer][last_index][:fullpath] = request.fullpath
46
- elsif add_to_referer?
47
- add_to_referer
48
- elsif index = in_referer_tree?
49
- session[:referer] = session[:referer].slice(Range.new(0, index))
50
- add_to_referer
51
- else
52
- reset_referer
53
- end
54
- end
55
-
56
- def add_to_referer
57
- session[:referer] << { base_url: request.base_url, path: request.path, fullpath: request.fullpath }
58
- end
59
-
60
- def reset_referer
61
- session[:referer] = [{ base_url: request.base_url, path: request.path, fullpath: request.fullpath }]
62
- end
63
-
64
- def join_parts(parts)
65
- path = parts.join('/')
66
- path[0] != '/' ? "/#{path}" : path
67
- end
68
-
69
- def is_last_referer?
70
- last = session[:referer].last
71
- last[:base_url] == request.base_url and last[:path] == request.path
26
+ def find_crumb(path)
27
+ params = Rails.application.routes.recognize_path(request.base_url + path) rescue return
28
+ if name = Crumbs::Definitions.find(params[:controller], params[:action], params)
29
+ if index = find_referer_index(path)
30
+ path = referers[index][:fullpath]
31
+ end
32
+ { name: name, path: path }
33
+ end
72
34
  end
73
-
74
- def add_to_referer?
75
- last = session[:referer].last
76
- parts = request.path.split('/')
77
- parts.size > 0 and last[:path] == join_parts(parts.slice(0, parts.size - 1))
35
+
36
+ def referers
37
+ session[:referers] ||= []
78
38
  end
79
39
 
80
- def in_referer?(path)
81
- find_in_referer path
40
+ def referers=(value)
41
+ session[:referers] = value
82
42
  end
83
-
84
- def in_referer_tree?
85
- parts = request.path.split('/')
86
- parts.pop
87
- while parts.size > 0
88
- index = find_in_referer_tree(parts)
89
- return index unless index.nil?
90
- parts.pop
91
- end
43
+
44
+ def find_referer_index(path)
45
+ referers.index { |referer| referer[:base_url] == request.base_url and referer[:path] == path }
92
46
  end
93
47
 
94
- def find_in_referer(path)
95
- session[:referer].index do |referer|
96
- referer[:base_url] == request.base_url and referer[:path] == path
97
- end
98
- end
99
-
100
- def find_in_referer_tree(parts)
101
- session[:referer].index do |referer|
102
- referer[:base_url] == request.base_url and
103
- referer[:path] == join_parts(parts) and
104
- referer[:path] != request.path
48
+ def update_referers
49
+ if referers.empty? or index = find_referer_index(request.path) or (referers.last[:path].count('/') == 1 ? '/' : request.path[0...request.path.rindex('/')])
50
+ if index == 0
51
+ self.referers = []
52
+ elsif index
53
+ self.referers = referers[0...index]
54
+ end
55
+ referers << { base_url: request.base_url, path: request.path, fullpath: request.fullpath }
56
+ else
57
+ self.referers = [{ base_url: request.base_url, path: request.path, fullpath: request.fullpath }]
105
58
  end
106
59
  end
107
-
60
+
108
61
  module ClassMethods
109
-
62
+
110
63
  protected
111
-
112
- def crumb(action, name = nil, &block)
64
+
65
+ def crumb(action, name=nil, &block)
113
66
  controller = self.name.gsub('::', '/').gsub('Controller', '').underscore
114
67
  name = block_given? ? block : name
115
- History.add(controller, action, name)
68
+ Crumbs::Definitions.add controller.to_s, action.to_s, name
116
69
  end
117
-
70
+
118
71
  end
119
72
  end
120
73
  end
@@ -0,0 +1,26 @@
1
+ module Crumbs
2
+ class Definitions
3
+ class << self
4
+
5
+ def all
6
+ @all ||= {}
7
+ end
8
+
9
+ def add(controller, action, name)
10
+ if all.has_key? controller
11
+ all[controller][action] = name
12
+ else
13
+ all[controller] = { action => name }
14
+ end
15
+ end
16
+
17
+ def find(controller, action, params)
18
+ if all.has_key? controller and all[controller].has_key? action
19
+ name = all[controller][action]
20
+ name.is_a?(Proc) ? name.call(params) : name
21
+ end
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -1,5 +1,5 @@
1
1
  module Crumbs
2
2
 
3
- VERSION = '1.0.8'
3
+ VERSION = '1.0.9'
4
4
 
5
5
  end
data/lib/crumbs.rb CHANGED
@@ -1,6 +1,5 @@
1
- require 'action_controller/metal/exceptions'
2
1
  require 'crumbs/action_controller/base'
3
- require 'crumbs/history'
2
+ require 'crumbs/definitions'
4
3
  require 'crumbs/railtie'
5
4
  require 'crumbs/version'
6
5
 
@@ -4,7 +4,7 @@ class PagesController < ApplicationController
4
4
  crumb(:static) { 'Static' }
5
5
  crumb :i18n do I18n.t('hello') end
6
6
  crumb :nested, ->(params) { 'Nested' }
7
- crumb :param, proc { |params| params[:param] }
7
+ crumb :param, proc { |params| "Param#{params[:param]}" }
8
8
 
9
9
  def home
10
10
  render :crumbs