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
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#name(options = {}) ⇒ Object
readonly
Returns the item’s name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#sub_navigation ⇒ Object
readonly
Returns the value of attribute sub_navigation.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#active_leaf_class ⇒ Object
Returns the configured active_leaf_class if the item is the selected leaf, nil otherwise.
-
#highlights_on ⇒ Object
Returns the :highlights_on option as set at initialization.
-
#html_options ⇒ Object
Returns the html-options hash for the item, i.e.
-
#initialize(container, key, name, url = nil, opts = {}, &sub_nav_block) ⇒ Item
constructor
see ItemContainer#item.
-
#link_html_options ⇒ Object
Returns the html attributes for the link as set with the :link_html option at initialization.
-
#method ⇒ Object
Returns the :method option as set at initialization.
-
#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 = 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
15 16 17 18 19 20 21 22 23 |
# File 'lib/simple_navigation/item.rb', line 15 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. = opts ([:items], &sub_nav_block) end |
Instance Attribute Details
#key ⇒ Object
Returns the value of attribute key.
5 6 7 |
# File 'lib/simple_navigation/item.rb', line 5 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.
30 31 32 |
# File 'lib/simple_navigation/item.rb', line 30 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/simple_navigation/item.rb', line 5 def @options end |
#sub_navigation ⇒ Object
Returns the value of attribute sub_navigation.
5 6 7 |
# File 'lib/simple_navigation/item.rb', line 5 def @sub_navigation end |
#url ⇒ Object
Returns the value of attribute url.
5 6 7 |
# File 'lib/simple_navigation/item.rb', line 5 def url @url end |
Instance Method Details
#active_leaf_class ⇒ Object
Returns the configured active_leaf_class if the item is the selected leaf, nil otherwise
65 66 67 68 69 |
# File 'lib/simple_navigation/item.rb', line 65 def active_leaf_class if ! && selected_by_condition? config.active_leaf_class end end |
#highlights_on ⇒ Object
Returns the :highlights_on option as set at initialization
80 81 82 |
# File 'lib/simple_navigation/item.rb', line 80 def highlights_on @highlights_on ||= [:highlights_on] end |
#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.
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/simple_navigation/item.rb', line 52 def html_opts = .fetch(:html) { Hash.new } html_opts[:id] ||= autogenerated_item_id classes = [html_opts[:class], selected_class, active_leaf_class] classes = classes.flatten.compact.join(' ') html_opts[:class] = classes if classes && !classes.empty? html_opts end |
#link_html_options ⇒ Object
Returns the html attributes for the link as set with the :link_html option at initialization
91 92 93 |
# File 'lib/simple_navigation/item.rb', line 91 def @link_html_options ||= [:link_html] end |
#method ⇒ Object
Returns the :method option as set at initialization
85 86 87 |
# File 'lib/simple_navigation/item.rb', line 85 def method @method ||= [:method] 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)
45 46 47 |
# File 'lib/simple_navigation/item.rb', line 45 def selected? @selected ||= || selected_by_condition? end |
#selected_class ⇒ Object
Returns the configured selected_class if the item is selected, nil otherwise
73 74 75 76 77 |
# File 'lib/simple_navigation/item.rb', line 73 def selected_class if selected? container.selected_class || config.selected_class end end |