Class: Breadcrumb::Helper
- Inherits:
-
Object
- Object
- Breadcrumb::Helper
- Defined in:
- lib/breadcrumb/helper.rb
Instance Method Summary collapse
-
#add(label, url = nil) ⇒ Object
add breadcrumb.
-
#parts ⇒ Object
get parts.
-
#prepend(label, url = nil) ⇒ Object
prepend elements to the breadcrumb.
-
#raw(content) ⇒ Object
raws the content.
- #separator ⇒ Object
-
#separator=(separator) ⇒ Object
set title separator.
- #title_separator ⇒ Object
-
#title_separator=(separator) ⇒ Object
set title separator.
-
#titleize(&block) ⇒ Object
Given add ‘Home’,‘/’ add ‘News’,‘/news’ add ‘The Last News’,‘news/the-last-news’.
- #to_s ⇒ Object
Instance Method Details
#add(label, url = nil) ⇒ Object
add breadcrumb
11 12 13 14 |
# File 'lib/breadcrumb/helper.rb', line 11 def add(label, url = nil) parts << [label, url] self end |
#parts ⇒ Object
get parts
6 7 8 |
# File 'lib/breadcrumb/helper.rb', line 6 def parts @parts ||= [] end |
#prepend(label, url = nil) ⇒ Object
prepend elements to the breadcrumb
17 18 19 20 |
# File 'lib/breadcrumb/helper.rb', line 17 def prepend(label, url = nil) parts.prepend([label, url]) self end |
#raw(content) ⇒ Object
raws the content
93 94 95 |
# File 'lib/breadcrumb/helper.rb', line 93 def raw(content) content.html_safe end |
#separator ⇒ Object
28 29 30 |
# File 'lib/breadcrumb/helper.rb', line 28 def separator @separator ||= '<span class="separator"> » </span>' end |
#separator=(separator) ⇒ Object
set title separator
23 24 25 26 |
# File 'lib/breadcrumb/helper.rb', line 23 def separator=(separator) @separator = separator self end |
#title_separator ⇒ Object
88 89 90 |
# File 'lib/breadcrumb/helper.rb', line 88 def title_separator @title_separator ||= ' :: ' end |
#title_separator=(separator) ⇒ Object
set title separator
83 84 85 86 |
# File 'lib/breadcrumb/helper.rb', line 83 def title_separator=(separator) @title_separator = separator self end |
#titleize(&block) ⇒ Object
Given
add 'Home','/'
add 'News','/news'
add 'The Last News','news/the-last-news'
titleize 'Home :: News :: The Last News'
with block given when block returns not nil, not false, uses return as title part
titleize {|part| Example.com if part == 'Home'} 'Example.com :: News :: The Last News'
when returns nil, uses default title part when returns false, skips part
titleize {|part, order| false if order == 0 } 'News :: The Last News'
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/breadcrumb/helper.rb', line 60 def titleize(&block) title_parts = [] order = 0 parts.each do |part| if block_given? response = yield(part.first,order) if response != false if response == nil title_parts << part.first else title_parts << response end end else title_parts << part.first end order += 1 end title_parts.join(title_separator) end |
#to_s ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/breadcrumb/helper.rb', line 32 def to_s html = [] parts.each do |part| if part == parts.last html << "<span>#{part[0]}</span>" else html << "<a href=\"#{part[1]}\">#{part[0]}</a>" end end html.length > 1 ? raw("<div class=\"breadcrumb\">#{html.join(separator)}</div>") : "" end |