Module: BreadcrumbsFor
- Defined in:
- lib/breadcrumbs_for.rb
Instance Method Summary collapse
- #array_to_caption(crumb) ⇒ Object
- #breadcrumbs_for(*crumbs_list) ⇒ Object (also: #crumbs_for)
- #crumb_caption(obj, method = 'name') ⇒ Object
- #crumb_html(item, options, pos = nil) ⇒ Object
- #crumb_link(caption, path, options = {}) ⇒ Object
- #crumb_to_caption(crumb) ⇒ Object
- #crumbs_html(crumbs, options) ⇒ Object
- #extract_crumb_params(options) ⇒ Object
- #root_caption ⇒ Object
- #root_crumb ⇒ Object
- #string_to_caption(crumb) ⇒ Object
- #symbol_caption(crumb) ⇒ Object
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 |
#breadcrumbs_for(*crumbs_list) ⇒ Object Also known as: crumbs_for
2 3 4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/breadcrumbs_for.rb', line 2 def *crumbs_list list, = extract_crumb_params(crumbs_list) crumbs = [] crumbs << crumb_html(root_crumb,,'root') if [: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,,is_last_crumb ? 'active' : nil) end crumbs_html(crumbs,) 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,,pos=nil) if [:type]==:list content_tag(:li, item, :class=>"crumb #{pos}") else item end end |
#crumb_link(caption, path, options = {}) ⇒ Object
50 51 52 53 |
# File 'lib/breadcrumbs_for.rb', line 50 def crumb_link caption, path, ={} [:class] = ['crumb',[:class]].compact.join(' ') link_to(caption, path, ) 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,) if [:type]==:list sep = "<li class=\"sep\">#{options[:sep]}</li>" raw ['<ul class="breadcrumbs">', crumbs.join(sep), '</ul>'].join else sep = content_tag(:span, [: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 defaults = { :root => true, :type => :list, :sep => '/', } last_option = .last if last_option.is_a?(Hash) && last_option[:crumbs_options] opts = .pop [, defaults.merge(opts[:crumbs_options])] else [, defaults] end end |
#root_caption ⇒ Object
59 60 61 |
# File 'lib/breadcrumbs_for.rb', line 59 def root_caption t("breadcrumbs.root", :default => 'Home' ) end |
#root_crumb ⇒ Object
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 |