Class: SolidusAdmin::MenuItem
- Inherits:
-
Object
- Object
- SolidusAdmin::MenuItem
- Defined in:
- lib/solidus_admin/menu_item.rb
Overview
Encapsulates the data for a main navigation item.
Instance Attribute Summary collapse
- #children ⇒ Object readonly private
-
#icon ⇒ Object
readonly
Returns the value of attribute icon.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#position ⇒ Object
readonly
Returns the value of attribute position.
-
#route ⇒ Symbol, Proc
readonly
The route to use for this item.
- #top_level ⇒ Object readonly private
Instance Method Summary collapse
-
#active?(url_helpers, fullpath) ⇒ Boolean
Returns whether the item should be marked as active.
-
#children? ⇒ Boolean
Whether this item has any children.
-
#current?(url_helpers, fullpath) ⇒ Boolean
Returns whether the item should be marked as current.
-
#initialize(key:, position:, route:, match_path: nil, icon: nil, children: [], top_level: true) ⇒ MenuItem
constructor
A new instance of MenuItem.
- #name ⇒ Object
-
#path(url_helpers) ⇒ String
The path for this item.
Constructor Details
#initialize(key:, position:, route:, match_path: nil, icon: nil, children: [], top_level: true) ⇒ MenuItem
Returns a new instance of MenuItem.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/solidus_admin/menu_item.rb', line 29 def initialize( key:, position:, route:, match_path: nil, icon: nil, children: [], top_level: true ) @key = key @icon = icon @position = position @children = children @top_level = top_level @route = route @match_path = match_path end |
Instance Attribute Details
#children ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
27 28 29 |
# File 'lib/solidus_admin/menu_item.rb', line 27 def children @children end |
#icon ⇒ Object (readonly)
Returns the value of attribute icon.
12 13 14 |
# File 'lib/solidus_admin/menu_item.rb', line 12 def icon @icon end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
8 9 10 |
# File 'lib/solidus_admin/menu_item.rb', line 8 def key @key end |
#position ⇒ Object (readonly)
Returns the value of attribute position.
16 17 18 |
# File 'lib/solidus_admin/menu_item.rb', line 16 def position @position end |
#route ⇒ Symbol, Proc (readonly)
Returns the route to use for this item. When a symbol is given, it will be called on the solidus_admin url helpers. When a proc is given, it will be evaluated in a context that has access to the solidus url helpers.
24 25 26 |
# File 'lib/solidus_admin/menu_item.rb', line 24 def route @route end |
#top_level ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
27 28 29 |
# File 'lib/solidus_admin/menu_item.rb', line 27 def top_level @top_level end |
Instance Method Details
#active?(url_helpers, fullpath) ⇒ Boolean
Returns whether the item should be marked as active
An item is considered active when it is the current item or any of its children is active.
91 92 93 94 95 |
# File 'lib/solidus_admin/menu_item.rb', line 91 def active?(url_helpers, fullpath) current?(url_helpers, fullpath) || @match_path&.call(fullpath) || children.any? { |child| child.active?(url_helpers, fullpath) } end |
#children? ⇒ Boolean
Returns whether this item has any children.
52 53 54 |
# File 'lib/solidus_admin/menu_item.rb', line 52 def children? @children.any? end |
#current?(url_helpers, fullpath) ⇒ Boolean
Returns whether the item should be marked as current
An item is considered the current one if its base path (that is, the path without any query parameters) matches the given full path.
77 78 79 |
# File 'lib/solidus_admin/menu_item.rb', line 77 def current?(url_helpers, fullpath) path(url_helpers) == fullpath.gsub(/\?.*$/, '') end |
#name ⇒ Object
47 48 49 |
# File 'lib/solidus_admin/menu_item.rb', line 47 def name I18n.t("solidus_admin.menu_item.#{key}", default: key.to_s.humanize) end |
#path(url_helpers) ⇒ String
Returns the path for this item.
59 60 61 62 63 64 65 66 |
# File 'lib/solidus_admin/menu_item.rb', line 59 def path(url_helpers) case @route when Symbol url_helpers.solidus_admin.public_send(@route) when Proc url_helpers.instance_exec(&@route) end end |