Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Simplify the admin navigation system (#13292)

Damian Legawiec174c6284367e
diff --git a/.claude/settings.local.json b/.claude/settings.local.json
index 478a680bbf..1246848569 100644
--- a/.claude/settings.local.json
+++ b/.claude/settings.local.json
@@ -11,8 +11,9 @@
"Bash(bundle exec rails:*)",
"Bash(curl:*)",
"Bash(bundle exec rake:*)",
- "Bash(bin/importmap:*)"
+ "Bash(bin/importmap:*)",
+ "Bash(ruby -c:*)"
],
"deny": []
}
-}
\ No newline at end of file
+}
diff --git a/admin/app/helpers/spree/admin/navigation_helper.rb b/admin/app/helpers/spree/admin/navigation_helper.rb
index 9589837bbb..7aa3aaa8e3 100644
--- a/admin/app/helpers/spree/admin/navigation_helper.rb
+++ b/admin/app/helpers/spree/admin/navigation_helper.rb
@@ -334,36 +334,7 @@ module Spree
# @return [Array<Spree::Admin::Navigation::Item>] the visible navigation items
def navigation_items(context = :sidebar)
# Pass the view context (self) so that can? and other helpers are available
- Spree::Admin::Navigation.visible_items(context, self)
- end
-
- # Get the current navigation context based on the current page
- # @return [Symbol] the current navigation context
- def current_navigation_context
- settings_area? ? :settings : :sidebar
- end
-
- # Check if a specific navigation item is active
- # @param key [Symbol] the navigation item key
- # @param context [Symbol] the navigation context
- # @return [Boolean] whether the navigation item is active
- def navigation_item_active?(key, context = current_navigation_context)
- item = Spree::Admin::Navigation.for(context).find(key)
- item&.active?(request.path, self)
- end
-
- # Get the active navigation item for the current path
- # @param context [Symbol] the navigation context
- # @return [Spree::Admin::Navigation::Item, nil] the active navigation item
- def active_navigation_item(context = current_navigation_context)
- Spree::Admin::Navigation.find_active_item(request.path, context, self)
- end
-
- # Get breadcrumbs from navigation for current path
- # @param context [Symbol] the navigation context
- # @return [Array<Spree::Admin::Navigation::Item>] the breadcrumb trail
- def navigation_breadcrumbs(context = current_navigation_context)
- Spree::Admin::Navigation.breadcrumbs_for(request.path, context, self)
+ Rails.application.config.spree_admin.navigation.send(context).visible_items(self)
end
# Renders page tab navigation for the given context
diff --git a/admin/app/models/spree/admin/navigation.rb b/admin/app/models/spree/admin/navigation.rb
index 4ff38a5387..722dd62396 100644
--- a/admin/app/models/spree/admin/navigation.rb
+++ b/admin/app/models/spree/admin/navigation.rb
@@ -1,112 +1,218 @@
module Spree
module Admin
class Navigation
- class << self
- # Store registries for different contexts
- def registries
- @registries ||= {}
- end
+ attr_reader :items, :context
- # Get or create a registry for a context
- def registry(context = :sidebar)
- registries[context.to_sym] ||= Registry.new(context.to_sym)
- end
+ def initialize(context)
+ @context = context
+ @items = {}
+ end
+
+ # Add a navigation item
+ def add(key, **options, &block)
+ key = key.to_sym
+ item = Item.new(key, **options)
- # Configure navigation for a specific context
- def configure(context = :sidebar, &block)
- reg = registry(context)
- builder = Builder.new(reg)
+ @items[key] = item
+
+ # If block provided, it's for children
+ if block_given?
+ builder = Builder.new(self, item)
# Support both block styles: |nav| nav.add or just add
- if block_given?
- if block.arity > 0
- # Block expects parameter: do |nav| nav.add ... end
- block.call(builder)
- else
- # Block uses implicit self: do add ... end
- builder.instance_eval(&block)
- end
+ if block.arity > 0
+ # Block expects parameter: do |nav| nav.add ... end
+ block.call(builder)
+ else
+ # Block uses implicit self: do add ... end
+ builder.instance_eval(&block)
end
- reg
end
- # Get a registry for method chaining
- def for(context)
- registry(context)
- end
+ sort_items!
+ item
+ end
- # Delegate common methods to the default (sidebar) registry
- def add(key, **options, &block)
- registry(:sidebar).add(key, **options, &block)
- end
+ # Remove a navigation item
+ def remove(key)
+ key = key.to_sym
+ removed = @items.delete(key)
- def remove(key)
- registry(:sidebar).remove(key)
+ # Also remove from any parent's children
+ @items.each_value do |item|
+ item.remove_child(key)
end
- def update(key, **options)
- registry(:sidebar).update(key, **options)
- end
+ removed
+ end
- def find(key)
- registry(:sidebar).find(key)
- end
+ # Update an existing navigation item
+ def update(key, **options)
+ key = key.to_sym
+ item = @items[key]
- def exists?(key)
- registry(:sidebar).exists?(key)
- end
+ return nil unless item
- def insert_before(target_key, new_key, **options)
- registry(:sidebar).insert_before(target_key, new_key, **options)
+ options.each do |attr, value|
+ item.send("#{attr}=", value) if item.respond_to?("#{attr}=")
end
- def insert_after(target_key, new_key, **options)
- registry(:sidebar).insert_after(target_key, new_key, **options)
- end
+ sort_items!
+ item
+ end
- def move(key, **position_options)
- registry(:sidebar).move(key, **position_options)
- end
+ # Find a navigation item
+ def find(key)
+ @items[key.to_sym]
+ end
- def replace(key, **options, &block)
- registry(:sidebar).replace(key, **options, &block)
- end
+ # Check if item exists
+ def exists?(key)
+ @items.key?(key.to_sym)
+ end
- def visible_items(context = :sidebar, user = nil)
- registry(context).visible_items(user)
- end
+ # Insert item before another item
+ def insert_before(target_key, new_key, **options)
+ target = find(target_key)
+ return nil unless target
- def root_items(context = :sidebar)
- registry(context).root_items
- end
+ new_position = target.position - 1
+ add(new_key, **options.merge(position: new_position))
+ end
- def breadcrumbs_for(current_path, context = :sidebar, view_context = nil)
- registry(context).breadcrumbs_for(current_path, view_context)
- end
+ # Insert item after another item
+ def insert_after(target_key, new_key, **options)
+ target = find(target_key)
+ return nil unless target
+
+ new_position = target.position + 1
+ add(new_key, **options.merge(position: new_position))
+ end
+
+ # Move item to a new position
+ def move(key, position: nil, before: nil, after: nil)
+ item = find(key)
+ return nil unless item
+
+ if before
+ target = find(before)
+ item.position = target.position - 1 if target
+ elsif after
+ target = find(after)
+ item.position = target.position + 1 if target
+ elsif position == :first
+ item.position = -999
+ elsif position == :last
+ item.position = 999
+ elsif position.is_a?(Integer)
+ item.position = position
+ end
+
+ sort_items!
+ item
+ end
+
+ # Replace an item
+ def replace(key, **options, &block)
+ remove(key)
+ add(key, **options, &block)
+ end
- def find_active_item(current_path, context = :sidebar, view_context = nil)
- registry(context).find_active_item(current_path, view_context)
+ # Get all root items (items without a parent)
+ def root_items
+ @items.values.select { |item| item.parent_key.nil? }.sort_by { |item| [item.position, item.key.to_s] }
+ end
+
+ # Get all items that are visible to the user
+ def visible_items(user = nil, parent_key = nil)
+ items_to_filter = if parent_key
+ find(parent_key)&.children || []
+ else
+ root_items
+ end
+
+ items_to_filter.select { |item| item.visible?(user) }
+ end
+
+ # Build tree structure
+ def build_tree
+ # First, clear all children
+ @items.each_value { |item| item.children.clear }
+
+ # Then rebuild the tree
+ @items.each_value do |item|
+ if item.parent_key && (parent = @items[item.parent_key])
+ parent.add_child(item)
+ end
end
- # Get all available contexts
- def contexts
- registries.keys
+ root_items
+ end
+
+ # Get breadcrumb trail for a path
+ def breadcrumbs_for(current_path, context = nil)
+ breadcrumbs = []
+
+ # Find active item
+ active_item = find_active_item(current_path, context)
+
+ # Build breadcrumb trail
+ while active_item
+ breadcrumbs.unshift(active_item)
+ active_item = active_item.parent_key ? find(active_item.parent_key) : nil
end
- # Clear all registries (useful for testing)
- def clear_all!
- @registries = {}
+ breadcrumbs
+ end
+
+ # Find the active item for current path
+ def find_active_item(current_path, context = nil)
+ # Check all items, deepest match wins
+ active_items = @items.values.select { |item| item.active?(current_path, context) }
+
+ # Return the one with the longest URL match
+ active_items.max_by { |item| item.resolve_url(context)&.length || 0 }
+ end
+
+ # Clear all items
+ def clear
+ @items.clear
+ end
+
+ # Get all registered paths (for settings_area? detection)
+ def registered_paths(context = nil)
+ @items.values.map { |item| item.resolve_url(context) }.compact
+ end
+
+ # Add a section
+ def section(key, label: nil, &block)
+ # Create a section header item
+ section_item = add(key, section_label: label || key.to_s.humanize, position: @items.size * 100)
+
+ if block_given?
+ builder = Builder.new(self, section_item)
+ builder.instance_eval(&block)
end
- # Clear a specific context
- def clear!(context = :sidebar)
- registry(context).clear
+ section_item
+ end
+
+ # Deep clone the registry
+ def deep_clone
+ cloned = self.class.new(context)
+ @items.each do |key, item|
+ cloned.items[key] = item.deep_clone
end
+ cloned.build_tree
+ cloned
+ end
+
+ private
+
+ def sort_items!
+ # Sort items by position, then rebuild tree
+ @items = @items.sort_by { |_key, item| [item.position, item.key.to_s] }.to_h
+ build_tree
end
end
end
end
-
-# Require the sub-classes
-require_relative 'navigation/item'
-require_relative 'navigation/registry'
-require_relative 'navigation/builder'
diff --git a/admin/app/models/spree/admin/navigation/registry.rb b/admin/app/models/spree/admin/navigation/registry.rb
deleted file mode 100644
index afcdd089c2..0000000000
--- a/admin/app/models/spree/admin/navigation/registry.rb
+++ /dev/null
@@ -1,220 +0,0 @@
-module Spree
- module Admin
- class Navigation
- class Registry
- attr_reader :items, :context
-
- def initialize(context)
- @context = context
- @items = {}
- end
-
- # Add a navigation item
- def add(key, **options, &block)
- key = key.to_sym
- item = Item.new(key, **options)
-
- @items[key] = item
-
- # If block provided, it's for children
- if block_given?
- builder = Builder.new(self, item)
- # Support both block styles: |nav| nav.add or just add
- if block.arity > 0
- # Block expects parameter: do |nav| nav.add ... end
- block.call(builder)
- else
- # Block uses implicit self: do add ... end
- builder.instance_eval(&block)
- end
- end
-
- sort_items!
- item
- end
-
- # Remove a navigation item
- def remove(key)
- key = key.to_sym
- removed = @items.delete(key)
-
- # Also remove from any parent's children
- @items.each_value do |item|
- item.remove_child(key)
- end
-
- removed
- end
-
- # Update an existing navigation item
- def update(key, **options)
- key = key.to_sym
- item = @items[key]
-
- return nil unless item
-
- options.each do |attr, value|
- item.send("#{attr}=", value) if item.respond_to?("#{attr}=")
- end
-
- sort_items!
- item
- end
-
- # Find a navigation item
- def find(key)
- @items[key.to_sym]
- end
-
- # Check if item exists
- def exists?(key)
- @items.key?(key.to_sym)
- end
-
- # Insert item before another item
- def insert_before(target_key, new_key, **options)
- target = find(target_key)
- return nil unless target
-
- new_position = target.position - 1
- add(new_key, **options.merge(position: new_position))
- end
-
- # Insert item after another item
- def insert_after(target_key, new_key, **options)
- target = find(target_key)
- return nil unless target
-
- new_position = target.position + 1
- add(new_key, **options.merge(position: new_position))
- end
-
- # Move item to a new position
- def move(key, position: nil, before: nil, after: nil)
- item = find(key)
- return nil unless item
-
- if before
- target = find(before)
- item.position = target.position - 1 if target
- elsif after
- target = find(after)
- item.position = target.position + 1 if target
- elsif position == :first
- item.position = -999
- elsif position == :last
- item.position = 999
- elsif position.is_a?(Integer)
- item.position = position
- end
-
- sort_items!
- item
- end
-
- # Replace an item
- def replace(key, **options, &block)
- remove(key)
- add(key, **options, &block)
- end
-
- # Get all root items (items without a parent)
- def root_items
- @items.values.select { |item| item.parent_key.nil? }.sort_by { |item| [item.position, item.key.to_s] }
- end
-
- # Get all items that are visible to the user
- def visible_items(user = nil, parent_key = nil)
- items_to_filter = if parent_key
- find(parent_key)&.children || []
- else
- root_items
- end
-
- items_to_filter.select { |item| item.visible?(user) }
- end
-
- # Build tree structure
- def build_tree
- # First, clear all children
- @items.each_value { |item| item.children.clear }
-
- # Then rebuild the tree
- @items.each_value do |item|
- if item.parent_key && (parent = @items[item.parent_key])
- parent.add_child(item)
- end
- end
-
- root_items
- end
-
- # Get breadcrumb trail for a path
- def breadcrumbs_for(current_path, context = nil)
- breadcrumbs = []
-
- # Find active item
- active_item = find_active_item(current_path, context)
-
- # Build breadcrumb trail
- while active_item
- breadcrumbs.unshift(active_item)
- active_item = active_item.parent_key ? find(active_item.parent_key) : nil
- end
-
- breadcrumbs
- end
-
- # Find the active item for current path
- def find_active_item(current_path, context = nil)
- # Check all items, deepest match wins
- active_items = @items.values.select { |item| item.active?(current_path, context) }
-
- # Return the one with the longest URL match
- active_items.max_by { |item| item.resolve_url(context)&.length || 0 }
- end
-
- # Clear all items
- def clear
- @items.clear
- end
-
- # Get all registered paths (for settings_area? detection)
- def registered_paths(context = nil)
- @items.values.map { |item| item.resolve_url(context) }.compact
- end
-
- # Add a section
- def section(key, label: nil, &block)
- # Create a section header item
- section_item = add(key, section_label: label || key.to_s.humanize, position: @items.size * 100)
-
- if block_given?
- builder = Builder.new(self, section_item)
- builder.instance_eval(&block)
- end
-
- section_item
- end
-
- # Deep clone the registry
- def deep_clone
- cloned = self.class.new(context)
- @items.each do |key, item|
- cloned.items[key] = item.deep_clone
- end
- cloned.build_tree
- cloned
- end
-
- private
-
- def sort_items!
- # Sort items by position, then rebuild tree
- @items = @items.sort_by { |_key, item| [item.position, item.key.to_s] }.to_h
- build_tree
- end
- end
- end
- end
-end
diff --git a/admin/lib/spree/admin/navigation_config.rb b/admin/config/initializers/spree_admin_navigation.rb
similarity index 70%
rename from admin/lib/spree/admin/navigation_config.rb
rename to admin/config/initializers/spree_admin_navigation.rb
index 641099e68f..3ad9a1a37b 100644
--- a/admin/lib/spree/admin/navigation_config.rb
+++ b/admin/config/initializers/spree_admin_navigation.rb
@@ -1,9 +1,14 @@
# Default Admin Navigation Configuration
# This file defines the default sidebar and settings navigation for Spree Admin
-Spree::Admin::Navigation.configure(:sidebar) do |nav|
+Rails.application.config.after_initialize do
+ # ===============================================
+ # Sidebar Navigation
+ # ===============================================
+ sidebar_nav = Rails.application.config.spree_admin.navigation[:sidebar]
+
# Getting Started (onboarding)
- nav.add :getting_started,
+ sidebar_nav.add :getting_started,
label: 'admin.getting_started',
url: :admin_getting_started_path,
icon: 'map',
@@ -14,7 +19,7 @@ Spree::Admin::Navigation.configure(:sidebar) do |nav|
active: -> { controller_name == 'dashboard' && action_name == 'getting_started' }
# Dashboard / Home
- nav.add :home,
+ sidebar_nav.add :home,
label: :home,
url: :admin_path,
icon: 'home',
@@ -22,7 +27,7 @@ Spree::Admin::Navigation.configure(:sidebar) do |nav|
active: -> { controller_name == 'dashboard' && action_name == 'show' }
# Orders with submenu
- nav.add :orders,
+ sidebar_nav.add :orders,
label: :orders,
url: :admin_orders_path,
icon: 'inbox',
@@ -35,28 +40,28 @@ Spree::Admin::Navigation.configure(:sidebar) do |nav|
} do |orders|
# Orders to Fulfill submenu
orders.add :orders_to_fulfill,
- label: 'admin.orders.orders_to_fulfill',
- url: -> { spree.admin_orders_path(q: {shipment_state_not_in: [:shipped, :canceled]}) },
- position: 10,
- active: -> { controller_name == 'orders' && params.dig(:q, :shipment_state_not_in).present? },
- if: -> {
- ready_to_ship_orders_count&.positive?
- },
- badge: -> {
- ready_to_ship_orders_count if ready_to_ship_orders_count&.positive?
- }
+ label: 'admin.orders.orders_to_fulfill',
+ url: -> { spree.admin_orders_path(q: {shipment_state_not_in: [:shipped, :canceled]}) },
+ position: 10,
+ active: -> { controller_name == 'orders' && params.dig(:q, :shipment_state_not_in).present? },
+ if: -> {
+ ready_to_ship_orders_count&.positive?
+ },
+ badge: -> {
+ ready_to_ship_orders_count if ready_to_ship_orders_count&.positive?
+ }
# Draft Orders
orders.add :draft_orders,
- label: :draft_orders,
- url: :admin_checkouts_path,
- position: 20,
- active: -> { controller_name == 'checkouts' || (@order.present? && [email protected]?) },
- if: -> { can?(:manage, :checkouts) }
+ label: :draft_orders,
+ url: :admin_checkouts_path,
+ position: 20,
+ active: -> { controller_name == 'checkouts' || (@order.present? && [email protected]?) },
+ if: -> { can?(:manage, :checkouts) }
end
# Returns with submenu
- nav.add :returns,
+ sidebar_nav.add :returns,
label: :returns,
url: :admin_customer_returns_path,
icon: 'receipt-refund',
@@ -72,7 +77,7 @@ Spree::Admin::Navigation.configure(:sidebar) do |nav|
end
# Products with submenu
- nav.add :products,
+ sidebar_nav.add :products,
label: :products,
url: :admin_products_path,
icon: 'package',
@@ -81,38 +86,38 @@ Spree::Admin::Navigation.configure(:sidebar) do |nav|
if: -> { can?(:manage, Spree::Product) } do |products|
# Stock
products.add :stock,
- label: :stock,
- url: :admin_stock_items_path,
- position: 10,
- active: -> { %w[stock_items stock_transfers].include?(controller_name) },
- if: -> { can?(:manage, Spree::StockItem) || can?(:manage, Spree::StockTransfer) }
+ label: :stock,
+ url: :admin_stock_items_path,
+ position: 10,
+ active: -> { %w[stock_items stock_transfers].include?(controller_name) },
+ if: -> { can?(:manage, Spree::StockItem) || can?(:manage, Spree::StockTransfer) }
# Taxonomies
products.add :taxonomies,
- label: :taxonomies,
- url: :admin_taxonomies_path,
- position: 20,
- active: -> { %w[taxonomies taxons].include?(controller_name) },
- if: -> { can?(:manage, Spree::Taxonomy) && can?(:manage, Spree::Taxon) }
+ label: :taxonomies,
+ url: :admin_taxonomies_path,
+ position: 20,
+ active: -> { %w[taxonomies taxons].include?(controller_name) },
+ if: -> { can?(:manage, Spree::Taxonomy) && can?(:manage, Spree::Taxon) }
# Options
products.add :options,
- label: :options,
- url: :admin_option_types_path,
- position: 30,
- active: -> { %w[option_types option_values].include?(controller_name) },
- if: -> { can?(:manage, Spree::OptionType) }
+ label: :options,
+ url: :admin_option_types_path,
+ position: 30,
+ active: -> { %w[option_types option_values].include?(controller_name) },
+ if: -> { can?(:manage, Spree::OptionType) }
# Properties
products.add :properties,
- label: :properties,
- url: :admin_properties_path,
- position: 40,
- if: -> { can?(:manage, Spree::Property) && Spree::Config.product_properties_enabled }
+ label: :properties,
+ url: :admin_properties_path,
+ position: 40,
+ if: -> { can?(:manage, Spree::Property) && Spree::Config.product_properties_enabled }
end
# Vendors (Enterprise Edition)
- nav.add :vendors,
+ sidebar_nav.add :vendors,
label: :vendors,
url: 'https://spreecommerce.org/marketplace-ecommerce/',
icon: 'heart-handshake',
@@ -123,7 +128,7 @@ Spree::Admin::Navigation.configure(:sidebar) do |nav|
target: '_blank'
# Customers with submenu
- nav.add :customers,
+ sidebar_nav.add :customers,
label: :customers,
url: :admin_users_path,
icon: 'users',
@@ -138,7 +143,7 @@ Spree::Admin::Navigation.configure(:sidebar) do |nav|
end
# Promotions with submenu
- nav.add :promotions,
+ sidebar_nav.add :promotions,
label: :promotions,
url: :admin_promotions_path,
icon: 'discount',
@@ -147,14 +152,14 @@ Spree::Admin::Navigation.configure(:sidebar) do |nav|
if: -> { can?(:manage, Spree::Promotion) } do |promotions|
# Gift Cards
promotions.add :gift_cards,
- label: :gift_cards,
- url: :admin_gift_cards_path,
- position: 10,
- active: -> { %w[gift_cards gift_card_batches].include?(controller_name) }
+ label: :gift_cards,
+ url: :admin_gift_cards_path,
+ position: 10,
+ active: -> { %w[gift_cards gift_card_batches].include?(controller_name) }
end
# Reports
- nav.add :reports,
+ sidebar_nav.add :reports,
label: :reports,
url: :admin_reports_path,
icon: 'chart-bar',
@@ -162,7 +167,7 @@ Spree::Admin::Navigation.configure(:sidebar) do |nav|
if: -> { can?(:manage, Spree::Report) }
# Storefront with submenu
- nav.add :storefront,
+ sidebar_nav.add :storefront,
label: 'admin.storefront',
url: :admin_themes_path,
icon: 'building-store',
@@ -171,36 +176,36 @@ Spree::Admin::Navigation.configure(:sidebar) do |nav|
if: -> { can?(:manage, Spree::Theme) } do |storefront|
# Themes
storefront.add :themes,
- label: :themes,
- url: :admin_themes_path,
- position: 10,
- if: -> { can?(:manage, Spree::Theme) }
+ label: :themes,
+ url: :admin_themes_path,
+ position: 10,
+ if: -> { can?(:manage, Spree::Theme) }
# Pages
storefront.add :pages,
- label: :pages,
- url: :admin_pages_path,
- position: 20,
- if: -> { can?(:manage, Spree::Page) }
+ label: :pages,
+ url: :admin_pages_path,
+ position: 20,
+ if: -> { can?(:manage, Spree::Page) }
# Posts (Blog)
storefront.add :posts,
- label: :posts,
- url: :admin_posts_path,
- position: 30,
- active: -> { %w[posts post_categories].include?(controller_name) },
- if: -> { can?(:manage, Spree::Post) }
+ label: :posts,
+ url: :admin_posts_path,
+ position: 30,
+ active: -> { %w[posts post_categories].include?(controller_name) },
+ if: -> { can?(:manage, Spree::Post) }
# Storefront Settings
storefront.add :storefront_settings,
- label: :settings,
- url: :edit_admin_storefront_path,
- position: 40,
- if: -> { can?(:manage, current_store) }
+ label: :settings,
+ url: :edit_admin_storefront_path,
+ position: 40,
+ if: -> { can?(:manage, current_store) }
end
# Integrations
- nav.add :integrations,
+ sidebar_nav.add :integrations,
label: :integrations,
url: :admin_integrations_path,
icon: 'plug-connected',
@@ -208,7 +213,7 @@ Spree::Admin::Navigation.configure(:sidebar) do |nav|
if: -> { can?(:manage, Spree::Integration) }
# Settings (bottom of sidebar)
- nav.add :settings,
+ sidebar_nav.add :settings,
label: :settings,
url: -> { spree.edit_admin_store_path(section: 'general-settings') },
icon: 'settings',
@@ -216,18 +221,20 @@ Spree::Admin::Navigation.configure(:sidebar) do |nav|
if: -> { can?(:manage, current_store) }
# Admin Users (bottom of sidebar)
- nav.add :admin_users,
+ sidebar_nav.add :admin_users,
label: :users,
url: :admin_admin_users_path,
icon: 'users',
position: 110,
if: -> { can?(:manage, Spree.admin_user_class) }
-end
-# Settings Navigation
-Spree::Admin::Navigation.configure(:settings) do |nav|
+ # ===============================================
+ # Settings Navigation
+ # ===============================================
+ settings_nav = Rails.application.config.spree_admin.navigation[:settings]
+
# Store Details
- nav.add :general_settings,
+ settings_nav.add :general_settings,
label: :store_details,
url: -> { spree.edit_admin_store_path(section: 'general-settings') },
icon: 'building-store',
@@ -236,7 +243,7 @@ Spree::Admin::Navigation.configure(:settings) do |nav|
if: -> { can?(:manage, current_store) }
# Admin Users
- nav.add :users,
+ settings_nav.add :users,
label: :users,
url: :admin_admin_users_path,
icon: 'users',
@@ -245,7 +252,7 @@ Spree::Admin::Navigation.configure(:settings) do |nav|
if: -> { can?(:manage, Spree.admin_user_class) }
# Emails
- nav.add :emails,
+ settings_nav.add :emails,
label: :emails,
url: -> { spree.edit_admin_store_path(section: 'emails') },
icon: 'send',
@@ -254,7 +261,7 @@ Spree::Admin::Navigation.configure(:settings) do |nav|
if: -> { can?(:manage, current_store) }
# Policies
- nav.add :policies,
+ settings_nav.add :policies,
label: :policies,
url: :admin_policies_path,
icon: 'list-check',
@@ -263,7 +270,7 @@ Spree::Admin::Navigation.configure(:settings) do |nav|
if: -> { can?(:manage, Spree::Policy) }
# Checkout
- nav.add :checkout,
+ settings_nav.add :checkout,
label: :checkout,
url: -> { spree.edit_admin_store_path(section: 'checkout') },
icon: 'shopping-cart',
@@ -272,7 +279,7 @@ Spree::Admin::Navigation.configure(:settings) do |nav|
if: -> { can?(:manage, current_store) }
# Domains
- nav.add :domains,
+ settings_nav.add :domains,
label: :domains,
url: :admin_custom_domains_path,
icon: 'world-www',
@@ -281,7 +288,7 @@ Spree::Admin::Navigation.configure(:settings) do |nav|
if: -> { can?(:manage, Spree::CustomDomain) }
# Payment Methods
- nav.add :payment_methods,
+ settings_nav.add :payment_methods,
label: :payments,
url: :admin_payment_methods_path,
icon: 'credit-card',
@@ -290,7 +297,7 @@ Spree::Admin::Navigation.configure(:settings) do |nav|
if: -> { can?(:manage, Spree::PaymentMethod) }
# Zones
- nav.add :zones,
+ settings_nav.add :zones,
label: :zones,
url: :admin_zones_path,
icon: 'world',
@@ -299,7 +306,7 @@ Spree::Admin::Navigation.configure(:settings) do |nav|
if: -> { can?(:manage, Spree::Zone) }
# Shipping Methods
- nav.add :shipping_methods,
+ settings_nav.add :shipping_methods,
label: :shipping,
url: :admin_shipping_methods_path,
icon: 'truck',
@@ -308,7 +315,7 @@ Spree::Admin::Navigation.configure(:settings) do |nav|
if: -> { can?(:manage, Spree::ShippingMethod) }
# Tax Settings
- nav.add :tax_rates,
+ settings_nav.add :tax_rates,
label: :tax,
url: :admin_tax_rates_path,
icon: 'receipt-tax',
@@ -317,7 +324,7 @@ Spree::Admin::Navigation.configure(:settings) do |nav|
if: -> { can?(:manage, Spree::TaxRate) }
# Returns
- nav.add :return_settings,
+ settings_nav.add :return_settings,
label: :returns,
url: :admin_return_authorization_reasons_path,
icon: 'receipt-refund',
@@ -326,7 +333,7 @@ Spree::Admin::Navigation.configure(:settings) do |nav|
if: -> { can?(:manage, Spree::ReturnAuthorizationReason) }
# Stock Locations
- nav.add :stock_locations,
+ settings_nav.add :stock_locations,
label: :stock_locations,
url: :admin_stock_locations_path,
icon: 'map-pin',
@@ -335,7 +342,7 @@ Spree::Admin::Navigation.configure(:settings) do |nav|
if: -> { can?(:manage, Spree::StockLocation) }
# Metafield Definitions
- nav.add :metafield_definitions,
+ settings_nav.add :metafield_definitions,
label: :metafield_definitions,
url: :admin_metafield_definitions_path,
icon: 'list-details',
@@ -344,7 +351,7 @@ Spree::Admin::Navigation.configure(:settings) do |nav|
if: -> { can?(:manage, Spree::MetafieldDefinition) }
# Audit Log
- nav.add :audits,
+ settings_nav.add :audits,
label: 'admin.audit_log',
url: :admin_audits_path,
icon: 'history',
@@ -357,7 +364,7 @@ Spree::Admin::Navigation.configure(:settings) do |nav|
}
# Developers
- nav.add :developers,
+ settings_nav.add :developers,
label: :developers,
url: :admin_oauth_applications_path,
icon: 'terminal',
@@ -366,142 +373,142 @@ Spree::Admin::Navigation.configure(:settings) do |nav|
if: -> { can?(:manage, Spree::OauthApplication) }
# Edit Profile
- nav.add :edit_profile,
+ settings_nav.add :edit_profile,
label: 'admin.edit_profile',
url: :edit_admin_profile_path,
icon: 'user-scan',
position: 200,
active: -> { controller_name == 'profiles' && action_name == 'edit' }
-end
-# ===============================================
-# Page Tab Navigations
-# ===============================================
+ # ===============================================
+ # Page Tab Navigations
+ # ===============================================
-# Tax Tab Navigation
-Spree::Admin::Navigation.configure(:tax_tabs) do |nav|
- nav.add :tax_rates,
+ # Tax Tab Navigation
+ tax_tabs_nav = Rails.application.config.spree_admin.navigation[:tax_tabs]
+
+ tax_tabs_nav.add :tax_rates,
label: :tax_rates,
url: :admin_tax_rates_path,
position: 10,
if: -> { can?(:manage, Spree::TaxRate) }
- nav.add :tax_categories,
+ tax_tabs_nav.add :tax_categories,
label: :tax_categories,
url: :admin_tax_categories_path,
position: 20,
if: -> { can?(:manage, Spree::TaxCategory) }
-end
-# Shipping Tab Navigation
-Spree::Admin::Navigation.configure(:shipping_tabs) do |nav|
- nav.add :shipping_methods,
+ # Shipping Tab Navigation
+ shipping_tabs_nav = Rails.application.config.spree_admin.navigation[:shipping_tabs]
+
+ shipping_tabs_nav.add :shipping_methods,
label: :shipping_methods,
url: :admin_shipping_methods_path,
position: 10,
active: -> { controller_name == 'shipping_methods' && action_name == 'index' },
if: -> { can?(:manage, Spree::ShippingMethod) }
- nav.add :shipping_categories,
+ shipping_tabs_nav.add :shipping_categories,
label: :shipping_categories,
url: :admin_shipping_categories_path,
position: 20,
if: -> { can?(:manage, Spree::ShippingCategory) }
-end
-# Team Tab Navigation
-Spree::Admin::Navigation.configure(:team_tabs) do |nav|
- nav.add :admin_users,
+ # Team Tab Navigation
+ team_tabs_nav = Rails.application.config.spree_admin.navigation[:team_tabs]
+
+ team_tabs_nav.add :admin_users,
label: :users,
url: :admin_admin_users_path,
position: 10,
if: -> { can?(:manage, Spree.admin_user_class) }
- nav.add :invitations,
+ team_tabs_nav.add :invitations,
label: :invitations,
url: :admin_invitations_path,
position: 20,
if: -> { can?(:manage, Spree::Invitation) }
- nav.add :roles,
+ team_tabs_nav.add :roles,
label: :roles,
url: :admin_roles_path,
position: 30,
if: -> { can?(:manage, Spree::Role) }
-end
-# Stock Tab Navigation
-Spree::Admin::Navigation.configure(:stock_tabs) do |nav|
- nav.add :stock_items,
+ # Stock Tab Navigation
+ stock_tabs_nav = Rails.application.config.spree_admin.navigation[:stock_tabs]
+
+ stock_tabs_nav.add :stock_items,
label: :stock_items,
url: :admin_stock_items_path,
position: 10,
active: -> { controller_name == 'stock_items' },
if: -> { can?(:manage, Spree::StockItem) }
- nav.add :stock_transfers,
+ stock_tabs_nav.add :stock_transfers,
label: :stock_transfers,
url: :admin_stock_transfers_path,
position: 20,
active: -> { controller_name == 'stock_transfers' },
if: -> { can?(:manage, Spree::StockTransfer) }
-end
-# Returns and Refunds Tab Navigation
-Spree::Admin::Navigation.configure(:returns_tabs) do |nav|
- nav.add :return_authorization_reasons,
+ # Returns and Refunds Tab Navigation
+ returns_tabs_nav = Rails.application.config.spree_admin.navigation[:returns_tabs]
+
+ returns_tabs_nav.add :return_authorization_reasons,
label: :return_authorization_reasons,
url: :admin_return_authorization_reasons_path,
position: 10,
if: -> { can?(:manage, Spree::ReturnAuthorizationReason) }
- nav.add :refund_reasons,
+ returns_tabs_nav.add :refund_reasons,
label: :refund_reasons,
url: :admin_refund_reasons_path,
position: 20,
if: -> { can?(:manage, Spree::RefundReason) }
- nav.add :reimbursement_types,
+ returns_tabs_nav.add :reimbursement_types,
label: :reimbursement_types,
url: :admin_reimbursement_types_path,
position: 30,
if: -> { can?(:manage, Spree::ReimbursementType) }
-end
-# Developers Tab Navigation
-Spree::Admin::Navigation.configure(:developers_tabs) do |nav|
- nav.add :api_keys,
+ # Developers Tab Navigation
+ developers_tabs_nav = Rails.application.config.spree_admin.navigation[:developers_tabs]
+
+ developers_tabs_nav.add :api_keys,
label: :api_keys,
url: :admin_oauth_applications_path,
position: 10,
active: -> { controller_name == 'oauth_applications' },
if: -> { can?(:manage, Spree::OauthApplication) }
- nav.add :webhooks,
+ developers_tabs_nav.add :webhooks,
label: :webhooks,
url: :admin_webhooks_subscribers_path,
position: 20,
active: -> { controller_name == 'webhooks_subscribers' },
if: -> { can?(:manage, Spree::Webhooks::Subscriber) }
-end
-# Audit Tab Navigation
-Spree::Admin::Navigation.configure(:audit_tabs) do |nav|
- nav.add :audit_log,
+ # Audit Tab Navigation
+ audit_tabs_nav = Rails.application.config.spree_admin.navigation[:audit_tabs]
+
+ audit_tabs_nav.add :audit_log,
label: 'admin.audit_log',
url: :admin_audits_path,
position: 10,
active: -> { controller_name == 'audits' },
if: -> { can?(:manage, current_store) }
- nav.add :exports,
+ audit_tabs_nav.add :exports,
label: :exports,
url: :admin_exports_path,
position: 20,
active: -> { controller_name == 'exports' },
if: -> { can?(:manage, Spree::Export) }
- nav.add :imports,
+ audit_tabs_nav.add :imports,
label: :imports,
url: :admin_imports_path,
position: 30,
diff --git a/admin/lib/spree/admin/engine.rb b/admin/lib/spree/admin/engine.rb
index 50f2ecef5d..af817186ed 100644
--- a/admin/lib/spree/admin/engine.rb
+++ b/admin/lib/spree/admin/engine.rb
@@ -125,7 +125,20 @@ module Spree
:webhooks_subscribers_header_partials,
:vendors_nav_partials,
:zones_actions_partials,
- :zones_header_partials
+ :zones_header_partials,
+ :navigation
+ )
+
+ NavigationEnvironment = Struct.new(
+ :sidebar,
+ :settings,
+ :tax_tabs,
+ :shipping_tabs,
+ :team_tabs,
+ :stock_tabs,
+ :returns_tabs,
+ :developers_tabs,
+ :audit_tabs
)
# accessible via Rails.application.config.spree_admin
@@ -182,17 +195,23 @@ module Spree
end
end
- config.to_prepare do
+ config.after_initialize do |app|
Environment.new.tap do |env|
env.members.each do |key|
Rails.application.config.spree_admin.send("#{key}=", [])
end
end
- # Load default navigation configuration
- # This needs to run on every code reload in development to repopulate
- # the navigation registries after the Navigation class is reloaded
- load File.expand_path('navigation_config.rb', __dir__)
+ app.config.spree_admin.navigation = NavigationEnvironment.new
+ app.config.spree_admin.navigation.sidebar = Spree::Admin::Navigation.new(:sidebar)
+ app.config.spree_admin.navigation.settings = Spree::Admin::Navigation.new(:settings)
+ app.config.spree_admin.navigation.tax_tabs = Spree::Admin::Navigation.new(:tax_tabs)
+ app.config.spree_admin.navigation.shipping_tabs = Spree::Admin::Navigation.new(:shipping_tabs)
+ app.config.spree_admin.navigation.team_tabs = Spree::Admin::Navigation.new(:team_tabs)
+ app.config.spree_admin.navigation.stock_tabs = Spree::Admin::Navigation.new(:stock_tabs)
+ app.config.spree_admin.navigation.returns_tabs = Spree::Admin::Navigation.new(:returns_tabs)
+ app.config.spree_admin.navigation.developers_tabs = Spree::Admin::Navigation.new(:developers_tabs)
+ app.config.spree_admin.navigation.audit_tabs = Spree::Admin::Navigation.new(:audit_tabs)
end
end
end
diff --git a/admin/spec/helpers/spree/admin/navigation_helper_spec.rb b/admin/spec/helpers/spree/admin/navigation_helper_spec.rb
index 489027d113..7e4d2f9efd 100644
--- a/admin/spec/helpers/spree/admin/navigation_helper_spec.rb
+++ b/admin/spec/helpers/spree/admin/navigation_helper_spec.rb
@@ -119,8 +119,10 @@ describe Spree::Admin::NavigationHelper, type: :helper do
end
describe '#render_navigation' do
+ let(:nav) { Rails.application.config.spree_admin.navigation.sidebar }
+
before do
- Spree::Admin::Navigation.clear_all!
+ nav.clear
allow(Spree::Admin::RuntimeConfig).to receive(:legacy_sidebar_navigation).and_return(false)
end
@@ -131,17 +133,13 @@ describe Spree::Admin::NavigationHelper, type: :helper do
end
it 'returns empty string when no items are visible' do
- Spree::Admin::Navigation.configure(:sidebar) do |nav|
- nav.add :dashboard, label: 'Dashboard', if: -> { false }
- end
+ nav.add :dashboard, label: 'Dashboard', if: -> { false }
expect(helper.render_navigation(:sidebar)).to eq('')
end
it 'renders navigation partial when items are visible' do
- Spree::Admin::Navigation.configure(:sidebar) do |nav|
- nav.add :dashboard, label: 'Dashboard', url: '/admin'
- end
+ nav.add :dashboard, label: 'Dashboard', url: '/admin'
allow(helper).to receive(:render).and_return('navigation html')
@@ -152,15 +150,15 @@ describe Spree::Admin::NavigationHelper, type: :helper do
end
describe '#navigation_items' do
+ let(:nav) { Rails.application.config.spree_admin.navigation.sidebar }
+
before do
- Spree::Admin::Navigation.clear_all!
+ nav.clear
end
it 'returns visible items for the given context' do
- Spree::Admin::Navigation.configure(:sidebar) do |nav|
- nav.add :dashboard, label: 'Dashboard', url: '/admin'
- nav.add :products, label: 'Products', url: '/admin/products'
- end
+ nav.add :dashboard, label: 'Dashboard', url: '/admin'
+ nav.add :products, label: 'Products', url: '/admin/products'
items = helper.navigation_items(:sidebar)
@@ -171,84 +169,12 @@ describe Spree::Admin::NavigationHelper, type: :helper do
it 'passes view context for permission checking' do
allow(helper).to receive(:can?).with(:manage, Spree::Product).and_return(false)
- Spree::Admin::Navigation.configure(:sidebar) do |nav|
- nav.add :dashboard, label: 'Dashboard'
- nav.add :products, label: 'Products', if: -> { can?(:manage, Spree::Product) }
- end
+ nav.add :dashboard, label: 'Dashboard'
+ nav.add :products, label: 'Products', if: -> { can?(:manage, Spree::Product) }
items = helper.navigation_items(:sidebar)
expect(items.map(&:key)).to eq([:dashboard])
end
end
-
- describe '#current_navigation_context' do
- it 'returns :settings when in settings area' do
- allow(helper).to receive(:settings_area?).and_return(true)
-
- expect(helper.current_navigation_context).to eq(:settings)
- end
-
- it 'returns :sidebar when not in settings area' do
- allow(helper).to receive(:settings_area?).and_return(false)
-
- expect(helper.current_navigation_context).to eq(:sidebar)
- end
- end
-
- describe '#navigation_item_active?' do
- before do
- Spree::Admin::Navigation.clear_all!
- allow(helper).to receive(:request).and_return(double(path: '/admin/products'))
- end
-
- it 'returns true when item is active' do
- Spree::Admin::Navigation.configure(:sidebar) do |nav|
- nav.add :products, label: 'Products', url: '/admin/products'
- end
-
- expect(helper.navigation_item_active?(:products, :sidebar)).to be true
- end
-
- it 'returns false when item is not active' do
- Spree::Admin::Navigation.configure(:sidebar) do |nav|
- nav.add :dashboard, label: 'Dashboard', url: '/admin/orders'
- end
-
- expect(helper.navigation_item_active?(:dashboard, :sidebar)).to be false
- end
-
- it 'returns nil when item does not exist' do
- expect(helper.navigation_item_active?(:nonexistent, :sidebar)).to be_nil
- end
- end
-
- describe '#active_navigation_item' do
- before do
- Spree::Admin::Navigation.clear_all!
- allow(helper).to receive(:request).and_return(double(path: '/admin/products'))
- allow(helper).to receive(:settings_area?).and_return(false)
- end
-
- it 'returns the active navigation item for current path' do
- Spree::Admin::Navigation.configure(:sidebar) do |nav|
- nav.add :dashboard, label: 'Dashboard', url: '/admin/orders'
- nav.add :products, label: 'Products', url: '/admin/products'
- end
-
- active_item = helper.active_navigation_item
-
- expect(active_item.key).to eq(:products)
- end
-
- it 'returns nil when no item is active' do
- Spree::Admin::Navigation.configure(:sidebar) do |nav|
- nav.add :dashboard, label: 'Dashboard', url: '/admin/orders'
- end
-
- active_item = helper.active_navigation_item
-
- expect(active_item).to be_nil
- end
- end
end
diff --git a/admin/spec/models/spree/admin/navigation/registry_spec.rb b/admin/spec/models/spree/admin/navigation/registry_spec.rb
deleted file mode 100644
index 6cb869c24d..0000000000
--- a/admin/spec/models/spree/admin/navigation/registry_spec.rb
+++ /dev/null
@@ -1,229 +0,0 @@
-require 'spec_helper'
-
-RSpec.describe Spree::Admin::Navigation::Registry do
- let(:registry) { described_class.new(:sidebar) }
-
- describe '#add' do
- it 'adds a navigation item' do
- item = registry.add(:dashboard, label: 'Dashboard', url: '/admin')
-
- expect(registry.find(:dashboard)).to eq(item)
- end
-
- it 'supports block for nested items' do
- registry.add(:products, label: 'Products') do |nav|
- nav.add(:all_products, label: 'All Products')
- end
-
- parent = registry.find(:products)
- expect(parent.children.size).to eq(1)
- expect(parent.children.first.key).to eq(:all_products)
- end
- end
-
- describe '#remove' do
- it 'removes a navigation item' do
- registry.add(:dashboard, label: 'Dashboard')
- registry.remove(:dashboard)
-
- expect(registry.find(:dashboard)).to be_nil
- end
-
- it 'removes item from parent children' do
- registry.add(:products, label: 'Products') do |nav|
- nav.add(:all_products, label: 'All Products')
- end
-
- registry.remove(:all_products)
-
- parent = registry.find(:products)
- expect(parent.children).to be_empty
- end
- end
-
- describe '#update' do
- it 'updates an existing item' do
- registry.add(:dashboard, label: 'Dashboard', icon: 'home')
- registry.update(:dashboard, label: 'Home', icon: 'house')
-
- item = registry.find(:dashboard)
- expect(item.label).to eq('Home')
- expect(item.icon).to eq('house')
- end
-
- it 'returns nil for non-existent item' do
- result = registry.update(:nonexistent, label: 'Test')
-
- expect(result).to be_nil
- end
- end
-
- describe '#find' do
- it 'finds an item by key' do
- item = registry.add(:dashboard, label: 'Dashboard')
-
- expect(registry.find(:dashboard)).to eq(item)
- end
-
- it 'returns nil for non-existent item' do
- expect(registry.find(:nonexistent)).to be_nil
- end
- end
-
- describe '#exists?' do
- it 'returns true when item exists' do
- registry.add(:dashboard, label: 'Dashboard')
-
- expect(registry.exists?(:dashboard)).to be true
- end
-
- it 'returns false when item does not exist' do
- expect(registry.exists?(:nonexistent)).to be false
- end
- end
-
- describe '#insert_before' do
- it 'inserts item before target' do
- registry.add(:products, label: 'Products', position: 20)
- registry.insert_before(:products, :orders, label: 'Orders')
-
- orders = registry.find(:orders)
- products = registry.find(:products)
-
- expect(orders.position).to be < products.position
- end
- end
-
- describe '#insert_after' do
- it 'inserts item after target' do
- registry.add(:products, label: 'Products', position: 20)
- registry.insert_after(:products, :customers, label: 'Customers')
-
- customers = registry.find(:customers)
- products = registry.find(:products)
-
- expect(customers.position).to be > products.position
- end
- end
-
- describe '#move' do
- it 'moves item to first position' do
- registry.add(:dashboard, label: 'Dashboard', position: 10)
- registry.add(:products, label: 'Products', position: 20)
- registry.move(:products, position: :first)
-
- products = registry.find(:products)
- expect(products.position).to eq(-999)
- end
-
- it 'moves item to last position' do
- registry.add(:dashboard, label: 'Dashboard', position: 10)
- registry.add(:products, label: 'Products', position: 20)
- registry.move(:dashboard, position: :last)
-
- dashboard = registry.find(:dashboard)
- expect(dashboard.position).to eq(999)
- end
-
- it 'moves item before another' do
- registry.add(:dashboard, label: 'Dashboard', position: 10)
- registry.add(:products, label: 'Products', position: 20)
- registry.move(:products, before: :dashboard)
-
- products = registry.find(:products)
- dashboard = registry.find(:dashboard)
-
- expect(products.position).to be < dashboard.position
- end
-
- it 'moves item after another' do
- registry.add(:dashboard, label: 'Dashboard', position: 10)
- registry.add(:products, label: 'Products', position: 20)
- registry.move(:dashboard, after: :products)
-
- dashboard = registry.find(:dashboard)
- products = registry.find(:products)
-
- expect(dashboard.position).to be > products.position
- end
- end
-
- describe '#root_items' do
- it 'returns items without parent' do
- registry.add(:dashboard, label: 'Dashboard')
- registry.add(:products, label: 'Products') do |nav|
- nav.add(:all_products, label: 'All Products')
- end
-
- root_items = registry.root_items
-
- expect(root_items.size).to eq(2)
- expect(root_items.map(&:key)).to contain_exactly(:dashboard, :products)
- end
-
- it 'returns items sorted by position' do
- registry.add(:products, label: 'Products', position: 20)
- registry.add(:dashboard, label: 'Dashboard', position: 10)
-
- root_items = registry.root_items
-
- expect(root_items.first.key).to eq(:dashboard)
- expect(root_items.last.key).to eq(:products)
- end
- end
-
- describe '#visible_items' do
- it 'returns all items when no conditions' do
- registry.add(:dashboard, label: 'Dashboard')
- registry.add(:products, label: 'Products')
-
- visible = registry.visible_items(nil)
-
- expect(visible.size).to eq(2)
- end
-
- it 'filters items based on condition with view context' do
- context = Object.new
- def context.admin?
- true
- end
-
- registry.add(:dashboard, label: 'Dashboard')
- registry.add(:admin_panel, label: 'Admin', if: -> { admin? })
- registry.add(:restricted, label: 'Restricted', if: -> { false })
-
- visible = registry.visible_items(context)
-
- expect(visible.map(&:key)).to contain_exactly(:dashboard, :admin_panel)
- end
- end
-
- describe '#section' do
- it 'creates a section header' do
- section = registry.section(:main, label: 'Main Section')
-
- expect(section.section?).to be true
- expect(section.section_label).to eq('Main Section')
- end
-
- it 'supports block for section items' do
- registry.section(:main, label: 'Main Section') do |nav|
- nav.add(:dashboard, label: 'Dashboard')
- nav.add(:products, label: 'Products')
- end
-
- section = registry.find(:main)
- expect(section.children.size).to eq(2)
- end
- end
-
- describe '#clear' do
- it 'removes all items' do
- registry.add(:dashboard, label: 'Dashboard')
- registry.add(:products, label: 'Products')
- registry.clear
-
- expect(registry.root_items).to be_empty
- end
- end
-end
diff --git a/admin/spec/models/spree/admin/navigation_spec.rb b/admin/spec/models/spree/admin/navigation_spec.rb
index 8928af12d1..eda9d9c25f 100644
--- a/admin/spec/models/spree/admin/navigation_spec.rb
+++ b/admin/spec/models/spree/admin/navigation_spec.rb
@@ -1,144 +1,229 @@
require 'spec_helper'
RSpec.describe Spree::Admin::Navigation do
- let(:current_store) { @default_store }
+ let(:navigation) { described_class.new(:sidebar) }
- after do
- # Clear all registries after each test
- described_class.clear_all!
- end
+ describe '#add' do
+ it 'adds a navigation item' do
+ item = navigation.add(:dashboard, label: 'Dashboard', url: '/admin')
+
+ expect(navigation.find(:dashboard)).to eq(item)
+ end
- describe '.configure' do
- it 'configures navigation for a context' do
- described_class.configure(:sidebar) do |nav|
- nav.add :dashboard, label: 'Dashboard', url: '/admin'
+ it 'supports block for nested items' do
+ navigation.add(:products, label: 'Products') do |nav|
+ nav.add(:all_products, label: 'All Products')
end
- expect(described_class.find(:dashboard)).not_to be_nil
+ parent = navigation.find(:products)
+ expect(parent.children.size).to eq(1)
+ expect(parent.children.first.key).to eq(:all_products)
end
+ end
- it 'supports multiple contexts' do
- described_class.configure(:sidebar) do |nav|
- nav.add :dashboard, label: 'Dashboard'
- end
+ describe '#remove' do
+ it 'removes a navigation item' do
+ navigation.add(:dashboard, label: 'Dashboard')
+ navigation.remove(:dashboard)
+
+ expect(navigation.find(:dashboard)).to be_nil
+ end
- described_class.configure(:settings) do |nav|
- nav.add :general, label: 'General'
+ it 'removes item from parent children' do
+ navigation.add(:products, label: 'Products') do |nav|
+ nav.add(:all_products, label: 'All Products')
end
- expect(described_class.registry(:sidebar).find(:dashboard)).not_to be_nil
- expect(described_class.registry(:settings).find(:general)).not_to be_nil
- expect(described_class.registry(:sidebar).find(:general)).to be_nil
+ navigation.remove(:all_products)
+
+ parent = navigation.find(:products)
+ expect(parent.children).to be_empty
end
end
- describe '.registry' do
- it 'creates a new registry for a context' do
- registry = described_class.registry(:custom)
+ describe '#update' do
+ it 'updates an existing item' do
+ navigation.add(:dashboard, label: 'Dashboard', icon: 'home')
+ navigation.update(:dashboard, label: 'Home', icon: 'house')
- expect(registry).to be_a(Spree::Admin::Navigation::Registry)
- expect(registry.context).to eq(:custom)
+ item = navigation.find(:dashboard)
+ expect(item.label).to eq('Home')
+ expect(item.icon).to eq('house')
end
- it 'returns the same registry for the same context' do
- registry1 = described_class.registry(:sidebar)
- registry2 = described_class.registry(:sidebar)
+ it 'returns nil for non-existent item' do
+ result = navigation.update(:nonexistent, label: 'Test')
- expect(registry1).to equal(registry2)
+ expect(result).to be_nil
end
end
- describe '.for' do
- it 'returns registry for context' do
- registry = described_class.for(:sidebar)
+ describe '#find' do
+ it 'finds an item by key' do
+ item = navigation.add(:dashboard, label: 'Dashboard')
- expect(registry).to be_a(Spree::Admin::Navigation::Registry)
+ expect(navigation.find(:dashboard)).to eq(item)
+ end
+
+ it 'returns nil for non-existent item' do
+ expect(navigation.find(:nonexistent)).to be_nil
end
end
- describe 'delegation to default registry' do
- it 'delegates add to sidebar registry' do
- described_class.add(:dashboard, label: 'Dashboard')
+ describe '#exists?' do
+ it 'returns true when item exists' do
+ navigation.add(:dashboard, label: 'Dashboard')
- expect(described_class.registry(:sidebar).find(:dashboard)).not_to be_nil
+ expect(navigation.exists?(:dashboard)).to be true
end
- it 'delegates remove to sidebar registry' do
- described_class.add(:dashboard, label: 'Dashboard')
- described_class.remove(:dashboard)
+ it 'returns false when item does not exist' do
+ expect(navigation.exists?(:nonexistent)).to be false
+ end
+ end
+
+ describe '#insert_before' do
+ it 'inserts item before target' do
+ navigation.add(:products, label: 'Products', position: 20)
+ navigation.insert_before(:products, :orders, label: 'Orders')
+
+ orders = navigation.find(:orders)
+ products = navigation.find(:products)
- expect(described_class.find(:dashboard)).to be_nil
+ expect(orders.position).to be < products.position
end
+ end
- it 'delegates update to sidebar registry' do
- described_class.add(:dashboard, label: 'Dashboard')
- described_class.update(:dashboard, label: 'Home')
+ describe '#insert_after' do
+ it 'inserts item after target' do
+ navigation.add(:products, label: 'Products', position: 20)
+ navigation.insert_after(:products, :customers, label: 'Customers')
- item = described_class.find(:dashboard)
- expect(item.label).to eq('Home')
+ customers = navigation.find(:customers)
+ products = navigation.find(:products)
+
+ expect(customers.position).to be > products.position
end
+ end
- it 'delegates find to sidebar registry' do
- item = described_class.add(:dashboard, label: 'Dashboard')
+ describe '#move' do
+ it 'moves item to first position' do
+ navigation.add(:dashboard, label: 'Dashboard', position: 10)
+ navigation.add(:products, label: 'Products', position: 20)
+ navigation.move(:products, position: :first)
- expect(described_class.find(:dashboard)).to eq(item)
+ products = navigation.find(:products)
+ expect(products.position).to eq(-999)
end
- it 'delegates exists? to sidebar registry' do
- described_class.add(:dashboard, label: 'Dashboard')
+ it 'moves item to last position' do
+ navigation.add(:dashboard, label: 'Dashboard', position: 10)
+ navigation.add(:products, label: 'Products', position: 20)
+ navigation.move(:dashboard, position: :last)
+
+ dashboard = navigation.find(:dashboard)
+ expect(dashboard.position).to eq(999)
+ end
+
+ it 'moves item before another' do
+ navigation.add(:dashboard, label: 'Dashboard', position: 10)
+ navigation.add(:products, label: 'Products', position: 20)
+ navigation.move(:products, before: :dashboard)
+
+ products = navigation.find(:products)
+ dashboard = navigation.find(:dashboard)
- expect(described_class.exists?(:dashboard)).to be true
- expect(described_class.exists?(:nonexistent)).to be false
+ expect(products.position).to be < dashboard.position
+ end
+
+ it 'moves item after another' do
+ navigation.add(:dashboard, label: 'Dashboard', position: 10)
+ navigation.add(:products, label: 'Products', position: 20)
+ navigation.move(:dashboard, after: :products)
+
+ dashboard = navigation.find(:dashboard)
+ products = navigation.find(:products)
+
+ expect(dashboard.position).to be > products.position
end
end
- describe '.visible_items' do
- it 'returns visible items for context with view context' do
- described_class.configure(:sidebar) do |nav|
- nav.add :dashboard, label: 'Dashboard'
- nav.add :admin, label: 'Admin', if: -> { admin? }
+ describe '#root_items' do
+ it 'returns items without parent' do
+ navigation.add(:dashboard, label: 'Dashboard')
+ navigation.add(:products, label: 'Products') do |nav|
+ nav.add(:all_products, label: 'All Products')
end
+ root_items = navigation.root_items
+
+ expect(root_items.size).to eq(2)
+ expect(root_items.map(&:key)).to contain_exactly(:dashboard, :products)
+ end
+
+ it 'returns items sorted by position' do
+ navigation.add(:products, label: 'Products', position: 20)
+ navigation.add(:dashboard, label: 'Dashboard', position: 10)
+
+ root_items = navigation.root_items
+
+ expect(root_items.first.key).to eq(:dashboard)
+ expect(root_items.last.key).to eq(:products)
+ end
+ end
+
+ describe '#visible_items' do
+ it 'returns all items when no conditions' do
+ navigation.add(:dashboard, label: 'Dashboard')
+ navigation.add(:products, label: 'Products')
+
+ visible = navigation.visible_items(nil)
+
+ expect(visible.size).to eq(2)
+ end
+
+ it 'filters items based on condition with view context' do
context = Object.new
def context.admin?
true
end
- items = described_class.visible_items(:sidebar, context)
- expect(items.map(&:key)).to contain_exactly(:dashboard, :admin)
- end
- end
+ navigation.add(:dashboard, label: 'Dashboard')
+ navigation.add(:admin_panel, label: 'Admin', if: -> { admin? })
+ navigation.add(:restricted, label: 'Restricted', if: -> { false })
- describe '.contexts' do
- it 'returns all registered contexts' do
- described_class.registry(:sidebar)
- described_class.registry(:settings)
- described_class.registry(:custom)
+ visible = navigation.visible_items(context)
- expect(described_class.contexts).to contain_exactly(:sidebar, :settings, :custom)
+ expect(visible.map(&:key)).to contain_exactly(:dashboard, :admin_panel)
end
end
- describe '.clear_all!' do
- it 'clears all registries' do
- described_class.add(:dashboard, label: 'Dashboard')
- described_class.for(:settings).add(:general, label: 'General')
+ describe '#section' do
+ it 'creates a section header' do
+ section = navigation.section(:main, label: 'Main Section')
- described_class.clear_all!
+ expect(section.section?).to be true
+ expect(section.section_label).to eq('Main Section')
+ end
- expect(described_class.registries).to be_empty
+ it 'supports block for section items' do
+ navigation.section(:main, label: 'Main Section') do |nav|
+ nav.add(:dashboard, label: 'Dashboard')
+ nav.add(:products, label: 'Products')
+ end
+
+ section = navigation.find(:main)
+ expect(section.children.size).to eq(2)
end
end
- describe '.clear!' do
- it 'clears specific registry' do
- described_class.add(:dashboard, label: 'Dashboard')
- described_class.for(:settings).add(:general, label: 'General')
-
- described_class.clear!(:sidebar)
+ describe '#clear' do
+ it 'removes all items' do
+ navigation.add(:dashboard, label: 'Dashboard')
+ navigation.add(:products, label: 'Products')
+ navigation.clear
- expect(described_class.find(:dashboard)).to be_nil
- expect(described_class.for(:settings).find(:general)).not_to be_nil
+ expect(navigation.root_items).to be_empty
end
end
end