Module: Crummy::ViewMethods
- Defined in:
- lib/crummy.rb
Instance Method Summary collapse
-
#add_crumb(name, url = nil) ⇒ Object
Add a crumb to the
crumbsarray. - #crumb_to_html(crumb, links) ⇒ Object
- #crumb_to_xml(crumb, links, seperator) ⇒ Object
-
#crumbs ⇒ Object
List the crumbs as an array.
-
#render_crumbs(options = {}) ⇒ Object
Render the list of crumbs as either html or xml.
Instance Method Details
#add_crumb(name, url = nil) ⇒ Object
Add a crumb to the crumbs array
64 65 66 |
# File 'lib/crummy.rb', line 64 def add_crumb(name, url=nil) crumbs.push [name, url] end |
#crumb_to_html(crumb, links) ⇒ Object
119 120 121 122 |
# File 'lib/crummy.rb', line 119 def crumb_to_html(crumb, links) name, url = crumb url && links ? link_to(name, url) : name end |
#crumb_to_xml(crumb, links, seperator) ⇒ Object
124 125 126 127 |
# File 'lib/crummy.rb', line 124 def crumb_to_xml(crumb, links, seperator) name, url = crumb url && links ? "<#{seperator} href=\"#{url}\">#{name}</#{seperator}>" : "<#{seperator}>#{name}</#{seperator}>" end |
#crumbs ⇒ Object
List the crumbs as an array
59 60 61 |
# File 'lib/crummy.rb', line 59 def crumbs @_crumbs ||= [] # Give me something to push to end |
#render_crumbs(options = {}) ⇒ Object
Render the list of crumbs as either html or xml
Takes 3 options: The output format. Can either be xml or html. Default :html
:format => (:html|:xml)
The seperator text. It does not assume you want spaces on either side so you must specify. Default » for :html and crumb for xml
:seperator => string
Render links in the output. Default true
:link => boolean
Examples:
render_crumbs #=> <a href="/">Home</a> » <a href="/businesses">Businesses</a>
render_crumbs :seperator => ' | ' #=> <a href="/">Home</a> | <a href="/businesses">Businesses</a>
render_crumbs :format => :xml #=> <crumb href="/">Home</crumb><crumb href="/businesses">Businesses</crumb>
The only argument is for the seperator text. It does not assume you want spaces on either side so you must specify. Defaults to »
render_crumbs(" . ") #=> <a href="/">Home</a> . <a href="/businesses">Businesses</a>
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/crummy.rb', line 87 def render_crumbs( = {}) [:format] = :html if [:format] == nil if [:seperator] == nil [:seperator] = " » " if [:format] == :html [:seperator] = "crumb" if [:format] == :xml end [:links] = true if [:links] == nil case [:format] when :html crumb_string = crumbs.collect do |crumb| crumb_to_html crumb, [:links] end * [:seperator] crumb_string = crumb_string.html_safe if crumb_string.respond_to?(:html_safe) crumb_string when :html_with_list list_items = '' crumbs.each_with_index do |crumb, pos| #append separator unless it is the last item in the list list_item_content = ( pos == crumbs.length-1 ? crumb_to_html(crumb, [:links]) : crumb_to_html(crumb, [:links]) + [:seperator] ) list_items << content_tag(:li, list_item_content ) end content_tag :ul, list_items, :class => 'breadcrumb' when :xml crumbs.collect do |crumb| crumb_to_xml crumb, [:links], [:seperator] end * '' else raise "Unknown breadcrumb output format" end end |