Class: Gretel::Renderer

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

Defined Under Namespace

Classes: Base, LinkCollection, NonSemanticRenderer, SemanticRenderer

Constant Summary collapse

DEFAULT_OPTIONS =
{
  style: :inline,
  pretext: "",
  posttext: "",
  separator: "",
  autoroot: true,
  display_single_fragment: false,
  link_current: false,
  link_current_to_request_path: true,
  semantic: false,
  class: "breadcrumbs",
  current_class: "current",
  pretext_class: "pretext",
  posttext_class: "posttext",
  link_class: nil,
  id: nil,
  aria_current: nil,
  link_data: nil
}
DEFAULT_STYLES =
{
  inline: { 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" },
  bootstrap4: { container_tag: :ol, fragment_tag: :li, class: "breadcrumb", fragment_class: "breadcrumb-item", current_class: "active" },
  bootstrap5: { container_tag: :ol, fragment_tag: :li, class: "breadcrumb", fragment_class: "breadcrumb-item", current_class: "active" },
  foundation5: { container_tag: :ul, fragment_tag: :li, class: "breadcrumbs", 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.



36
37
38
39
40
# File 'lib/gretel/renderer.rb', line 36

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.



138
139
140
# File 'lib/gretel/renderer.rb', line 138

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 }


148
149
150
# File 'lib/gretel/renderer.rb', line 148

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

.stylesObject

Hash of registered styles.



153
154
155
# File 'lib/gretel/renderer.rb', line 153

def styles
  @styles ||= DEFAULT_STYLES
end

Instance Method Details

#parent_breadcrumb(options = {}) ⇒ Object

Returns the parent breadcrumb.



51
52
53
# File 'lib/gretel/renderer.rb', line 51

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

#render(options) ⇒ Object

Renders the breadcrumbs HTML.



43
44
45
46
47
48
# File 'lib/gretel/renderer.rb', line 43

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

  LinkCollection.new(context, links, options)
end

#yield_parent_breadcrumb(options = {}) ⇒ Object

Yields the parent breadcrumb if any.



56
57
58
59
60
# File 'lib/gretel/renderer.rb', line 56

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