Module: Crummy::ViewMethods

Defined in:
lib/crummy.rb

Instance Method Summary collapse

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

#crumbsObject

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 &raquo; for :html and crumb for xml

:seperator => string

Render links in the output. Default true

:link => boolean        

Examples:
render_crumbs                     #=> <a href="/">Home</a> &raquo; <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 &raquo;

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(options = {})
  options[:format] = :html if options[:format] == nil
  if options[:seperator] == nil
    options[:seperator] = " &raquo; " if options[:format] == :html 
    options[:seperator] = "crumb" if options[:format] == :xml 
  end
  options[:links] = true if options[:links] == nil
  case options[:format]
  when :html
    crumb_string = crumbs.collect do |crumb|
      crumb_to_html crumb, options[:links]
    end * options[: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, options[:links]) : crumb_to_html(crumb, options[:links]) + options[:seperator] ) 
      list_items << (:li, list_item_content )
    end
     :ul, list_items, :class => 'breadcrumb'
  when :xml
    crumbs.collect do |crumb|
      crumb_to_xml crumb, options[:links], options[:seperator]
    end * ''
  else
    raise "Unknown breadcrumb output format"
  end
end