gretel 1.1.1 → 1.1.2

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/crumb.rb CHANGED
@@ -1,9 +1,9 @@
1
1
  module Gretel
2
2
  class Crumb
3
- attr_accessor :link, :parent
3
+ attr_accessor :links, :parent
4
4
 
5
- def initialize(link, parent)
6
- @link, @parent = link, parent
5
+ def initialize(links, parent)
6
+ @links, @parent = links, parent
7
7
  end
8
8
  end
9
9
  end
data/lib/gretel/crumbs.rb CHANGED
@@ -21,21 +21,21 @@ module Gretel
21
21
  end
22
22
 
23
23
  def get_crumb(name, object = nil)
24
- raise "Crumb '#{name}' not found." unless all[name]
25
-
26
24
  @object = object # share the object so we can call it from link() and parent()
27
- @link = nil
28
25
  @parent = nil
29
26
 
30
27
  all[name].call(object)
31
- Gretel::Crumb.new(@link, @parent)
28
+ Gretel::Crumb.new(@links, @parent)
32
29
  end
33
30
 
34
31
  def link(text, url, options = {})
35
32
  text = text.call(@object) if text.is_a?(Proc)
36
33
  url = url.call(@object) if url.is_a?(Proc)
37
34
 
38
- @link = Gretel::Link.new(text, url, options)
35
+ @links ||= []
36
+ @links << Gretel::Link.new(text, url, options)
37
+
38
+ @links
39
39
  end
40
40
 
41
41
  def parent(name, object = nil)
@@ -33,43 +33,59 @@ module Gretel
33
33
  def breadcrumb_for(*args)
34
34
  options = args.extract_options!
35
35
  link_last = options[:link_last]
36
- options[:link_last] = true
37
- separator = (options[:separator] || "&gt;").html_safe
38
36
 
39
37
  name, object = args[0], args[1]
40
38
 
39
+ links = []
40
+
41
41
  crumb = Crumbs.get_crumb(name, object)
42
- if link_last
43
- out = link_to_if(link_last, crumb.link.text, crumb.link.url, crumb.link.options.merge(:class => "current"))
44
- else
45
- if options[:use_microformats]
46
- out = content_tag(:span, crumb.link.text, :class => "current", :itemprop => "title")
47
- else
48
- out = content_tag(:span, crumb.link.text, :class => "current")
49
- end
42
+ while link = crumb.links.shift
43
+ links.unshift link
50
44
  end
51
45
 
52
- while parent = crumb.parent
53
- last_parent = parent.name
54
- crumb = Crumbs.get_crumb(parent.name, parent.object)
55
- if options[:use_microformats]
56
- out = content_tag(:div, link_to(content_tag(:span, crumb.link.text, :itemprop => "title"), crumb.link.url, crumb.link.options.merge(:itemprop => "url")) + " ", :itemscope => "", :itemtype => "http://data-vocabulary.org/Breadcrumb") + " " + separator + " " + out
57
- else
58
- out = link_to(crumb.link.text, crumb.link.url, crumb.link.options) + " " + separator + " " + out
46
+ while crumb = crumb.parent
47
+ last_parent = crumb.name
48
+ crumb = Crumbs.get_crumb(crumb.name, crumb.object)
49
+ while link = crumb.links.shift
50
+ links.unshift link
59
51
  end
60
52
  end
61
53
 
62
- # TODO: Refactor this
63
54
  if options[:autoroot] && name != :root && last_parent != :root
64
55
  crumb = Crumbs.get_crumb(:root)
56
+ while link = crumb.links.shift
57
+ links.unshift link
58
+ end
59
+ end
60
+
61
+ last_link = links.pop
62
+
63
+ out = []
64
+ while link = links.shift
65
65
  if options[:use_microformats]
66
- out = content_tag(:div, link_to(content_tag(:span, crumb.link.text, :itemprop => "title"), crumb.link.url, crumb.link.options.merge(:itemprop => "url")) + " ", :itemscope => "", :itemtype => "http://data-vocabulary.org/Breadcrumb") + " " + separator + " " + out
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")
67
+ else
68
+ out << link_to(link.text, link.url)
69
+ end
70
+ end
71
+
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
67
79
  else
