Class: Crumby::Trail
- Inherits:
-
Object
- Object
- Crumby::Trail
- Defined in:
- lib/crumby/trail.rb
Overview
it represent on breadcrumb trail
Instance Attribute Summary collapse
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
Instance Method Summary collapse
-
#add(*args) ⇒ Crumby::Entry
add a new entry.
-
#count ⇒ Fixnum
Returns total entries.
-
#initialize ⇒ Trail
constructor
A new instance of Trail.
-
#render(view, options) ⇒ String
render the trail by a renderer.
-
#title(suffix, options) ⇒ String
build a title of trail e.g.
Constructor Details
#initialize ⇒ Trail
Returns a new instance of Trail.
8 9 10 |
# File 'lib/crumby/trail.rb', line 8 def initialize @entries = [] end |
Instance Attribute Details
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
6 7 8 |
# File 'lib/crumby/trail.rb', line 6 def entries @entries end |
Instance Method Details
#render(combined) ⇒ Crumby::Entry #render(label, route) ⇒ Crumby::Entry
add a new entry
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/crumby/trail.rb', line 34 def add(*args) # extract options = args. # call without any arguments raise ArgumentError, "Need arguments." if args.empty? # process arguments if args.count == 1 value = args.first if value.is_a? String label = value elsif value.is_a? Symbol label = value.to_s.humanize route = value elsif value.respond_to? :model_name label = value.model_name.human route = value elsif value.kind_of? Array if value.last.respond_to? :model_name label = value.last.model_name.human else label = value.last.to_s.humanize end route = value else label = value.to_s.humanize end else label = args.first route = args.second end entry = Entry.new(self, count, label, route, ) @entries << entry entry end |
#count ⇒ Fixnum
Returns total entries
14 15 16 |
# File 'lib/crumby/trail.rb', line 14 def count entries.count end |
#render(view, options) ⇒ String
render the trail by a renderer
79 80 81 82 83 84 85 |
# File 'lib/crumby/trail.rb', line 79 def render(*args) = args. renderer_class = [:renderer] || Crumby::Renderer.default_renderer raise ArgumentError if not renderer_class.class == Class or not renderer_class.ancestors.include? Crumby::Renderer::Base view = args.first renderer_class.new(self, view, ).render end |
#title(suffix, options) ⇒ String
build a title of trail e.g. for page title
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/crumby/trail.rb', line 100 def title(*args) = args. suffix = args.first = { divider: " » ", reverse: true, skip_first: true } = .merge title_entries = entries title_entries = title_entries[1..-1] if [:skip_first] if not title_entries.nil? and title_entries.count > 0 title = title_entries.reverse.collect{ |e| e[:label] } title += [suffix] if suffix.present? title.join([:divider]) else suffix.to_s end end |