class DatGretel::Renderer
Constants
- DEFAULT_OPTIONS
- DEFAULT_STYLES
Attributes
context[R]
Public Class Methods
new(context, breadcrumb_key, *breadcrumb_args)
click to toggle source
# File lib/dat_gretel/renderer.rb, line 29 def initialize(context, breadcrumb_key, *breadcrumb_args) @context = context @breadcrumb_key = breadcrumb_key @breadcrumb_args = breadcrumb_args end
Private Class Methods
register_style(style_key, options)
click to toggle source
Registers a style for later use.
Gretel::Renderer.register_style :ul, { container_tag: :ul, fragment_tag: :li }
# File lib/dat_gretel/renderer.rb, line 146 def register_style(style_key, options) styles[style_key] = options end
styles()
click to toggle source
Hash of registered styles.
# File lib/dat_gretel/renderer.rb, line 151 def styles @styles ||= DEFAULT_STYLES end
Public Instance Methods
render(options)
click to toggle source
Renders the breadcrumbs HTML.
# File lib/dat_gretel/renderer.rb, line 36 def render(options) options = options_for_render(options) links = links_for_render(options) LinkCollection.new(context, links, options) end
yield_links(options = {}) { |render(options)| ... }
click to toggle source
Yields links with applied options.
# File lib/dat_gretel/renderer.rb, line 44 def yield_links(options = {}) yield render(options) end
Private Instance Methods
links()
click to toggle source
Array of links for the path of the breadcrumb. Also reloads the breadcrumb configuration if needed.
# File lib/dat_gretel/renderer.rb, line 106 def links @links ||= if @breadcrumb_key.present? # Reload breadcrumbs configuration if needed DatGretel::Crumbs.reload_if_needed # Get breadcrumb set by the `breadcrumb` method crumb = DatGretel::Crumb.new(context, breadcrumb_key, *breadcrumb_args) # Links of first crumb links = crumb.links.dup # Get parent links links.unshift(*parent_links_for(crumb)) links else [] end end
links_for_render(options = {})
click to toggle source
Array of links with applied options.
# File lib/dat_gretel/renderer.rb, line 80 def links_for_render(options = {}) out = links.dup # Handle autoroot if options[:autoroot] && out.map(&:key).exclude?(:root) && DatGretel::Crumbs.crumb_defined?(:root) out.unshift(*DatGretel::Crumb.new(context, :root).links) end # Set current link to actual path if options[:link_current_to_request_path] && out.any? && request out.last.url = request.fullpath end # Handle show root alone if out.size == 1 && !options[:display_single_fragment] out.shift end # Set last link to current out.last.try(:current!) out end
method_missing(method, *args, &block)
click to toggle source
Proxy to view context.
# File lib/dat_gretel/renderer.rb, line 136 def method_missing(method, *args, &block) context.send(method, *args, &block) end
options_for_render(options = {})
click to toggle source
Returns merged options for rendering breadcrumbs.
# File lib/dat_gretel/renderer.rb, line 65 def options_for_render(options = {}) style = options_for_style(options[:style] || DEFAULT_OPTIONS[:style]) DEFAULT_OPTIONS.merge(style).merge(options) end
options_for_style(style_key)
click to toggle source
Returns options for the given style_key
and raises an exception if it's not found.
# File lib/dat_gretel/renderer.rb, line 71 def options_for_style(style_key) if style = self.class.styles[style_key] style else raise ArgumentError, "Breadcrumbs style #{style_key.inspect} not found. Use any of #{self.class.styles.keys.inspect}." end end
parent_links_for(crumb)
click to toggle source
Returns parent links for the crumb.
# File lib/dat_gretel/renderer.rb, line 127 def parent_links_for(crumb) links = [] while crumb = crumb.parent links.unshift(*crumb.links) end links end