Class: SimpleNavigation::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_navigation/core/item.rb,
lib/simple_navigation/rails_controller_methods.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, items = nil, &sub_nav_block) ⇒ Item

see ItemContainer#item

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



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/simple_navigation/core/item.rb', line 11

def initialize(container, key, name, url, options, items=nil, &sub_nav_block)
  @container = container
  @container.dom_class = options.delete(:container_class) if options[:container_class]
  @container.dom_id = options.delete(:container_id) if options[:container_id]
  @key = key
  @method = options.delete(:method)
  @name = name
  @url = url.instance_of?(Proc) ? url.call : url
  @highlights_on = options.delete(:highlights_on)
  @exclude_highlighting = options.delete(:exclude_highlight_if)
  @html_options = options
  if sub_nav_block || items
    @sub_navigation = ItemContainer.new(@container.level + 1)
    sub_nav_block.call @sub_navigation if sub_nav_block
    @sub_navigation.items = items if items
  end
end

Instance Attribute Details

#exclude_highlightingObject (readonly)

Returns the value of attribute exclude_highlighting.



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

def exclude_highlighting
  @exclude_highlighting
end

#highlights_onObject (readonly)

Returns the value of attribute highlights_on.



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

def highlights_on
  @highlights_on
end

#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.



42
43
44
45
46
47
48
# File 'lib/simple_navigation/core/item.rb', line 42

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

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

#methodObject (readonly)

Returns the value of attribute method.



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

def method
  @method
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Returns the value of attribute sub_navigation.



5
6
7
# File 'lib/simple_navigation/core/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/core/item.rb', line 5

def url
  @url
end

Instance Method Details

#item_excludedObject



50
51
52
53
54
55
# File 'lib/simple_navigation/core/item.rb', line 50

def item_excluded
  if exclude_highlighting
    raise ArgumentError, ':exclude_highlight_if must be a regexp' unless exclude_highlighting.instance_of?(Regexp)
    SimpleNavigation.request_uri =~ exclude_highlighting
  end
end

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


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

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

#selected_by_config?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/simple_navigation/core/item.rb', line 74

def selected_by_config?
  false
end

#selected_classObject

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



58
59
60
61
62
63
64
65
# File 'lib/simple_navigation/core/item.rb', line 58

def selected_class
  if selected?
    self.item_excluded ? nil : SimpleNavigation.config.selected_class
    # SimpleNavigation.config.selected_class
  else
    nil
  end
end