Class: Spree::Admin::Navigation::Item

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/admin/navigation/item.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, **options) ⇒ Item

Returns a new instance of Item.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/models/spree/admin/navigation/item.rb', line 8

def initialize(key, **options)
  @key = key.to_sym
  @label = options[:label]
  @url = options[:url]
  @icon = options[:icon]
  @position = options[:position] || 999
  @parent_key = options[:parent]
  @active_condition = options[:active]
  @condition = options.key?(:if) ? options[:if] : options[:condition]
  @badge = options[:badge]
  @badge_class = options[:badge_class]
  @tooltip = options[:tooltip]
  @target = options[:target]
  @data_attributes = options[:data_attributes] || {}
  @section_label = options[:section_label]
  @children = []
end

Instance Attribute Details

#active_conditionObject

Returns the value of attribute active_condition.



5
6
7
# File 'app/models/spree/admin/navigation/item.rb', line 5

def active_condition
  @active_condition
end

#badgeObject

Returns the value of attribute badge.



5
6
7
# File 'app/models/spree/admin/navigation/item.rb', line 5

def badge
  @badge
end

#badge_classObject

Returns the value of attribute badge_class.



5
6
7
# File 'app/models/spree/admin/navigation/item.rb', line 5

def badge_class
  @badge_class
end

#childrenObject

Returns the value of attribute children.



5
6
7
# File 'app/models/spree/admin/navigation/item.rb', line 5

def children
  @children
end

#conditionObject

Returns the value of attribute condition.



5
6
7
# File 'app/models/spree/admin/navigation/item.rb', line 5

def condition
  @condition
end

#data_attributesObject

Returns the value of attribute data_attributes.



5
6
7
# File 'app/models/spree/admin/navigation/item.rb', line 5

def data_attributes
  @data_attributes
end

#iconObject

Returns the value of attribute icon.



5
6
7
# File 'app/models/spree/admin/navigation/item.rb', line 5

def icon
  @icon
end

#keyObject

Returns the value of attribute key.



5
6
7
# File 'app/models/spree/admin/navigation/item.rb', line 5

def key
  @key
end

#labelObject

Returns the value of attribute label.



5
6
7
# File 'app/models/spree/admin/navigation/item.rb', line 5

def label
  @label
end

#parent_keyObject

Returns the value of attribute parent_key.



5
6
7
# File 'app/models/spree/admin/navigation/item.rb', line 5

def parent_key
  @parent_key
end

#positionObject

Returns the value of attribute position.



5
6
7
# File 'app/models/spree/admin/navigation/item.rb', line 5

def position
  @position
end

#section_labelObject

Returns the value of attribute section_label.



5
6
7
# File 'app/models/spree/admin/navigation/item.rb', line 5

def 
  @section_label
end

#targetObject

Returns the value of attribute target.



5
6
7
# File 'app/models/spree/admin/navigation/item.rb', line 5

def target
  @target
end

#tooltipObject

Returns the value of attribute tooltip.



5
6
7
# File 'app/models/spree/admin/navigation/item.rb', line 5

def tooltip
  @tooltip
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'app/models/spree/admin/navigation/item.rb', line 5

def url
  @url
end

Instance Method Details

#active?(current_path, context = nil) ⇒ Boolean

Check if this item is active based on current path

Parameters:

  • current_path (String)

    The current request path

  • context (Object) (defaults to: nil)

    View context with access to route helpers

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/models/spree/admin/navigation/item.rb', line 48

def active?(current_path, context = nil)
  # Use custom active condition if provided (most flexible)
  if active_condition.respond_to?(:call)
    if context&.respond_to?(:instance_exec)
      return context.instance_exec(&active_condition)
    else
      return active_condition.call
    end
  end

  # Match exact path
  item_url = resolve_url(context)
  return true if item_url && current_path == item_url

  # Check if any child item is active
  return true if children.any? { |child| child.active?(current_path, context) }

  # Default: match if path starts with url (handled by active_link_to)
  if item_url
    current_path.start_with?(item_url)
  else
    false
  end
end