68
- out = link_to(crumb.link.text, crumb.link.url, crumb.link.options) + " " + separator + " " + out
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")
82
+ else
83
+ out << content_tag(:span, last_link.text, :class => "current", :itemprop => "title")
84
+ end
69
85
  end
70
86
  end
71
87
 
72
- out
88
+ out.join(" " + (options[:separator] || "&gt;") + " ").html_safe
73
89
  end
74
90
  end
75
91
  end
@@ -0,0 +1,90 @@
1
+ module Gretel
2
+ module HelperMethods
3
+ include ActionView::Helpers::UrlHelper
4
+ def controller # hack because ActionView::Helpers::UrlHelper needs a controller method
5
+ end
6
+
7
+ def self.included(base)
8
+ base.send :helper_method, :breadcrumb_for, :breadcrumb
9
+ end
10
+
11
+ def breadcrumb(*args)
12
+ options = args.extract_options!
13
+ name, object = args[0], args[1]
14
+
15
+ if name
16
+ @_breadcrumb_name = name
17
+ @_breadcrumb_object = object
18
+ else
19
+ if @_breadcrumb_name
20
+ crumb = breadcrumb_for(@_breadcrumb_name, @_breadcrumb_object, options)
21
+ elsif options[:show_root_alone]
22
+ crumb = breadcrumb_for(:root, options)
23
+ end
24
+ end
25
+
26
+ if crumb && options[:pretext]
27
+ crumb = options[:pretext].html_safe + " " + crumb
28
+ end
29
+
30
+ crumb
31
+ end
32
+
33
+ def breadcrumb_for(*args)
34
+ options = args.extract_options!
35
+ link_last = options[:link_last]
36
+
37
+ name, object = args[0], args[1]
38
+
39
+ crumbs = []
40
+
41
+ crumb = Crumbs.get_crumb(name, object)
42
+ if crumb.links
43
+ last_link = crumb.links.pop
44
+ #if link_last
45
+ # out = out + " " + separator + " " + link_to_if(link_last, last_link.text, last_crumb.link.url, last_crumb.options.merge(:class => "current"))
46
+ #else
47
+ # if options[:use_microformats]
48
+ # out = out + " " + separator + " " + content_tag(:span, last_link.text, :class => "current", :itemprop => "title")
49
+ # else
50
+ crumbs << content_tag(:span, last_link.text, :class => "current")
51
+ # end
52
+ #end
53
+ end
54
+
55
+ last_parent = nil
56
+ while parent = crumb.parent
57
+ last_parent = parent.name
58
+ crumb = Crumbs.get_crumb(parent.name, parent.object)
59
+
60
+ if crumb.links
61
+ while link = crumb.links.shift
62
+ if link
63
+ #if options[:use_microformats]
64
+ # parents = out + " " + separator + " " + 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")
65
+ #else
66
+ crumbs.shift link_to(link.text, link.url, link.options)
67
+ #end
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+ # TODO: Refactor this
74
+ if options[:autoroot] && name != :root && last_parent != :root
75
+ crumb = Crumbs.get_crumb(:root)
76
+ if crumb.links
77
+ while link = crumb.links.shift
78
+ #if options[:use_microformats]
79
+ # 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") + " " + separator + " " + out
80
+ #else
81
+ crumbs.unshift link_to(link.text, link.url, link.options)
82
+ #end
83
+ end
84
+ end
85
+ end
86
+
87
+ crumbs.join(" " + (options[:separator] || "&gt;") + " ").html_safe
88
+ end
89
+ end
90
+ end
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: 17
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 1
9
- - 1
10
- version: 1.1.1
9
+ - 2
10
+ version: 1.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Lasse Bunk
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-05-21 00:00:00 Z
18
+ date: 2012-05-26 00:00:00 Z
19
19
  dependencies: []
20
20
 
21
21
  description: Gretel is a Ruby on Rails plugin that makes it easy yet flexible to create breadcrumbs.
@@ -32,6 +32,7 @@ files:
32
32
  - lib/gretel/crumb.rb
33
33
  - lib/gretel/crumbs.rb
34
34
  - lib/gretel/helper_methods.rb
35
+ - lib/gretel/helper_methods_old.rb
35
36
  - lib/gretel/link.rb
36
37
  - lib/gretel/parent.rb
37
38
  - lib/gretel.rb