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" }, foundation5: { container_tag: :ul, fragment_tag: :li, class: "breadcrumb", current_class: "current" } }
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.
25 26 27 28 29 |
# File 'lib/gretel/renderer.rb', line 25 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
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, ) styles[style_key] = end |
.styles ⇒ Object
Hash of registered styles.
203 204 205 |
# File 'lib/gretel/renderer.rb', line 203 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.
72 73 74 |
# File 'lib/gretel/renderer.rb', line 72 def (name, url, = {}) context.link_to(name, url, ) end |
#parent_breadcrumb(options = {}) ⇒ Object
Returns the parent breadcrumb.
59 60 61 62 |
# File 'lib/gretel/renderer.rb', line 59 def ( = {}) = () links_for_render()[-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() = () links = links_for_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([: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.
53 54 55 56 |
# File 'lib/gretel/renderer.rb', line 53 def yield_links( = {}) = () yield links_for_render() 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 ( = {}) if parent = () yield parent end end |