Class: SimpleNavigation::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_navigation/item.rb

Overview

Represents an item in your navigation. Gets generated by the item method in the config-file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(container, key, name, url, options, sub_nav_block) ⇒ Item

see ItemContainer#item



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/simple_navigation/item.rb', line 9

def initialize(container, key, name, url, options, sub_nav_block) #:nodoc:
  @container = container
  @key = key
  @method = options.delete(:method)
  @name = name
  @url = url
  @html_options = options
  if sub_nav_block
    @sub_navigation = ItemContainer.new(@container.level + 1)
    sub_nav_block.call @sub_navigation
  end
end

Instance Attribute Details

#html_optionsObject

Returns the html-options hash for the item, i.e. the options specified for this item in the config-file. It also adds the ‘selected’ class to the list of classes if necessary.



35
36
37
38
39
40
41
# File 'lib/simple_navigation/item.rb', line 35

def html_options
  default_options = self.autogenerate_item_ids? ? {:id => key.to_s} : {}
  options = default_options.merge(@html_options)
  options[:class] = [@html_options[:class], self.selected_class].flatten.compact.join(' ')
  options.delete(:class) if options[:class].blank?
  options
end

#keyObject (readonly)

Returns the value of attribute key.



5
6
7
# File 'lib/simple_navigation/item.rb', line 5

def key
  @key
end

#methodObject (readonly)

Returns the value of attribute method.



5
6
7
# File 'lib/simple_navigation/item.rb', line 5

def method
  @method
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/simple_navigation/item.rb', line 5

def name
  @name
end

Returns the value of attribute sub_navigation.



5
6
7
# File 'lib/simple_navigation/item.rb', line 5

def sub_navigation
  @sub_navigation
end

#urlObject (readonly)

Returns the value of attribute url.



5
6
7
# File 'lib/simple_navigation/item.rb', line 5

def url
  @url
end

Instance Method Details

#selected?Boolean

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

  • it has been explicitly selected in a controller or

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


29
30
31
# File 'lib/simple_navigation/item.rb', line 29

def selected?
  @selected = @selected || selected_by_config? || selected_by_subnav? || selected_by_url?
end

#selected_classObject

Returns the configured selected_class if the item is selected, nil otherwise



45
46
47
# File 'lib/simple_navigation/item.rb', line 45

def selected_class
  selected? ? SimpleNavigation.config.selected_class : nil
end