Class: SimpleNavigation::Item
- Inherits:
-
Object
- Object
- SimpleNavigation::Item
- 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
-
#html_options ⇒ Object
Returns the html-options hash for the item, i.e.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#sub_navigation ⇒ Object
readonly
Returns the value of attribute sub_navigation.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#initialize(container, key, name, url, options, sub_nav_block) ⇒ Item
constructor
see ItemContainer#item.
-
#selected? ⇒ Boolean
Returns true if this navigation item should be rendered as ‘selected’.
-
#selected_class ⇒ Object
Returns the configured selected_class if the item is selected, nil otherwise.
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, , sub_nav_block) #:nodoc: @container = container @key = key @method = .delete(:method) @name = name @url = url @html_options = if sub_nav_block @sub_navigation = ItemContainer.new(@container.level + 1) sub_nav_block.call @sub_navigation end end |
Instance Attribute Details
#html_options ⇒ Object
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 = self.autogenerate_item_ids? ? {:id => key.to_s} : {} = .merge(@html_options) [:class] = [@html_options[:class], self.selected_class].flatten.compact.join(' ') .delete(:class) if [:class].blank? end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
5 6 7 |
# File 'lib/simple_navigation/item.rb', line 5 def key @key end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
5 6 7 |
# File 'lib/simple_navigation/item.rb', line 5 def method @method end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/simple_navigation/item.rb', line 5 def name @name end |
#sub_navigation ⇒ Object (readonly)
Returns the value of attribute sub_navigation.
5 6 7 |
# File 'lib/simple_navigation/item.rb', line 5 def @sub_navigation end |
#url ⇒ Object (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)
29 30 31 |
# File 'lib/simple_navigation/item.rb', line 29 def selected? @selected = @selected || selected_by_config? || || selected_by_url? end |
#selected_class ⇒ Object
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 |