Class: Spree::Admin::Navigation::Item
- Inherits:
-
Object
- Object
- Spree::Admin::Navigation::Item
- Defined in:
- app/models/spree/admin/navigation/item.rb
Instance Attribute Summary collapse
-
#active_condition ⇒ Object
Returns the value of attribute active_condition.
-
#badge ⇒ Object
Returns the value of attribute badge.
-
#badge_class ⇒ Object
Returns the value of attribute badge_class.
-
#children ⇒ Object
Returns the value of attribute children.
-
#condition ⇒ Object
Returns the value of attribute condition.
-
#data_attributes ⇒ Object
Returns the value of attribute data_attributes.
-
#icon ⇒ Object
Returns the value of attribute icon.
-
#key ⇒ Object
Returns the value of attribute key.
-
#label ⇒ Object
Returns the value of attribute label.
-
#parent_key ⇒ Object
Returns the value of attribute parent_key.
-
#position ⇒ Object
Returns the value of attribute position.
-
#section_label ⇒ Object
Returns the value of attribute section_label.
-
#target ⇒ Object
Returns the value of attribute target.
-
#tooltip ⇒ Object
Returns the value of attribute tooltip.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
-
#active?(current_path, context = nil) ⇒ Boolean
Check if this item is active based on current path.
-
#add_child(item) ⇒ Object
Add a child item.
-
#badge_value(view_context = nil) ⇒ Object
Compute badge value.
-
#deep_clone ⇒ Object
Deep clone for modifications.
-
#initialize(key, **options) ⇒ Item
constructor
A new instance of Item.
- #inspect ⇒ Object
-
#remove_child(key) ⇒ Object
Remove a child item.
-
#resolve_label ⇒ Object
Resolve label (handles i18n keys).
-
#resolve_url(context = nil) ⇒ Object
Resolve URL (handles symbols, procs, and strings).
-
#section? ⇒ Boolean
Check if this is a section header.
-
#sort_children! ⇒ Object
Sort children by position.
-
#to_h ⇒ Object
Convert to hash.
-
#visible?(user_or_context = nil) ⇒ Boolean
Check if this item should be visible for the given user/context.
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, **) @key = key.to_sym @label = [:label] @url = [:url] @icon = [:icon] @position = [:position] || 999 @parent_key = [:parent] @active_condition = [:active] @condition = .key?(:if) ? [:if] : [:condition] @badge = [:badge] @badge_class = [:badge_class] @tooltip = [:tooltip] @target = [:target] @data_attributes = [:data_attributes] || {} @section_label = [:section_label] @children = [] end |
Instance Attribute Details
#active_condition ⇒ Object
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 |
#badge ⇒ Object
Returns the value of attribute badge.
5 6 7 |
# File 'app/models/spree/admin/navigation/item.rb', line 5 def badge @badge end |
#badge_class ⇒ Object
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 |
#children ⇒ Object
Returns the value of attribute children.
5 6 7 |
# File 'app/models/spree/admin/navigation/item.rb', line 5 def children @children end |
#condition ⇒ Object
Returns the value of attribute condition.
5 6 7 |
# File 'app/models/spree/admin/navigation/item.rb', line 5 def condition @condition end |
#data_attributes ⇒ Object
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 |
#icon ⇒ Object
Returns the value of attribute icon.
5 6 7 |
# File 'app/models/spree/admin/navigation/item.rb', line 5 def icon @icon end |
#key ⇒ Object
Returns the value of attribute key.
5 6 7 |
# File 'app/models/spree/admin/navigation/item.rb', line 5 def key @key end |
#label ⇒ Object
Returns the value of attribute label.
5 6 7 |
# File 'app/models/spree/admin/navigation/item.rb', line 5 def label @label end |
#parent_key ⇒ Object
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 |
#position ⇒ Object
Returns the value of attribute position.
5 6 7 |
# File 'app/models/spree/admin/navigation/item.rb', line 5 def position @position end |
#section_label ⇒ Object
Returns the value of attribute section_label.
5 6 7 |
# File 'app/models/spree/admin/navigation/item.rb', line 5 def section_label @section_label end |
#target ⇒ Object
Returns the value of attribute target.
5 6 7 |
# File 'app/models/spree/admin/navigation/item.rb', line 5 def target @target end |
#tooltip ⇒ Object
Returns the value of attribute tooltip.
5 6 7 |
# File 'app/models/spree/admin/navigation/item.rb', line 5 def tooltip @tooltip end |
#url ⇒ Object
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
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
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_clone ⇒ Object
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 |
#inspect ⇒ Object
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_label ⇒ Object
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)
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
124 125 126 |
# File 'app/models/spree/admin/navigation/item.rb', line 124 def section? section_label.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_h ⇒ Object
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: section_label } end |
#visible?(user_or_context = nil) ⇒ Boolean
Check if this item should be visible for the given user/context
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 |