#add_child(item) ⇒ Object

Add a child item



129
130
131
132
133
# File 'app/models/spree/admin/navigation/item.rb', line 129

def add_child(item)
  children << item
  item.parent_key = key
  sort_children!
end

#badge_value(view_context = nil) ⇒ Object

Compute badge value

Parameters:

  • view_context (Object) (defaults to: nil)

    View context with access to helper methods



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/models/spree/admin/navigation/item.rb', line 108

def badge_value(view_context = nil)
  return nil unless badge

  if badge.respond_to?(:call)
    # Evaluate badge in view context if available (for access to helpers)
    if view_context&.respond_to?(:instance_exec)
      view_context.instance_exec(&badge)
    else
      badge.call
    end
  else
    badge
  end
end

#deep_cloneObject

Deep clone for modifications



146
147
148
149
150
# File 'app/models/spree/admin/navigation/item.rb', line 146

def deep_clone
  cloned = self.class.new(key, to_h)
  cloned.children = children.map(&:deep_clone)
  cloned
end

#inspectObject



171
172
173
# File 'app/models/spree/admin/navigation/item.rb', line 171

def inspect
  "#<Spree::Admin::Navigation::Item key=#{key} label=#{label} children=#{children.size}>"
end

#remove_child(key) ⇒ Object

Remove a child item



136
137
138
# File 'app/models/spree/admin/navigation/item.rb', line 136

def remove_child(key)
  children.reject! { |child| child.key == key }
end

#resolve_labelObject

Resolve label (handles i18n keys)



99
100
101
102
103
104
# File 'app/models/spree/admin/navigation/item.rb', line 99

def resolve_label
  return label unless label.is_a?(String) || label.is_a?(Symbol)

  # Use Spree.t for translation which handles the spree namespace
  Spree.t(label, default: label.to_s.humanize)
end

#resolve_url(context = nil) ⇒ Object

Resolve URL (handles symbols, procs, and strings)

Parameters:

  • context (Object) (defaults to: nil)

    View context with access to route helpers



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/models/spree/admin/navigation/item.rb', line 75

def resolve_url(context = nil)
  case url
  when Symbol
    # Try to call the route helper on the context (which has spree routes)
    if context&.respond_to?(url)
      context.send(url)
    elsif context&.respond_to?(:spree)
      context.spree.send(url) rescue url.to_s
    else
      url.to_s
    end
  when Proc
    # Evaluate proc in the context where route helpers are available
    if context&.respond_to?(:instance_exec)
      context.instance_exec(&url)
    else
      url.call
    end
  else
    url
  end
end

#section?Boolean

Check if this is a section header

Returns:

  • (Boolean)


124
125
126
# File 'app/models/spree/admin/navigation/item.rb', line 124

def section?
  .present?
end

#sort_children!Object

Sort children by position



141
142
143
# File 'app/models/spree/admin/navigation/item.rb', line 141

def sort_children!
  children.sort_by! { |child| [child.position, child.key.to_s] }
end

#to_hObject

Convert to hash



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'app/models/spree/admin/navigation/item.rb', line 153

def to_h
  {
    label: label,
    url: url,
    icon: icon,
    position: position,
    parent: parent_key,
    active: active_condition,
    condition: condition,
    badge: badge,
    badge_class: badge_class,
    tooltip: tooltip,
    target: target,
    data_attributes: data_attributes,
    section_label: 
  }
end

#visible?(user_or_context = nil) ⇒ Boolean

Check if this item should be visible for the given user/context

Parameters:

  • user_or_context (Object) (defaults to: nil)

    Either a user object or a view context

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/spree/admin/navigation/item.rb', line 28

def visible?(user_or_context = nil)
  return true if condition.nil?

  if condition.respond_to?(:call)
    # If we have a view context with instance_exec, use it to evaluate the condition
    # This allows access to can? and other helper methods
    if user_or_context.respond_to?(:instance_exec)
      user_or_context.instance_exec(&condition)
    else
      # Otherwise, call with the user object
      condition.call(user_or_context)
    end
  else
    condition
  end
end