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.

Admin nav system: parent should be active automatically if children a… (#13306)

Damian Legawiec4bc7144854df
diff --git a/admin/app/models/spree/admin/navigation/item.rb b/admin/app/models/spree/admin/navigation/item.rb
index ae0f4fa2b9..89e28c389e 100644
--- a/admin/app/models/spree/admin/navigation/item.rb
+++ b/admin/app/models/spree/admin/navigation/item.rb
@@ -59,6 +59,9 @@ module Spree
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)
diff --git a/admin/config/initializers/spree_admin_navigation.rb b/admin/config/initializers/spree_admin_navigation.rb
index 9eb6d46d6b..6dbafdfc2b 100644
--- a/admin/config/initializers/spree_admin_navigation.rb
+++ b/admin/config/initializers/spree_admin_navigation.rb
@@ -32,7 +32,6 @@ Rails.application.config.after_initialize do
url: :admin_orders_path,
icon: 'inbox',
position: 20,
- active: -> { %w[orders checkouts].include?(controller_name) || @order&.present? || @parent&.is_a?(Spree::Order) },
if: -> { can?(:manage, Spree::Order) },
badge: -> {
# Evaluated in view context with access to helper methods
@@ -66,7 +65,6 @@ Rails.application.config.after_initialize do
url: :admin_customer_returns_path,
icon: 'receipt-refund',
position: 25,
- active: -> { %w[customer_returns return_authorizations].include?(controller_name) },
if: -> { can?(:manage, Spree::CustomerReturn) || can?(:manage, Spree::ReturnAuthorization) } do |returns|
# Return Authorizations
returns.add :return_authorizations,
@@ -82,7 +80,6 @@ Rails.application.config.after_initialize do
url: :admin_products_path,
icon: 'package',
position: 30,
- active: -> { %w[products external_categories taxons taxonomies option_types option_values properties stock_items stock_transfers variants digital_assets].include?(controller_name) },
if: -> { can?(:manage, Spree::Product) } do |products|
# Stock
products.add :stock,
@@ -133,7 +130,6 @@ Rails.application.config.after_initialize do
url: :admin_users_path,
icon: 'users',
position: 40,
- active: -> { %w[users newsletter_subscribers].include?(controller_name) },
if: -> { can?(:manage, Spree.user_class) } do |customers|
# Newsletter Subscribers
customers.add :newsletter_subscribers,
@@ -148,7 +144,6 @@ Rails.application.config.after_initialize do
url: :admin_promotions_path,
icon: 'discount',
position: 50,
- active: -> { %w[promotions gift_cards gift_card_batches coupon_codes].include?(controller_name) },
if: -> { can?(:manage, Spree::Promotion) } do |promotions|
# Gift Cards
promotions.add :gift_cards,
@@ -172,7 +167,6 @@ Rails.application.config.after_initialize do
url: :admin_themes_path,
icon: 'building-store',
position: 70,
- active: -> { %w[themes posts post_categories pages storefront].include?(controller_name) || (controller_name == 'storefronts' && action_name == 'edit') },
if: -> { can?(:manage, Spree::Theme) } do |storefront|
# Pages
diff --git a/admin/spec/models/spree/admin/navigation/item_spec.rb b/admin/spec/models/spree/admin/navigation/item_spec.rb
index 94da38214f..6faab67580 100644
--- a/admin/spec/models/spree/admin/navigation/item_spec.rb
+++ b/admin/spec/models/spree/admin/navigation/item_spec.rb
@@ -120,6 +120,84 @@ RSpec.describe Spree::Admin::Navigation::Item do
expect(item.active?('/admin/orders', context)).to be true
end
+
+ context 'when no active_condition is set and item has children' do
+ let(:parent) { described_class.new(:products, url: '/admin/products') }
+ let(:child1) { described_class.new(:stock, url: '/admin/stock_items', active: -> { controller_name == 'stock_items' }) }
+ let(:child2) { described_class.new(:taxonomies, url: '/admin/taxonomies', active: -> { controller_name == 'taxonomies' }) }
+
+ before do
+ parent.add_child(child1)
+ parent.add_child(child2)
+ end
+
+ it 'returns true when a child item is active' do
+ context = Object.new
+ def context.controller_name
+ 'stock_items'
+ end
+
+ expect(parent.active?('/admin/stock_items', context)).to be true
+ end
+
+ it 'returns true when any child item is active' do
+ context = Object.new
+ def context.controller_name
+ 'taxonomies'
+ end
+
+ expect(parent.active?('/admin/taxonomies', context)).to be true
+ end
+
+ it 'returns false when no child item is active and path does not match' do
+ context = Object.new
+ def context.controller_name
+ 'orders'
+ end
+
+ expect(parent.active?('/admin/orders', context)).to be false
+ end
+
+ it 'returns true for exact parent url match even when no child is active' do
+ context = Object.new
+ def context.controller_name
+ 'products'
+ end
+
+ expect(parent.active?('/admin/products', context)).to be true
+ end
+
+ it 'checks children recursively' do
+ # Create a child without active_condition so it will check its own children
+ child_without_condition = described_class.new(:child, url: '/admin/child')
+ grandchild = described_class.new(:nested, url: '/admin/nested', active: -> { controller_name == 'nested' })
+ child_without_condition.add_child(grandchild)
+ parent.add_child(child_without_condition)
+
+ context = Object.new
+ def context.controller_name
+ 'nested'
+ end
+
+ expect(parent.active?('/admin/nested', context)).to be true
+ end
+ end
+
+ context 'when active_condition is set' do
+ it 'does not check children and uses the condition instead' do
+ parent = described_class.new(:products, url: '/admin/products', active: -> { controller_name == 'products' })
+ child = described_class.new(:stock, url: '/admin/stock_items', active: -> { controller_name == 'stock_items' })
+ parent.add_child(child)
+
+ context = Object.new
+ def context.controller_name
+ 'stock_items'
+ end
+
+ # Parent's active_condition returns false because controller_name is 'stock_items', not 'products'
+ expect(parent.active?('/admin/stock_items', context)).to be false
+ end
+ end
end
describe '#resolve_url' do