Class: SolidusAdmin::MenuItem

Inherits:
Object
  • Object
show all
Defined in:
lib/solidus_admin/menu_item.rb

Overview

Encapsulates the data for a main navigation item.

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#childrenObject (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

#iconObject (readonly)

Returns the value of attribute icon.



12
13
14
# File 'lib/solidus_admin/menu_item.rb', line 12

def icon
  @icon
end

#keyObject (readonly)

Returns the value of attribute key.



8
9
10
# File 'lib/solidus_admin/menu_item.rb', line 8

def key
  @key
end

#positionObject (readonly)

Returns the value of attribute position.



16
17
18
# File 'lib/solidus_admin/menu_item.rb', line 16

def position
  @position
end

#routeSymbol, 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.

Returns:

  • (Symbol, Proc)

    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.

See Also:



24
25
26
# File 'lib/solidus_admin/menu_item.rb', line 24

def route
  @route
end

#top_levelObject (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.

Parameters:

  • url_helpers (#solidus_admin, #spree)

    context object giving access to url helpers

  • fullpath (String)

    the full path of the current request

Returns:

  • (Boolean)

See Also:



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.

Returns:

  • (Boolean)

    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.

Parameters:

  • url_helpers (#solidus_admin, #spree)

    context object giving access to url helpers

  • fullpath (String)

    the full path of the current request

Returns:

  • (Boolean)


77
78
79
# File 'lib/solidus_admin/menu_item.rb', line 77

def current?(url_helpers, fullpath)
  path(url_helpers) == fullpath.gsub(/\?.*$/, '')
end

#nameObject



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.

Parameters:

  • url_helpers (#solidus_admin, #spree)

    context object giving access to url helpers

Returns:

  • (String)

    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