Class: Crumby::Trail

Inherits:
Object
  • Object
show all
Defined in:
lib/crumby/trail.rb

Overview

it represent on breadcrumb trail

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTrail

Returns a new instance of Trail.



8
9
10
# File 'lib/crumby/trail.rb', line 8

def initialize
  @entries = []
end

Instance Attribute Details

#entriesObject (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

Examples:

add :books
add @book
add [:admin, @book]
add "Books", :admin_books
add "Books", [:admin,:books]
add "Book", [:admin, @book]
add "Google", "http://google.de"

Overloads:

  • #render(combined) ⇒ Crumby::Entry

    Parameters:

    • combined (Object)
  • #render(label, route) ⇒ Crumby::Entry

    Parameters:

    • label (String)

      the label passthrough to link_to

    • route (Symbol, Array, String)

      route passthrough to link_to

Returns:

Raises:

  • (ArgumentError)


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
  options = args.extract_options!

  # 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, options)
  @entries << entry
  entry
end

#countFixnum

Returns total entries

Returns:

  • (Fixnum)

    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

Parameters:

  • view (ActionView::Base)
  • options (Hash)

    passthrough to renderer

Options Hash (options):

  • :renderer (Class)

    overwrite default renderer

Returns:

  • (String)

    rendered trail

Raises:

  • (ArgumentError)


79
80
81
82
83
84
85
# File 'lib/crumby/trail.rb', line 79

def render(*args)
  options = args.extract_options!
  renderer_class = options[: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, options).render
end

#title(suffix, options) ⇒ String

build a title of trail e.g. for page title

Examples:

title
title "The Site"
title "The Site", divider: " - "

Parameters:

  • suffix (String)

    last item.

  • options (Hash)

Options Hash (options):

  • :divider (String)

    The divider. default is “ » ”

  • :reverse (Boolean)

    reverse the title building. default is true

  • :skip_first (Boolean)

    remove first entry. it is usefull

Returns:

  • (String)

    build title. e.g New Book » Books » Admin



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)
  options = args.extract_options!
  suffix = args.first

  default_options = {
    divider: " » ",
    reverse: true,
    skip_first: true
  }
  options = default_options.merge options

  title_entries = entries
  title_entries = title_entries[1..-1] if options[: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(options[:divider])
  else
    suffix.to_s
  end
end