Class: SimpleNavigation::ItemContainer
- Inherits:
-
Object
- Object
- SimpleNavigation::ItemContainer
- Defined in:
- lib/simple_navigation/item_container.rb
Overview
Holds the Items for a navigation ‘level’.
Instance Attribute Summary collapse
-
#auto_highlight ⇒ Object
Returns the value of attribute auto_highlight.
-
#dom_class ⇒ Object
Returns the value of attribute dom_class.
-
#dom_id ⇒ Object
Returns the value of attribute dom_id.
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#renderer ⇒ Object
Returns the value of attribute renderer.
Instance Method Summary collapse
-
#[](navi_key) ⇒ Object
Returns the Item with the specified key, nil otherwise.
-
#active_item_container_for(desired_level) ⇒ Object
Returns the active item_container for the specified level (recursively looks up items in selected sub_navigation if level is deeper than this container’s level).
-
#current_explicit_navigation ⇒ Object
Returns the current navigation that has been explicitely defined in the controller for this container’s level.
-
#initialize(level = 1) ⇒ ItemContainer
constructor
:nodoc:.
-
#item(key, name, url, options = {}, &block) ⇒ Object
Creates a new navigation item.
-
#level_for_item(navi_key) ⇒ Object
Returns the level of the item specified by navi_key.
-
#render(include_sub_navigation = false, options = {}) ⇒ Object
Renders the items in this ItemContainer using the configured renderer.
-
#selected? ⇒ Boolean
Returns true if any of this container’s items is selected.
-
#selected_item ⇒ Object
Returns the currently selected item, nil if no item is selected.
Constructor Details
#initialize(level = 1) ⇒ ItemContainer
:nodoc:
9 10 11 12 13 14 |
# File 'lib/simple_navigation/item_container.rb', line 9 def initialize(level=1) #:nodoc: @level = level @items = [] @renderer = SimpleNavigation.config.renderer @auto_highlight = true end |
Instance Attribute Details
#auto_highlight ⇒ Object
Returns the value of attribute auto_highlight.
7 8 9 |
# File 'lib/simple_navigation/item_container.rb', line 7 def auto_highlight @auto_highlight end |
#dom_class ⇒ Object
Returns the value of attribute dom_class.
7 8 9 |
# File 'lib/simple_navigation/item_container.rb', line 7 def dom_class @dom_class end |
#dom_id ⇒ Object
Returns the value of attribute dom_id.
7 8 9 |
# File 'lib/simple_navigation/item_container.rb', line 7 def dom_id @dom_id end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
6 7 8 |
# File 'lib/simple_navigation/item_container.rb', line 6 def items @items end |
#level ⇒ Object (readonly)
Returns the value of attribute level.
6 7 8 |
# File 'lib/simple_navigation/item_container.rb', line 6 def level @level end |
#renderer ⇒ Object
Returns the value of attribute renderer.
7 8 9 |
# File 'lib/simple_navigation/item_container.rb', line 7 def renderer @renderer end |
Instance Method Details
#[](navi_key) ⇒ Object
Returns the Item with the specified key, nil otherwise.
40 41 42 |
# File 'lib/simple_navigation/item_container.rb', line 40 def [](navi_key) items.find {|i| i.key == navi_key} end |
#active_item_container_for(desired_level) ⇒ Object
Returns the active item_container for the specified level (recursively looks up items in selected sub_navigation if level is deeper than this container’s level).
89 90 91 92 93 |
# File 'lib/simple_navigation/item_container.rb', line 89 def active_item_container_for(desired_level) return self if self.level == desired_level return nil unless return selected_item..active_item_container_for(desired_level) end |
#current_explicit_navigation ⇒ Object
Returns the current navigation that has been explicitely defined in the controller for this container’s level. Returns nil if no explicit current navigation has been set.
82 83 84 |
# File 'lib/simple_navigation/item_container.rb', line 82 def SimpleNavigation.(level) end |
#item(key, name, url, options = {}, &block) ⇒ Object
Creates a new navigation item.
The key
is a symbol which uniquely defines your navigation item in the scope of the primary_navigation or the sub_navigation.
The name
will be displayed in the rendered navigation. This can also be a call to your I18n-framework.
The url
is the address that the generated item points to. You can also use url_helpers (named routes, restful routes helper, url_for etc.)
The options
can be used to specify the following things:
-
html_attributes
- will be included in the rendered navigation item (e.g. id, class etc.) -
:if
- Specifies a proc to call to determine if the item should be rendered (e.g.:if => Proc.new { current_user.admin? }
). The proc should evaluate to a true or false value and is evaluated in the context of the view. -
:unless
- Specifies a proc to call to determine if the item should not be rendered (e.g.:unless => Proc.new { current_user.admin? }
). The proc should evaluate to a true or false value and is evaluated in the context of the view.
The block
- if specified - will hold the item’s sub_navigation.
34 35 36 |
# File 'lib/simple_navigation/item_container.rb', line 34 def item(key, name, url, ={}, &block) (@items << SimpleNavigation::Item.new(self, key, name, url, , block)) if should_add_item?() end |
#level_for_item(navi_key) ⇒ Object
Returns the level of the item specified by navi_key. Recursively works its way down the item’s sub_navigations if the desired item is not found directly in this container’s items. Returns nil item cannot be found.
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/simple_navigation/item_container.rb', line 48 def level_for_item(navi_key) my_item = self[navi_key] return self.level if my_item items.each do |i| if i. level = i..level_for_item(navi_key) return level unless level.nil? end end return nil end |
#render(include_sub_navigation = false, options = {}) ⇒ Object
Renders the items in this ItemContainer using the configured renderer.
Set include_sub_navigation
to true if you want to nest the sub_navigation into the active parent_navigation
63 64 65 |
# File 'lib/simple_navigation/item_container.rb', line 63 def render(=false, ={}) self.renderer.new.render(self, , ) end |
#selected? ⇒ Boolean
Returns true if any of this container’s items is selected.
69 70 71 |
# File 'lib/simple_navigation/item_container.rb', line 69 def selected? items.any? {|i| i.selected?} end |
#selected_item ⇒ Object
Returns the currently selected item, nil if no item is selected.
75 76 77 |
# File 'lib/simple_navigation/item_container.rb', line 75 def selected_item self[] || items.find {|i| i.selected?} end |