Class: Gretel::Renderer

Inherits:
Object
  • Object
show all
Extended by:
Resettable
Defined in:
lib/gretel/renderer.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  style: :default,
  pretext: "",
  posttext: "",
  separator: "",
  autoroot: true,
  display_single_fragment: false,
  link_current: false,
  semantic: false,
  class: "breadcrumbs",
  current_class: "current",
  id: nil
}
DEFAULT_STYLES =
{
  default: { container_tag: :div, separator: " › " },
  ol: { container_tag: :ol, fragment_tag: :li },
  ul: { container_tag: :ul, fragment_tag: :li },
  bootstrap: { container_tag: :ol, fragment_tag: :li, class: "breadcrumb", current_class: "active" },
  foundation5: { container_tag: :ul, fragment_tag: :li, class: "breadcrumb", current_class: "current" }
}

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Resettable

reset!

Constructor Details

#initialize(context, breadcrumb_key, *breadcrumb_args) ⇒ Renderer

Returns a new instance of Renderer.



25
26
27
28
29
# File 'lib/gretel/renderer.rb', line 25

def initialize(context, breadcrumb_key, *breadcrumb_args)
  @context = context
  @breadcrumb_key = breadcrumb_key
  @breadcrumb_args = breadcrumb_args
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)

Proxy to view context



188
189
190
# File 'lib/gretel/renderer.rb', line 188

def method_missing(method, *args, &block)
  context.send(method, *args, &block)
end

Class Method Details

.register_style(style_key, options) ⇒ Object

Registers a style for later use.

Gretel::Renderer.register_style :ul, { container_tag: :ul, fragment_tag: :li }


198
199
200
# File 'lib/gretel/renderer.rb', line 198

def register_style(style_key, options)
  styles[style_key] = options
end

.stylesObject

Hash of registered styles.



203
204
205
# File 'lib/gretel/renderer.rb', line 203

def styles
  @styles ||= DEFAULT_STYLES
end

Instance Method Details

Proxy for context.link_to that can be overridden by plugins.



72
73
74
# File 'lib/gretel/renderer.rb', line 72

def breadcrumb_link_to(name, url, options = {})
  context.link_to(name, url, options)
end

#parent_breadcrumb(options = {}) ⇒ Object

Returns the parent breadcrumb.



59
60
61
62
# File 'lib/gretel/renderer.rb', line 59

def parent_breadcrumb(options = {})
  options = options_for_render(options)
  links_for_render(options)[-2]
end

#render(options) ⇒ Object

Renders the breadcrumbs HTML.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gretel/renderer.rb', line 32

def render(options)
  options = options_for_render(options)
  links = links_for_render(options)

  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])

  # Build the final HTML
  html = (options[:pretext] + fragments.join(options[:separator]) + options[:posttext]).html_safe
  (options[:container_tag], html, id: options[:id], class: options[:class])
end

Yields links with applied options.

Yields:

  • (links_for_render(options))


53
54
55
56
# File 'lib/gretel/renderer.rb', line 53

def yield_links(options = {})
  options = options_for_render(options)
  yield links_for_render(options)
end

#yield_parent_breadcrumb(options = {}) ⇒ Object

Yields the parent breadcrumb if any.



65
66
67
68
69
# File 'lib/gretel/renderer.rb', line 65

def yield_parent_breadcrumb(options = {})
  if parent = parent_breadcrumb(options)
    yield parent
  end
end