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

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)



154
155
156
# File 'lib/rocket_navigation/renderer/base.rb', line 154

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


57
58
59
# File 'lib/rocket_navigation/renderer/base.rb', line 57

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

#consider_sub_navigation?(item) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rocket_navigation/renderer/base.rb', line 113

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)


84
85
86
# File 'lib/rocket_navigation/renderer/base.rb', line 84

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

#expand_sub_navigation?(item) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/rocket_navigation/renderer/base.rb', line 126

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

#include_sub_navigation?(item) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/rocket_navigation/renderer/base.rb', line 96

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

#levelObject



88
89
90
# File 'lib/rocket_navigation/renderer/base.rb', line 88

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


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

override this method if needed



80
81
82
# File 'lib/rocket_navigation/renderer/base.rb', line 80

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.



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 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)


92
93
94
# File 'lib/rocket_navigation/renderer/base.rb', line 92

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)


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)
  ('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.



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