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
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_html(item) ⇒ Object
-
#item_options(item) ⇒ Object
override this method if needed.
- #level ⇒ Object
- #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)
154 155 156 |
# File 'lib/rocket_navigation/renderer/base.rb', line 154 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
57 58 59 |
# File 'lib/rocket_navigation/renderer/base.rb', line 57 def base_link_html @base_link_html ||= container.link_html.merge([:link_html] || {}) end |
#consider_sub_navigation?(item) ⇒ Boolean
113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/rocket_navigation/renderer/base.rb', line 113 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
84 85 86 |
# File 'lib/rocket_navigation/renderer/base.rb', line 84 def !.key?(:expand_all) || [:expand_all] == false end |
#expand_sub_navigation?(item) ⇒ Boolean
126 127 128 |
# File 'lib/rocket_navigation/renderer/base.rb', line 126 def (item) || item.selected? end |
#include_sub_navigation?(item) ⇒ Boolean
96 97 98 |
# File 'lib/rocket_navigation/renderer/base.rb', line 96 def (item) (item) && (item) end |
#item_html(item) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rocket_navigation/renderer/base.rb', line 38 def item_html(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 base_item_html.except(:class).merge({ class: classes.reject { |c| c.nil? } }) end |
#item_options(item) ⇒ Object
override this method if needed
53 54 55 |
# File 'lib/rocket_navigation/renderer/base.rb', line 53 def (item) item_html(item) end |
#level ⇒ Object
88 89 90 |
# File 'lib/rocket_navigation/renderer/base.rb', line 88 def level [:level] || :all end |
#link_html(item) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rocket_navigation/renderer/base.rb', line 61 def link_html(item) classes = Array.wrap(base_link_html[:class] || []) if item.selected? classes.push(selected_class(:link)) end ret = base_link_html.except(:class) ret.merge!({ class: classes.reject { |c| c.nil? } }) unless item.method.blank? ret.merge!({ method: method }) end ret end |
#link_options(item) ⇒ Object
override this method if needed
80 81 82 |
# File 'lib/rocket_navigation/renderer/base.rb', line 80 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.
109 110 111 |
# File 'lib/rocket_navigation/renderer/base.rb', line 109 def render(item_container) fail NotImplementedError, 'subclass responsibility' end |
#render_sub_navigation_for(item) ⇒ Object
100 101 102 |
# File 'lib/rocket_navigation/renderer/base.rb', line 100 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
92 93 94 |
# File 'lib/rocket_navigation/renderer/base.rb', line 92 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.)
134 135 136 |
# File 'lib/rocket_navigation/renderer/base.rb', line 134 def suppress_link?(item) item.url.nil? end |
#suppressed_tag_for(item) ⇒ Object
render an item as a non-active link (span)
149 150 151 |
# File 'lib/rocket_navigation/renderer/base.rb', line 149 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.
140 141 142 143 144 145 146 |
# File 'lib/rocket_navigation/renderer/base.rb', line 140 def tag_for(item) if suppress_link?(item) suppressed_tag_for(item) else active_tag_for(item) end end |