gretel 1.1.5 → 1.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/gretel/crumbs.rb CHANGED
@@ -31,7 +31,11 @@ module Gretel
31
31
  Gretel::Crumb.new(@links, @parent)
32
32
  end
33
33
 
34
- def link(text, url, options = {})
34
+ def link(*args)
35
+ options = args.extract_options!
36
+ text = args[0]
37
+ url = args[1]
38
+
35
39
  text = text.call(@object) if text.is_a?(Proc)
36
40
  url = url.call(@object) if url.is_a?(Proc)
37
41
 
@@ -5,7 +5,7 @@ module Gretel
5
5
  end
6
6
 
7
7
  def self.included(base)
8
- base.send :helper_method, :breadcrumb_for, :breadcrumb
8
+ base.send :helper_method, :breadcrumbs, :breadcrumb
9
9
  end
10
10
 
11
11
  def breadcrumb(*args)
@@ -23,69 +23,104 @@ module Gretel
23
23
  end
24
24
  end
25
25
 
26
- if crumb && options[:pretext]
27
- crumb = options[:pretext].html_safe + " " + crumb
26
+ if crumb
27
+ if options[:pretext]
28
+ crumb = options[:pretext].html_safe + crumb
29
+ end
30
+ if options[:posttext]
31
+ crumb = crumb + options[:posttext].html_safe
32
+ end
28
33
  end
29
34
 
30
35
  crumb
31
36
  end
32
37
 
33
- def breadcrumb_for(*args)
38
+ def breadcrumbs(*args)
34
39
  options = args.extract_options!
35
- link_last = options[:link_last]
36
-
37
- name, object = args[0], args[1]
38
40
 
39
- links = []
40
-
41
- crumb = Crumbs.get_crumb(name, object)
42
- while link = crumb.links.pop
43
- links.unshift link
44
- end
45
-
46
- while crumb = crumb.parent
47
- last_parent = crumb.name
48
- crumb = Crumbs.get_crumb(crumb.name, crumb.object)
41
+ if @_breadcrumb_name
42
+ links = []
43
+
44
+ crumb = Crumbs.get_crumb(@_breadcrumb_name, @_breadcrumb_object)
49
45
  while link = crumb.links.pop
50
- links.unshift link
46
+ links.unshift ViewLink.new(link.text, link.url, link.options)
51
47
  end
52
- end
53
-
54
- if options[:autoroot] && name != :root && last_parent != :root
55
- crumb = Crumbs.get_crumb(:root)
56
- while link = crumb.links.pop
57
- links.unshift link
48
+
49
+ while crumb = crumb.parent
50
+ last_parent = crumb.name
51
+ crumb = Crumbs.get_crumb(crumb.name, crumb.object)
52
+ while link = crumb.links.pop
53
+ links.unshift ViewLink.new(link.text, link.url, link.options)
54
+ end
55
+ end
56
+
57
+ if options[:autoroot] && @_breadcrumb_name != :root && last_parent != :root
58
+ crumb = Crumbs.get_crumb(:root)
59
+ while link = crumb.links.pop
60
+ links.unshift ViewLink.new(link.text, link.url, link.options)
61
+ end
62
+ end
63
+
64
+ current_link = links.pop
65
+
66
+ out = []
67
+ while link = links.shift
68
+ out << ViewLink.new(link.text, link.url, link.options)
58
69
  end
70
+
71
+ if current_link
72
+ out << ViewLink.new(current_link.text, current_link.url, current_link.options, true)
73
+ end
74
+ else
75
+ out = []
59
76
  end
77
+
78
+ out
79
+ end
80
+
81
+ def breadcrumb_for(*args)
82
+ options = args.extract_options!
83
+ name, object = args[0], args[1]
84
+
85
+ links = breadcrumbs(name, object, options)
60
86
 
61
- last_link = links.pop
87
+ current_link = links.pop
62
88
 
