Sha256: 28d4d1b1fa50947fcc98fed2f1c92c9558b0f96d8116febca7b2aaad796b3806

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

require "i18n" unless defined?(I18n)
require "breadcrumbs/render"
require "breadcrumbs/action_controller_ext" if defined?(ActionController)

class Breadcrumbs
  attr_accessor :items

  def initialize # :nodoc:
    self.items = []
  end

  # Add a new breadcrumbs.
  #
  #   breadcrumbs.add "Home"
  #   breadcrumbs.add "Home", "/"
  #   breadcrumbs.add "Home", "/", :class => "home"
  #
  # If you provide a symbol as text, it will try to
  # find it as I18n scope.
  #
  def add(text, url = nil, options = {})
    options.reverse_merge!(:i18n => true)
    text = translate(text) if options.delete(:i18n)
    items << [text.to_s, url, options]
  end

  # Render breadcrumbs using the specified format.
  # Use HTML lists by default, but can be plain links.
  #
  #   breadcrumbs.render
  #   breadcrumbs.render(:format => :inline)
  #   breadcrumbs.render(:format => :inline, :separator => "|")
  #   breadcrumbs.render(:format => :list)
  #   breadcrumbs.render(:format => :ordered_list)
  #   breadcrumbs.render(:id => "breadcrumbs")
  #   breadcrumbs.render(:class => "breadcrumbs")
  #
  def render(options = {})
    if options[:format] == :inline
      Breadcrumbs::Render::Inline.new(self, options).render
    else
      Breadcrumbs::Render::List.new(self, options).render
    end
  end

  alias :<< :add

  def translate(scope) # :nodoc:
    text = I18n.t(scope, :scope => :breadcrumbs, :raise => true) rescue nil
    text ||= I18n.t(scope, :default => scope.to_s)
    text
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
breadcrumbs-0.1.4 lib/breadcrumbs.rb