class Crumby::Trail

it represent on breadcrumb trail

Attributes

entries[R]

Public Class Methods

new() click to toggle source
# File lib/crumby/trail.rb, line 8
def initialize
  @entries = []
end

Public Instance Methods

add(*args) click to toggle source

add a new entry @example

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

@overload render(combined)

@param [Object] combined

@overload render(label, route)

@param [String] label the label passthrough to link_to
@param [Symbol, Array, String] route route passthrough to link_to

@return [Crumby::Entry] builded entry

# 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
count() click to toggle source

Returns total entries @return [Fixnum] total entries

# File lib/crumby/trail.rb, line 14
def count
  entries.count
end
render(*args) click to toggle source

render the trail by a renderer @overload render(view, options)

@param [ActionView::Base] view
@param [Hash] options passthrough to renderer
@option options [Class] :renderer overwrite default renderer

@return [String] rendered trail

# 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(*args) click to toggle source

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

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

@overload title(suffix, options)

@param [String] suffix last item.
@param [Hash] options
@option options [String] :divider The divider. default is " » "
@option options [Boolean] :reverse reverse the title building. default is true
@option options [Boolean] :skip_first remove first entry. it is usefull

@return [String] build title. e.g New Book » Books » Admin

# 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