Class: RocketNavigation::Item
- Inherits:
-
Object
- Object
- RocketNavigation::Item
- Extended by:
- Forwardable
- Defined in:
- lib/rocket_navigation/item.rb
Overview
Represents an item in your navigation.
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#name(options = {}) ⇒ Object
readonly
Returns the item’s 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
- #active_branch? ⇒ Boolean
- #active_leaf? ⇒ Boolean
-
#highlights_on ⇒ Object
Returns the :highlights_on option as set at initialization.
-
#initialize(container, key, name, url = nil, opts = {}, &sub_nav_block) ⇒ Item
constructor
see ItemContainer#item.
-
#method ⇒ Object
Returns the :method option as set at initialization.
-
#root_path_match? ⇒ Boolean
Returns true if both the item’s url and the request’s url are root_path.
-
#selected? ⇒ Boolean
Returns true if this navigation item should be rendered as ‘selected’.
-
#selected_by_condition? ⇒ Boolean
Returns true if the item’s url matches the request’s current url.
-
#selected_by_subnav? ⇒ Boolean
Returns true if item has a subnavigation and the sub_navigation is selected.
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
19 20 21 22 23 24 25 26 27 |
# File 'lib/rocket_navigation/item.rb', line 19 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.
6 7 8 |
# File 'lib/rocket_navigation/item.rb', line 6 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.
34 35 36 |
# File 'lib/rocket_navigation/item.rb', line 34 def name @name end |
#sub_navigation ⇒ Object
Returns the value of attribute sub_navigation.
6 7 8 |
# File 'lib/rocket_navigation/item.rb', line 6 def end |
#url ⇒ Object
Returns the value of attribute url.
6 7 8 |
# File 'lib/rocket_navigation/item.rb', line 6 def url @url end |
Instance Method Details
#active_branch? ⇒ Boolean
48 49 50 |
# File 'lib/rocket_navigation/item.rb', line 48 def active_branch? @active_branch ||= && !selected_by_condition? end |
#active_leaf? ⇒ Boolean
52 53 54 |
# File 'lib/rocket_navigation/item.rb', line 52 def active_leaf? @active_leaf ||= selected_by_condition? end |
#highlights_on ⇒ Object
Returns the :highlights_on option as set at initialization
57 58 59 |
# File 'lib/rocket_navigation/item.rb', line 57 def highlights_on @highlights_on ||= [:highlights_on] end |
#method ⇒ Object
Returns the :method option as set at initialization
62 63 64 |
# File 'lib/rocket_navigation/item.rb', line 62 def method @method ||= [:method] end |
#root_path_match? ⇒ Boolean
Returns true if both the item’s url and the request’s url are root_path
78 79 80 |
# File 'lib/rocket_navigation/item.rb', line 78 def root_path_match? url == '/' 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)
44 45 46 |
# File 'lib/rocket_navigation/item.rb', line 44 def selected? @selected ||= || selected_by_condition? end |
#selected_by_condition? ⇒ Boolean
Returns true if the item’s url matches the request’s current url.
73 74 75 |
# File 'lib/rocket_navigation/item.rb', line 73 def selected_by_condition? is_active_nav_link?(url, highlights_on) end |
#selected_by_subnav? ⇒ Boolean
Returns true if item has a subnavigation and the sub_navigation is selected
68 69 70 |
# File 'lib/rocket_navigation/item.rb', line 68 def && .selected? end |