class BreadcrumbTrail::HTMLBuilder

Creates a structure of HTML elements to render the breadcrumbs.

Public Instance Methods

call() click to toggle source

Renders the breadcrumbs in HTML tags. If no options were provided on initialization, it uses defaults.

@option @options [String] :outer (“ol”) The outer tag element

to use.

@option @options [String] :inner (“li”) The inner tag element

to use.

@option @options [Hash] :outer_options (nil) The outer tag

element attributes to use.  Things like `class="some-class"`
are best placed here.

@option @options [Hash] :inner_options (nil) The inner tag

element attributes to use.  Things like `class="some-class"`
are best placed here.

@return [String]

# File lib/breadcrumb_trail/builder.rb, line 72
def call
  outer_tag = @options.fetch(:outer, "ol")
  inner_tag = @options.fetch(:inner, "li")
  outer = tag(outer_tag,
              @options.fetch(:outer_options, nil),
              true) if outer_tag
  inner = tag(inner_tag,
              @options.fetch(:inner_options, nil),
              true) if inner_tag

  buffer = ActiveSupport::SafeBuffer.new
  buffer.safe_concat(outer) if outer_tag

  @breadcrumbs.each do |breadcrumb|
    buffer.safe_concat(inner) if inner_tag
    buffer << link_to(breadcrumb.computed_name(@context),
                      breadcrumb.computed_path(@context),
                      breadcrumb.options)
    buffer.safe_concat("</#{inner_tag}>") if inner_tag
  end

  buffer.safe_concat("</#{outer_tag}>") if outer_tag
  buffer
end