Class: RocketNavigation::Item

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/rocket_navigation/item.rb

Overview

Represents an item in your navigation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(container, key, name, url = nil, opts = {}, &sub_nav_block) ⇒ Item

see ItemContainer#item

The subnavigation (if any) is either provided by a block or passed in directly as items



21
22
23
24
25
26
27
28
29
# File 'lib/rocket_navigation/item.rb', line 21

def initialize(container, key, name, url = nil, opts = {}, &sub_nav_block)
  self.container = container
  self.key = key
  self.name = name.respond_to?(:call) ? name.call : name
  self.url =  url.respond_to?(:call) ? url.call : url
  self.options = opts

  setup_sub_navigation(options[:items], &sub_nav_block)
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



6
7
8
# File 'lib/rocket_navigation/item.rb', line 6

def key
  @key
end

#name(options = {}) ⇒ Object

Returns the item’s name. If :apply_generator option is set to true (default), the name will be passed to the name_generator specified in the configuration.



36
37
38
# File 'lib/rocket_navigation/item.rb', line 36

def name
  @name
end

Returns the value of attribute sub_navigation.



6
7
8
# File 'lib/rocket_navigation/item.rb', line 6

def sub_navigation
  @sub_navigation
end

#urlObject

Returns the value of attribute url.



6
7
8
# File 'lib/rocket_navigation/item.rb', line 6

def url
  @url
end

Instance Method Details

#active_branch?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/rocket_navigation/item.rb', line 50

def active_branch?
  @active_branch ||= selected_by_subnav? && !selected_by_condition?
end

#active_leaf?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/rocket_navigation/item.rb', line 54

def active_leaf?
  @active_leaf ||= selected_by_condition? && !selected_by_subnav?
end

#highlights_onObject

Returns the :highlights_on option as set at initialization



59
60
61
# File 'lib/rocket_navigation/item.rb', line 59

def highlights_on
  @highlights_on ||= options[:highlights_on]
end

#inspectObject



84
85
86
87
88
89
90
91
92
# File 'lib/rocket_navigation/item.rb', line 84

def inspect
"#<RocketNavigation::Item:#{object_id}
  @key=#{@key}
  @name=#{@name}
  @sub_navigation=#{@sub_navigation.inspect}
  @url=#{@url.inspect}
  @options=#{@options.inspect}
>"
end

#methodObject

Returns the :method option as set at initialization



64
65
66
# File 'lib/rocket_navigation/item.rb', line 64

def method
  @method ||= options[:method]
end

#root_path_match?Boolean

Returns true if both the item’s url and the request’s url are root_path

Returns:

  • (Boolean)


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

def root_path_match?
  url == '/'
end

#selected?Boolean

Returns true if this navigation item should be rendered as ‘selected’. An item is selected if

  • it has a subnavigation and one of its subnavigation items is selected or

  • its url matches the url of the current request (auto highlighting)

Returns:

  • (Boolean)


46
47
48
# File 'lib/rocket_navigation/item.rb', line 46

def selected?
  @selected ||= selected_by_subnav? || selected_by_condition?
end

#selected_by_condition?Boolean

Returns true if the item’s url matches the request’s current url.

Returns:

  • (Boolean)


75
76
77
# File 'lib/rocket_navigation/item.rb', line 75

def selected_by_condition?
  is_active_nav_link?(url, highlights_on)
end

#selected_by_subnav?Boolean

Returns true if item has a subnavigation and the sub_navigation is selected

Returns:

  • (Boolean)


70
71
72
# File 'lib/rocket_navigation/item.rb', line 70

def selected_by_subnav?
  sub_navigation && sub_navigation.selected?
end