class RocketNavigation::Renderer::Base
This is the base class for all renderers.
A renderer is responsible for rendering an ItemContainer
and its containing items to HTML.
Attributes
Public Class Methods
# File lib/rocket_navigation/renderer/base.rb, line 16 def initialize(container, options = {}) @container = container @options = options end
Public Instance Methods
render an item as an active link (a)
# File lib/rocket_navigation/renderer/base.rb, line 168 def active_tag_for(item) link_to(item.name, item.url, link_options(item)) end
# File lib/rocket_navigation/renderer/base.rb, line 34 def base_item_html @base_item_html ||= container.item_html.merge(options[:item_html] || {}) end
# File lib/rocket_navigation/renderer/base.rb, line 66 def base_link_html @base_link_html ||= container.link_html.merge(options[:link_html] || {}) end
# File lib/rocket_navigation/renderer/base.rb, line 25 def container_html @container_html ||= container.container_html.merge(options[:container_html] || {}) end
override this method if needed
# File lib/rocket_navigation/renderer/base.rb, line 30 def container_options container_html end
# File lib/rocket_navigation/renderer/base.rb, line 98 def expand_all? !options.key?(:expand_all) || options[:expand_all] == true end
# File lib/rocket_navigation/renderer/base.rb, line 42 def item_classes(item) classes = Array.wrap(base_item_html[:class] || []) if item.selected? classes.push(selected_class(:item)) end if item.active_branch? classes.push(selected_class(:branch)) end classes = classes.reject { |c| c.nil? } + item_extra_classes(item) end
override in renderer if needed
# File lib/rocket_navigation/renderer/base.rb, line 39 def item_extra_classes(item) [] end
# File lib/rocket_navigation/renderer/base.rb, line 53 def item_html(item) ret = base_item_html.except(:class) classes = item_classes(item) ret.merge!({class: classes}) unless classes.blank? ret end
override this method if needed
# File lib/rocket_navigation/renderer/base.rb, line 62 def item_options(item) item_html(item) end
# File lib/rocket_navigation/renderer/base.rb, line 102 def level options[:level] || :all end
# File lib/rocket_navigation/renderer/base.rb, line 74 def link_classes(item) classes = Array.wrap(base_link_html[:class] || []) if item.selected? classes.push(selected_class(:link)) end classes = classes.reject { |c| c.nil? } + link_extra_classes(item) end
override in renderer if needed
# File lib/rocket_navigation/renderer/base.rb, line 71 def link_extra_classes(item) [] end
# File lib/rocket_navigation/renderer/base.rb, line 82 def link_html(item) ret = base_link_html.except(:class) ret.merge!({ method: item.method }) unless item.method.blank? classes = link_classes(item) ret.merge!({class: classes}) unless classes.blank? ret end
override this method if needed
# File lib/rocket_navigation/renderer/base.rb, line 94 def link_options(item) link_html(item) end
Renders the specified ItemContainer
to HTML.
When implementing a renderer, please consider to call include_sub_navigation? to determine whether an item's sub_navigation should be rendered or not.
# File lib/rocket_navigation/renderer/base.rb, line 123 def render(item_container) fail NotImplementedError, 'subclass responsibility' end
# File lib/rocket_navigation/renderer/base.rb, line 21 def selected_class(type) container.selected_class[type] || (options[:selected_class] || {})[type] end
# File lib/rocket_navigation/renderer/base.rb, line 106 def skip_if_empty? !!options[:skip_if_empty] end
to allow overriding when there is specific logic determining when a link should not be rendered (eg. breadcrumbs renderer does not render the final breadcrumb as a link when instructed not to do so.)
# File lib/rocket_navigation/renderer/base.rb, line 148 def suppress_link?(item) item.url.nil? end
render an item as a non-active link (span)
# File lib/rocket_navigation/renderer/base.rb, line 163 def suppressed_tag_for(item) content_tag('span', item.name, link_options(item).except(:method)) end
determine and return link or static content depending on item/renderer conditions.
# File lib/rocket_navigation/renderer/base.rb, line 154 def tag_for(item) if suppress_link?(item) suppressed_tag_for(item) else active_tag_for(item) end end