63
89
  out = []
64
90
  while link = links.shift
65
- if options[:use_microformats]
66
- out << content_tag(:div, link_to(content_tag(:span, link.text, :itemprop => "title"), link.url, link.options.merge(:itemprop => "url")), :itemscope => "", :itemtype => "http://data-vocabulary.org/Breadcrumb")
91
+ out << get_crumb(link.text, link.url, options[:semantic], nil, link.options)
92
+ end
93
+
94
+ if current_link
95
+ if options[:link_last] || options[:link_current]
96
+ out << get_crumb(current_link.text, current_link.url, options[:semantic], "current", current_link.options)
67
97
  else
68
- out << link_to(link.text, link.url)
98
+ out << get_crumb(current_link.text, nil, options[:semantic], "current")
69
99
  end
70
100
  end
71
101
 
72
- if last_link
73
- if options[:link_last]
74
- if options[:use_microformats]
75
- out << content_tag(:div, link_to(content_tag(:span, last_link.text, :class => "current", :itemprop => "title"), last_link.url, last_link.options.merge(:itemprop => "url")), :itemscope => "", :itemtype => "http://data-vocabulary.org/Breadcrumb")
76
- else
77
- out << link_to(last_link.text, last_link.url, :class => "current")
78
- end
102
+ out.join(options[:separator] || " &gt; ").html_safe
103
+ end
104
+
105
+ def get_crumb(text, url, semantic, css_class, options = {})
106
+ if url.blank?
107
+ if semantic
108
+ content_tag(:div, content_tag(:span, text, :class => css_class, :itemprop => "title"), :itemscope => "", :itemtype => "http://data-vocabulary.org/Breadcrumb")
79
109
  else
80
- if options[:use_microformats]
81
- out << content_tag(:div, content_tag(:span, last_link.text, :class => "current", :itemprop => "title"), :itemscope => "", :itemtype => "http://data-vocabulary.org/Breadcrumb")
110
+ if css_class
111
+ content_tag(:span, text, :class => css_class)
82
112
  else
83
- out << content_tag(:span, last_link.text, :class => "current")
113
+ text
84
114
  end
85
115
  end
116
+ else
117
+ options.merge! :class => (options[:class] ? options[:class] + " " : "") + css_class if css_class
118
+ if semantic
119
+ content_tag(:div, link_to(content_tag(:span, text, :itemprop => "title"), url, options.merge(:itemprop => "url")), :itemscope => "", :itemtype => "http://data-vocabulary.org/Breadcrumb")
120
+ else
121
+ link_to(text, url, options)
122
+ end
86
123
  end
87
-
88
- out.join(" " + (options[:separator] || "&gt;") + " ").html_safe
89
124
  end
90
125
  end
91
126
  end
@@ -0,0 +1,13 @@
1
+ module Gretel
2
+ class ViewLink
3
+ attr_reader :text, :url, :options, :current
4
+
5
+ def initialize(text, url, options, current = false)
6
+ @text, @url, @options, @current = text, url, options, current
7
+ end
8
+
9
+ def current?
10
+ current
11
+ end
12
+ end
13
+ end
data/lib/gretel.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'gretel/crumb'
2
2
  require 'gretel/crumbs'
3
3
  require 'gretel/helper_methods'
4
+ require 'gretel/view_link'
4
5
  require 'gretel/link'
5
6
  require 'gretel/parent'
6
7
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gretel
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 1
9
- - 5
10
- version: 1.1.5
9
+ - 6
10
+ version: 1.1.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Lasse Bunk
@@ -34,6 +34,7 @@ files:
34
34
  - lib/gretel/helper_methods.rb
35
35
  - lib/gretel/link.rb
36
36
  - lib/gretel/parent.rb
37
+ - lib/gretel/view_link.rb
37
38
  - lib/gretel.rb
38
39
  homepage: http://github.com/lassebunk/gretel
39
40
  licenses: []