Module: BreadcrumbsFor

Defined in:
lib/breadcrumbs_for.rb

Instance Method Summary collapse

Instance Method Details

#array_to_caption(crumb) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/breadcrumbs_for.rb', line 72

def array_to_caption crumb
  if crumb.size>1
    case crumb.first.class.to_s
      when 'Symbol' # Is a namespace. Skip it
        crumb_to_caption(crumb.last)
      when 'String' # Is an action name. Use it
        string_to_caption(crumb.first) << ' ' << crumb_caption(crumb.last)
      else # Use last item only
        cramb_to_caption(crumb.last)
    end
  else
    cramb_to_caption(crumb[0])
  end
end


2
3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/breadcrumbs_for.rb', line 2

def breadcrumbs_for *crumbs_list
  list, options = extract_crumb_params(crumbs_list)
  crumbs = []
  crumbs << crumb_html(root_crumb,options,'root') if options[:root]
  crumbs_count = list.size
  list.each_with_index do |crumb,index|
    caption = crumb_to_caption(crumb)
    is_last_crumb = ((index+1) == crumbs_count)
    item = crumb.is_a?(String) ? caption : crumb_link(caption, url_for(crumb))
    crumbs << crumb_html(item,options,is_last_crumb ? 'active' : nil)
  end
  crumbs_html(crumbs,options)
end

#crumb_caption(obj, method = 'name') ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/breadcrumbs_for.rb', line 102

def crumb_caption obj, method='name'
  [method,'name','title'].each do |name_method|
    if obj.respond_to?(name_method)
      was_method       = "#{name_method}_was"
      changed_method   = "#{name_method}_changed?"
      tracking_enabled = obj.respond_to?(was_method) && obj.respond_to?(changed_method)
      return (tracking_enabled && obj.send(changed_method)) ? obj.send(was_method) : obj.send(name_method.to_s)
    end
  end
  crumb.class.to_s.humanize
end

#crumb_html(item, options, pos = nil) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/breadcrumbs_for.rb', line 32

def crumb_html(item,options,pos=nil)
  if options[:type]==:list
    (:li, item, :class=>"crumb #{pos}")
  else
    item
  end
end


50
51
52
53
# File 'lib/breadcrumbs_for.rb', line 50

def crumb_link caption, path, options={}
  options[:class] = ['crumb',options[:class]].compact.join(' ')
  link_to(caption, path, options)
end

#crumb_to_caption(crumb) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/breadcrumbs_for.rb', line 87

def crumb_to_caption crumb
  case crumb.class.to_s
    when 'Symbol'
      symbol_caption(crumb)
    when 'String'
      string_to_caption(crumb)
    when 'Array'
      array_to_caption(crumb)
    when 'Hash'
      crumb.delete(:crumb)
    else
      crumb_caption(crumb)
  end
end

#crumbs_html(crumbs, options) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/breadcrumbs_for.rb', line 40

def crumbs_html(crumbs,options)
  if options[:type]==:list
    sep = "<li class=\"sep\">#{options[:sep]}</li>"
    raw ['<ul class="breadcrumbs">', crumbs.join(sep), '</ul>'].join
  else
    sep = (:span, options[:sep], :class=>'sep')
    raw crumbs.join(sep)
  end
end

#extract_crumb_params(options) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/breadcrumbs_for.rb', line 17

def extract_crumb_params options
  defaults = {
    :root => true,
    :type => :list,
    :sep => '/',
  }
  last_option = options.last
  if last_option.is_a?(Hash) && last_option[:crumbs_options]
    opts = options.pop
    [options, defaults.merge(opts[:crumbs_options])]
  else
    [options, defaults]
  end
end

#root_captionObject



59
60
61
# File 'lib/breadcrumbs_for.rb', line 59

def root_caption
  t("breadcrumbs.root", :default => 'Home' )
end

#root_crumbObject



55
56
57
# File 'lib/breadcrumbs_for.rb', line 55

def root_crumb
  crumb_link(root_caption, root_path, :class=>'home_crumb')
end

#string_to_caption(crumb) ⇒ Object



63
64
65
# File 'lib/breadcrumbs_for.rb', line 63

def string_to_caption crumb
  t("breadcrumbs.actions.#{crumb.to_s}", :default => crumb.to_s.capitalize)
end

#symbol_caption(crumb) ⇒ Object



67
68
69
70
# File 'lib/breadcrumbs_for.rb', line 67

def symbol_caption crumb
  return root_caption if crumb == :root
  t("breadcrumbs.names.#{crumb.to_s}", :default => crumb.to_s.gsub('_',' ').capitalize)
end