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.
- #inspect ⇒ Object
-
#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
21 22 23 24 25 26 27 28 29 |
# File 'lib/rocket_navigation/item.rb', line 21 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.
36 37 38 |
# File 'lib/rocket_navigation/item.rb', line 36 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
50 51 52 |
# File 'lib/rocket_navigation/item.rb', line 50 def active_branch? @active_branch ||= && !selected_by_condition? end |
#active_leaf? ⇒ Boolean
54 55 56 |
# File 'lib/rocket_navigation/item.rb', line 54 def active_leaf? @active_leaf ||= selected_by_condition? && ! end |
#highlights_on ⇒ Object
Returns the :highlights_on option as set at initialization
59 60 61 |
# File 'lib/rocket_navigation/item.rb', line 59 def highlights_on @highlights_on ||= [:highlights_on] end |
#inspect ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/rocket_navigation/item.rb', line 84 def inspect "#<RocketNavigation::Item:#{object_id} @key=#{@key} @name=#{@name} @sub_navigation=#{@sub_navigation.inspect} @url=#{@url.inspect} @options=#{@options.inspect} >" end |
#method ⇒ Object
Returns the :method option as set at initialization
64 65 66 |
# File 'lib/rocket_navigation/item.rb', line 64 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
80 81 82 |
# File 'lib/rocket_navigation/item.rb', line 80 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)
46 47 48 |
# File 'lib/rocket_navigation/item.rb', line 46 def selected? @selected ||= || selected_by_condition? end |
#selected_by_condition? ⇒ Boolean
Returns true if the item’s url matches the request’s current url.
75 76 77 |
# File 'lib/rocket_navigation/item.rb', line 75 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
70 71 72 |
# File 'lib/rocket_navigation/item.rb', line 70 def && .selected? end |