class Gretel::Renderer::LinkCollection

Attributes

context[R]
options[R]

Public Class Methods

new(context, links, options = {}) click to toggle source
# File lib/gretel/renderer.rb, line 158
def initialize(context, links, options = {})
  @context, @links, @options = context, links, options
  concat links
end

Public Instance Methods

keys() click to toggle source

Helper for returning all link keys to allow for simple testing.

# File lib/gretel/renderer.rb, line 164
def keys
  map(&:key)
end
method_missing(method, *args, &block) click to toggle source

Proxy to view context.

# File lib/gretel/renderer.rb, line 249
def method_missing(method, *args, &block)
  context.send(method, *args, &block)
end
render() click to toggle source

Renders the links into breadcrumbs.

# File lib/gretel/renderer.rb, line 169
def render
  return "" if links.empty?

  # Loop through all but the last (current) link and build HTML of the fragments
  fragments = links[0..-2].map do |link|
    render_fragment(options[:fragment_tag], link.text, link.url, options[:semantic])
  end

  # The current link is handled a little differently, and is only linked if specified in the options
  current_link = links.last
  fragments << render_fragment(options[:fragment_tag], current_link.text, (options[:link_current] ? current_link.url : nil), options[:semantic], class: options[:current_class], current_link: current_link.url)

  # Build the final HTML
  html_fragments = []

  if options[:pretext].present?
    html_fragments << content_tag(:span, options[:pretext], class: options[:pretext_class])
  end

  html_fragments << fragments.join(options[:separator])

  if options[:posttext].present?
    html_fragments << content_tag(:span, options[:posttext], class: options[:posttext_class])
  end

  html = html_fragments.join(" ").html_safe
  content_tag(options[:container_tag], html, id: options[:id], class: options[:class])
end
Also aliased as: to_s
render_fragment(fragment_tag, text, url, semantic, options = {}) click to toggle source

Renders HTML for a breadcrumb fragment, i.e. a breadcrumb link.

# File lib/gretel/renderer.rb, line 201
def render_fragment(fragment_tag, text, url, semantic, options = {})
  if semantic
    render_semantic_fragment(fragment_tag, text, url, options)
  else
    render_nonsemantic_fragment(fragment_tag, text, url, options)
  end
end
render_nonsemantic_fragment(fragment_tag, text, url, options = {}) click to toggle source

Renders regular, non-semantic fragment HTML.

# File lib/gretel/renderer.rb, line 230
def render_nonsemantic_fragment(fragment_tag, text, url, options = {})
  if fragment_tag
    text = breadcrumb_link_to(text, url) if url.present?
    content_tag(fragment_tag, text, class: options[:class])
  elsif url.present?
    breadcrumb_link_to(text, url, class: options[:class])
  elsif options[:class].present?
    content_tag(:span, text, class: options[:class])
  else
    text
  end
end
render_semantic_fragment(fragment_tag, text, url, options = {}) click to toggle source

Renders semantic fragment HTML.

# File lib/gretel/renderer.rb, line 210
def render_semantic_fragment(fragment_tag, text, url, options = {})
  if fragment_tag
    text = content_tag(:span, text, itemprop: "title")

    if url.present?
      text = breadcrumb_link_to(text, url, itemprop: "url")
    elsif options[:current_link].present?
      current_url = "#{root_url}#{options[:current_link].gsub(/^\//, '')}"
      text = text + tag(:meta, itemprop: "url", content: current_url)
    end

    content_tag(fragment_tag, text, class: options[:class], itemscope: "", itemtype: "http://data-vocabulary.org/Breadcrumb")
  elsif url.present?
    content_tag(:span, breadcrumb_link_to(content_tag(:span, text, itemprop: "title"), url, class: options[:class], itemprop: "url"), itemscope: "", itemtype: "http://data-vocabulary.org/Breadcrumb")
  else
    content_tag(:span, content_tag(:span, text, class: options[:class], itemprop: "title"), itemscope: "", itemtype: "http://data-vocabulary.org/Breadcrumb")
  end
end
to_s()
Alias for: render