Class: Gretel::Renderer
- Inherits:
-
Object
- Object
- Gretel::Renderer
- 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" } }
Class Method Summary collapse
-
.register_style(style_key, options) ⇒ Object
Registers a style for later use.
-
.styles ⇒ Object
Hash of registered styles.
Instance Method Summary collapse
-
#breadcrumb_link_to(name, url, options = {}) ⇒ Object
Proxy for
context.link_tothat can be overridden by plugins. -
#initialize(context, breadcrumb_key, *breadcrumb_args) ⇒ Renderer
constructor
A new instance of Renderer.
-
#parent_breadcrumb(options = {}) ⇒ Object
Returns the parent breadcrumb.
-
#render(options) ⇒ Object
Renders the breadcrumbs HTML.
-
#yield_links(options = {}) {|links_for_render(options)| ... } ⇒ Object
Yields links with applied options.
-
#yield_parent_breadcrumb(options = {}) ⇒ Object
Yields the parent breadcrumb if any.
Methods included from Resettable
Constructor Details
#initialize(context, breadcrumb_key, *breadcrumb_args) ⇒ Renderer
Returns a new instance of Renderer.
24 25 26 27 28 |
# File 'lib/gretel/renderer.rb', line 24 def initialize(context, , *) @context = context = = 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
190 191 192 |
# File 'lib/gretel/renderer.rb', line 190 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 }
200 201 202 |
# File 'lib/gretel/renderer.rb', line 200 def register_style(style_key, ) styles[style_key] = end |
.styles ⇒ Object
Hash of registered styles.
205 206 207 |
# File 'lib/gretel/renderer.rb', line 205 def styles @styles ||= DEFAULT_STYLES end |
Instance Method Details
#breadcrumb_link_to(name, url, options = {}) ⇒ Object
Proxy for context.link_to that can be overridden by plugins.
74 75 76 |
# File 'lib/gretel/renderer.rb', line 74 def (name, url, = {}) context.link_to(name, url, ) end |
#parent_breadcrumb(options = {}) ⇒ Object
Returns the parent breadcrumb.
61 62 63 64 |
# File 'lib/gretel/renderer.rb', line 61 def ( = {}) = () links_for_render()[-2] end |
#render(options) ⇒ Object
Renders the breadcrumbs HTML.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/gretel/renderer.rb', line 31 def render() = () links = links_for_render() return "" if links.empty? # Array to hold the HTML fragments fragments = [] # Loop through all but the last (current) link and build HTML of the fragments links[0..-2].each do |link| fragments << render_fragment([:fragment_tag], link.text, link.url, [: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([:fragment_tag], current_link.text, ([:link_current] ? current_link.url : nil), [:semantic], class: [:current_class]) # Build the final HTML html = ([:pretext] + fragments.join([:separator]) + [:posttext]).html_safe content_tag([:container_tag], html, id: [:id], class: [:class]) end |
#yield_links(options = {}) {|links_for_render(options)| ... } ⇒ Object
Yields links with applied options.
55 56 57 58 |
# File 'lib/gretel/renderer.rb', line 55 def yield_links( = {}) = () yield links_for_render() end |
#yield_parent_breadcrumb(options = {}) ⇒ Object
Yields the parent breadcrumb if any.
67 68 69 70 71 |
# File 'lib/gretel/renderer.rb', line 67 def ( = {}) if parent = () yield parent end end |