Class: RocketNavigation::Renderer::Base

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, options = {})
  @container = container
  @options = options
end

Instance Attribute Details

#containerObject (readonly)

Returns the value of attribute container.



11
12
13
# File 'lib/rocket_navigation/renderer/base.rb', line 11

def container
  @container
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/rocket_navigation/renderer/base.rb', line 11

def options
  @options
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, link_options(item))
end

#base_item_htmlObject



34
35
36
# File 'lib/rocket_navigation/renderer/base.rb', line 34

def base_item_html
  @base_item_html ||= container.item_html.merge(options[:item_html] || {})
end


66
67
68
# File 'lib/rocket_navigation/renderer/base.rb', line 66

def base_link_html
  @base_link_html ||= container.link_html.merge(options[:link_html] || {})
end

#consider_sub_navigation?(item) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/rocket_navigation/renderer/base.rb', line 127

def consider_sub_navigation?(item)
  return false unless item.sub_navigation

  case level
  when :all
    true
  when Range
    item.sub_navigation.level <= level.max
  else
    false
  end
end

#container_htmlObject



25
26
27
# File 'lib/rocket_navigation/renderer/base.rb', line 25

def container_html
  @container_html ||= container.container_html.merge(options[:container_html] || {})
end

#container_optionsObject

override this method if needed



30
31
32
# File 'lib/rocket_navigation/renderer/base.rb', line 30

def container_options
  container_html
end

#expand_all?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/rocket_navigation/renderer/base.rb', line 98

def expand_all?
  !options.key?(:expand_all) || options[:expand_all] == false
end

#expand_sub_navigation?(item) ⇒ Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/rocket_navigation/renderer/base.rb', line 140

def expand_sub_navigation?(item)
  expand_all? || item.selected?
end

#include_sub_navigation?(item) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/rocket_navigation/renderer/base.rb', line 110

def include_sub_navigation?(item)
  consider_sub_navigation?(item) && expand_sub_navigation?(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_options(item)
  item_html(item)
end

#levelObject



102
103
104
# File 'lib/rocket_navigation/renderer/base.rb', line 102

def level
  options[:level] || :all
end


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

override in renderer if needed



71
72
73
# File 'lib/rocket_navigation/renderer/base.rb', line 71

def link_extra_classes(item)
  []
end


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

override this method if needed



94
95
96
# File 'lib/rocket_navigation/renderer/base.rb', line 94

def link_options(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 render_sub_navigation_for(item)
  item.sub_navigation.render(options)
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] || (options[:selected_class] || {})[type]
end

#skip_if_empty?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/rocket_navigation/renderer/base.rb', line 106

def skip_if_empty?
  !!options[: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.)

Returns:

  • (Boolean)


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)
  ('span', item.name, link_options(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