Class: RocketNavigation::Renderer::Base
- Inherits:
-
Object
- Object
- RocketNavigation::Renderer::Base
- Extended by:
- Forwardable
- Defined in:
- lib/rocket_navigation/renderer/base.rb
Overview
This is the base class for all renderers.
A renderer is responsible for rendering an ItemContainer and its containing items to HTML.
Direct Known Subclasses
Bootstrap, Breadcrumbs, BreadcrumbsOnRails, Json, Links, List, Text
Instance Attribute Summary collapse
-
#container ⇒ Object
readonly
Returns the value of attribute container.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#active_tag_for(item) ⇒ Object
render an item as an active link (a).
- #base_item_html ⇒ Object
- #base_link_html ⇒ Object
- #consider_sub_navigation?(item) ⇒ Boolean
- #container_html ⇒ Object
-
#container_options ⇒ Object
override this method if needed.
- #expand_all? ⇒ Boolean
- #expand_sub_navigation?(item) ⇒ Boolean
- #include_sub_navigation?(item) ⇒ Boolean
-
#initialize(container, options = {}) ⇒ Base
constructor
A new instance of Base.
- #item_classes(item) ⇒ Object
-
#item_extra_classes(item) ⇒ Object
override in renderer if needed.
- #item_html(item) ⇒ Object
-
#item_options(item) ⇒ Object
override this method if needed.
- #level ⇒ Object
- #link_classes(item) ⇒ Object
-
#link_extra_classes(item) ⇒ Object
override in renderer if needed.
- #link_html(item) ⇒ Object
-
#link_options(item) ⇒ Object
override this method if needed.
-
#render(item_container) ⇒ Object
Renders the specified ItemContainer to HTML.
- #render_sub_navigation_for(item) ⇒ Object
- #selected_class(type) ⇒ Object
- #skip_if_empty? ⇒ Boolean
-
#suppress_link?(item) ⇒ Boolean
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.).
-
#suppressed_tag_for(item) ⇒ Object
render an item as a non-active link (span).
-
#tag_for(item) ⇒ Object
determine and return link or static content depending on item/renderer conditions.
Constructor Details
#initialize(container, options = {}) ⇒ Base
Returns a new instance of Base.
16 17 18 19 |
# File 'lib/rocket_navigation/renderer/base.rb', line 16 def initialize(container, = {}) @container = container = end |
Instance Attribute Details
#container ⇒ Object (readonly)
Returns the value of attribute container.
11 12 13 |
# File 'lib/rocket_navigation/renderer/base.rb', line 11 def container @container end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
11 12 13 |
# File 'lib/rocket_navigation/renderer/base.rb', line 11 def end |
Instance Method Details
#active_tag_for(item) ⇒ Object
render an item as an active link (a)
168 169 170 |
# File 'lib/rocket_navigation/renderer/base.rb', line 168 def active_tag_for(item) link_to(item.name, item.url, (item)) end |
#base_item_html ⇒ Object
34 35 36 |
# File 'lib/rocket_navigation/renderer/base.rb', line 34 def base_item_html @base_item_html ||= container.item_html.merge([:item_html] || {}) end |
#base_link_html ⇒ Object
66 67 68 |
# File 'lib/rocket_navigation/renderer/base.rb', line 66 def base_link_html @base_link_html ||= container.link_html.merge([:link_html] || {}) end |
#consider_sub_navigation?(item) ⇒ Boolean
127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/rocket_navigation/renderer/base.rb', line 127 def (item) return false unless item. case level when :all true when Range item..level <= level.max else false end end |
#container_html ⇒ Object
25 26 27 |
# File 'lib/rocket_navigation/renderer/base.rb', line 25 def container_html @container_html ||= container.container_html.merge([:container_html] || {}) end |
#container_options ⇒ Object
override this method if needed
30 31 32 |
# File 'lib/rocket_navigation/renderer/base.rb', line 30 def container_html end |
#expand_all? ⇒ Boolean
98 99 100 |
# File 'lib/rocket_navigation/renderer/base.rb', line 98 def !.key?(:expand_all) || [:expand_all] == false end |
#expand_sub_navigation?(item) ⇒ Boolean
140 141 142 |
# File 'lib/rocket_navigation/renderer/base.rb', line 140 def (item) || item.selected? end |
#include_sub_navigation?(item) ⇒ Boolean
110 111 112 |
# File 'lib/rocket_navigation/renderer/base.rb', line 110 def (item) (item) && (item) end |
#item_classes(item) ⇒ Object
42 43 44 45 46 47 48 49 50 51 |
# 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 |
#item_extra_classes(item) ⇒ Object
override in renderer if needed
39 40 41 |
# File 'lib/rocket_navigation/renderer/base.rb', line 39 def item_extra_classes(item) [] end |
#item_html(item) ⇒ Object
53 54 55 56 57 58 59 |
# 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 |
#item_options(item) ⇒ Object
override this method if needed
62 63 64 |
# File 'lib/rocket_navigation/renderer/base.rb', line 62 def (item) item_html(item) end |
#level ⇒ Object
102 103 104 |
# File 'lib/rocket_navigation/renderer/base.rb', line 102 def level [:level] || :all end |
#link_classes(item) ⇒ Object
74 75 76 77 78 79 80 |
# 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 |
#link_extra_classes(item) ⇒ Object
override in renderer if needed
71 72 73 |
# File 'lib/rocket_navigation/renderer/base.rb', line 71 def link_extra_classes(item) [] end |
#link_html(item) ⇒ Object
82 83 84 85 86 87 88 89 90 91 |
# 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 |
#link_options(item) ⇒ Object
override this method if needed
94 95 96 |
# File 'lib/rocket_navigation/renderer/base.rb', line 94 def (item) link_html(item) end |
#render(item_container) ⇒ Object
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.
123 124 125 |
# File 'lib/rocket_navigation/renderer/base.rb', line 123 def render(item_container) fail NotImplementedError, 'subclass responsibility' end |
#render_sub_navigation_for(item) ⇒ Object
114 115 116 |
# File 'lib/rocket_navigation/renderer/base.rb', line 114 def (item) item..render() end |
#selected_class(type) ⇒ Object
21 22 23 |
# File 'lib/rocket_navigation/renderer/base.rb', line 21 def selected_class(type) container.selected_class[type] || ([:selected_class] || {})[type] end |
#skip_if_empty? ⇒ Boolean
106 107 108 |
# File 'lib/rocket_navigation/renderer/base.rb', line 106 def skip_if_empty? !![:skip_if_empty] end |
#suppress_link?(item) ⇒ Boolean
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.)
148 149 150 |
# File 'lib/rocket_navigation/renderer/base.rb', line 148 def suppress_link?(item) item.url.nil? end |
#suppressed_tag_for(item) ⇒ Object
render an item as a non-active link (span)
163 164 165 |
# File 'lib/rocket_navigation/renderer/base.rb', line 163 def suppressed_tag_for(item) content_tag('span', item.name, (item).except(:method)) end |
#tag_for(item) ⇒ Object
determine and return link or static content depending on item/renderer conditions.
154 155 156 157 158 159 160 |
# 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 |