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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Replace partials with helper methods for rendering admin navigation (#13519)
Damian Legawiec2cded117a218
diff --git a/admin/app/helpers/spree/admin/navigation_helper.rb b/admin/app/helpers/spree/admin/navigation_helper.rbindex 616d64d1cd..633f7212d5 100644--- a/admin/app/helpers/spree/admin/navigation_helper.rb+++ b/admin/app/helpers/spree/admin/navigation_helper.rb@@ -323,10 +323,7 @@ module Spreeitems = navigation_items(context)return '' if items.empty?- render 'spree/admin/shared/navigation',- items: items,- context: context,- **options+ render_navigation_items(items, context)end# Get navigation items for the given context@@ -337,6 +334,51 @@ module SpreeSpree.admin.navigation.send(context)&.visible_items(self) || []end+ # Renders navigation items as an unordered list+ # @param items [Array<Spree::Admin::Navigation::Item>] navigation items to render+ # @param context [Symbol] the navigation context+ # @return [SafeBuffer] the rendered HTML+ def render_navigation_items(items, context)+ return ''.html_safe if items.empty?++ content_tag :ul, class: 'nav flex-col' do+ safe_join(items.map { |item| render_navigation_item(item, context) })+ end+ end++ # Renders a single navigation item+ # @param item [Spree::Admin::Navigation::Item] the navigation item+ # @param context [Symbol] the navigation context+ # @return [SafeBuffer] the rendered HTML+ def render_navigation_item(item, context)+ return render_nav_section_header(item) if item.section?++ item_url = item.resolve_url(self)+ item_label = item.resolve_label+ badge_value = item.badge_value(self)+ is_active = item.active?(request.path, self)+ has_children = item.children.present?+ tooltip_text = item.tooltip++ # Build data attributes+ data_attrs = item.data_attributes.dup+ data_attrs[:controller] = 'tooltip' if tooltip_text.present?++ # Build HTML options+ html_options = {}+ html_options[:target] = item.target if item.target.present?+ html_options[:id] = "nav-link-#{item.key}" if item.key.present?++ # Build complete label with badge and tooltip+ complete_label = build_nav_label(item_label, badge_value, item.badge_class, tooltip_text)++ if has_children+ render_nav_item_with_children(item, complete_label, item_url, item_label, is_active, data_attrs, html_options, context)+ else+ nav_item(complete_label, item_url, icon: item.icon, active: is_active, data: data_attrs, **html_options)+ end+ end+# Renders page tab navigation for the given context# @param context [Symbol] the navigation context (:tax_tabs, :shipping_tabs, etc.)# @param options [Hash] additional options for rendering@@ -355,6 +397,54 @@ module Spreeend.join.html_safeendend++ private++ # Builds navigation label with optional badge and tooltip+ # @return [SafeBuffer] the label HTML+ def build_nav_label(label, badge_value, badge_class, tooltip_text)+ result = label.to_s+ if badge_value.present?+ css_class = badge_class.presence || 'badge-light'+ result += content_tag(:span, badge_value, class: "badge ml-auto #{css_class}")+ end+ if tooltip_text.present?+ result += tooltip(tooltip_text)+ end+ result.html_safe+ end++ # Renders a section header+ # @return [SafeBuffer] the section header HTML+ def render_nav_section_header(item)+ content_tag :li, class: 'nav-item nav-section-header mt-4 border-t pt-4 pl-2' do+ content_tag :span, item.section_label, class: 'text-gray-600 uppercase font-light text-sm'+ end+ end++ # Renders a nav item that has children (with submenu)+ # @return [SafeBuffer] the nav item with submenu HTML+ def render_nav_item_with_children(item, complete_label, item_url, item_label, is_active, data_attrs, html_options, context)+ visible_children = item.children.select { |child| child.visible?(self) }++ # Main nav item+ main_item = nav_item(complete_label, item_url, icon: item.icon, active: is_active, data: data_attrs, **html_options)++ # Submenu for expanded sidebar (only shown when active)+ submenu = content_tag :ul, class: "nav-submenu#{' hidden' unless is_active}", id: "nav-submenu-#{item.key}" do+ safe_join(visible_children.map { |child| render_navigation_item(child, context) })+ end++ # Dropdown submenu for collapsed sidebar (shown on hover)+ dropdown = content_tag :ul, class: 'nav-submenu-dropdown hidden dropdown-container', id: "nav-submenu-dropdown-#{item.key}" do+ # Parent item as first dropdown item+ parent_link = nav_item(item_label, item_url, icon: nil)+ children_items = safe_join(visible_children.map { |child| render_navigation_item(child, context) })+ parent_link + children_items+ end++ main_item + submenu + dropdown+ endendendenddiff --git a/admin/app/views/spree/admin/shared/_navigation.html.erb b/admin/app/views/spree/admin/shared/_navigation.html.erbdeleted file mode 100644index 96944d427f..0000000000--- a/admin/app/views/spree/admin/shared/_navigation.html.erb+++ /dev/null@@ -1,3 +0,0 @@-<ul class="nav flex-col">- <%= render collection: items, partial: 'spree/admin/shared/navigation_item', as: :item, locals: { context: context } %>-</ul>diff --git a/admin/app/views/spree/admin/shared/_navigation_item.html.erb b/admin/app/views/spree/admin/shared/_navigation_item.html.erbdeleted file mode 100644index 0279913349..0000000000--- a/admin/app/views/spree/admin/shared/_navigation_item.html.erb+++ /dev/null@@ -1,60 +0,0 @@-<%- # Resolve item properties (pass self as context for route helpers)- item_url = item.resolve_url(self)- item_label = item.resolve_label- badge_value = item.badge_value(self)- is_active = item.active?(request.path, self)- has_children = item.children.present?- tooltip_text = item.tooltip-- # Build data attributes - only add tooltip controller if tooltip is present- data_attrs = item.data_attributes.dup- if tooltip_text.present?- data_attrs[:controller] = 'tooltip'- end-- # Build additional HTML options (like target)- html_options = {}- html_options[:target] = item.target if item.target.present?- html_options[:id] = "nav-link-#{item.key}" if item.key.present?-- # Build complete label with badge (like old menu pattern)- complete_label = item_label- if badge_value.present?- badge_class = item.badge_class.presence || 'badge-light'- complete_label += content_tag(:span, badge_value, class: "badge ml-auto #{badge_class}")- end- # Add tooltip if present- if tooltip_text.present?- complete_label += tooltip(tooltip_text)- end- complete_label = complete_label&.html_safe-%>--<% if item.section? %>- <%# Section header %>- <li class="nav-item nav-section-header mt-4 border-t pt-4 pl-2">- <span class="text-gray-600 uppercase font-light text-sm"><%= item.section_label %></span>- </li>-<% else %>- <% if has_children %>- <%# Item with children (submenu shows when parent is active) %>- <%= nav_item(complete_label, item_url, icon: item.icon, active: is_active, data: data_attrs, **html_options) %>-- <%# Submenu for expanded sidebar (only shown when active) %>- <ul class="nav-submenu <% unless is_active %>hidden<% end %>" id="nav-submenu-<%= item.key %>">- <%= render collection: item.children.select { |child| child.visible?(self) }, partial: 'spree/admin/shared/navigation_item', as: :item, locals: { context: context } %>- </ul>-- <%# Submenu dropdown for collapsed sidebar (always rendered, shown on hover) %>- <ul class="nav-submenu-dropdown hidden dropdown-container" id="nav-submenu-dropdown-<%= item.key %>">- <%# Add parent item as first item in dropdown %>- <%= nav_item(item_label, item_url, icon: nil) %>-- <%= render collection: item.children.select { |child| child.visible?(self) }, partial: 'spree/admin/shared/navigation_item', as: :item, locals: { context: context } %>- </ul>- <% else %>- <%# Regular item without children %>- <%= nav_item(complete_label, item_url, icon: item.icon, active: is_active, data: data_attrs, **html_options) %>- <% end %>-<% end %>diff --git a/admin/spec/helpers/spree/admin/navigation_helper_spec.rb b/admin/spec/helpers/spree/admin/navigation_helper_spec.rbindex f462073e9d..b1c5613347 100644--- a/admin/spec/helpers/spree/admin/navigation_helper_spec.rb+++ b/admin/spec/helpers/spree/admin/navigation_helper_spec.rb@@ -138,14 +138,14 @@ describe Spree::Admin::NavigationHelper, type: :helper doexpect(helper.render_navigation(:sidebar)).to eq('')end- it 'renders navigation partial when items are visible' do+ it 'renders navigation HTML when items are visible' donav.add :dashboard, label: 'Dashboard', url: '/admin'-- allow(helper).to receive(:render).and_return('navigation html')+ allow(helper).to receive(:request).and_return(double(path: '/admin', original_fullpath: '/admin'))result = helper.render_navigation(:sidebar)- expect(result).to eq('navigation html')+ expect(result).to include('nav flex-col')+ expect(result).to include('Dashboard')endend@@ -177,4 +177,183 @@ describe Spree::Admin::NavigationHelper, type: :helper doexpect(items.map(&:key)).to eq([:dashboard])endend++ describe '#render_navigation_items' do+ let(:nav) { Spree.admin.navigation.sidebar }++ before do+ nav.clear+ allow(helper).to receive(:request).and_return(double(path: '/admin', original_fullpath: '/admin'))+ end++ it 'returns empty string for empty items' do+ expect(helper.render_navigation_items([], :sidebar)).to eq('')+ end++ it 'renders ul with nav flex-col class' do+ nav.add :dashboard, label: 'Dashboard', url: '/admin'+ items = helper.navigation_items(:sidebar)++ result = helper.render_navigation_items(items, :sidebar)++ expect(result).to have_selector('ul.nav.flex-col')+ end++ it 'renders all visible items' do+ nav.add :dashboard, label: 'Dashboard', url: '/admin'+ nav.add :products, label: 'Products', url: '/admin/products'+ items = helper.navigation_items(:sidebar)++ result = helper.render_navigation_items(items, :sidebar)++ expect(result).to include('Dashboard')+ expect(result).to include('Products')+ end+ end++ describe '#render_navigation_item' do+ let(:nav) { Spree.admin.navigation.sidebar }++ before do+ nav.clear+ allow(helper).to receive(:request).and_return(double(path: '/admin', original_fullpath: '/admin'))+ end++ context 'with a regular item' do+ it 'renders a nav item with label and url' do+ nav.add :dashboard, label: 'Dashboard', url: '/admin'+ item = nav.visible_items(helper).first++ result = helper.render_navigation_item(item, :sidebar)++ expect(result).to include('Dashboard')+ expect(result).to include('/admin')+ expect(result).to have_selector('li.nav-item')+ end++ it 'renders icon when present' do+ nav.add :dashboard, label: 'Dashboard', url: '/admin', icon: 'home'+ item = nav.visible_items(helper).first++ result = helper.render_navigation_item(item, :sidebar)++ expect(result).to include('ti-home')+ end++ it 'renders id attribute when key is present' do+ nav.add :dashboard, label: 'Dashboard', url: '/admin'+ item = nav.visible_items(helper).first++ result = helper.render_navigation_item(item, :sidebar)++ expect(result).to include('id="nav-link-dashboard"')+ end++ it 'renders target attribute when present' do+ nav.add :external, label: 'External', url: 'https://example.com', target: '_blank'+ item = nav.visible_items(helper).first++ result = helper.render_navigation_item(item, :sidebar)++ expect(result).to include('target="_blank"')+ end+ end++ context 'with a section header' do+ it 'renders section header' do+ nav.add :section, section_label: 'Settings Section'+ item = nav.visible_items(helper).first++ result = helper.render_navigation_item(item, :sidebar)++ expect(result).to have_selector('li.nav-section-header')+ expect(result).to include('Settings Section')+ end+ end++ context 'with children' do+ before do+ nav.add :products, label: 'Products', url: '/admin/products', icon: 'package' do |products|+ products.add :all_products, label: 'All Products', url: '/admin/products'+ products.add :categories, label: 'Categories', url: '/admin/taxons'+ end+ end++ it 'renders main item' do+ item = nav.visible_items(helper).first++ result = helper.render_navigation_item(item, :sidebar)++ expect(result).to include('Products')+ end++ it 'renders submenu with children' do+ item = nav.visible_items(helper).first++ result = helper.render_navigation_item(item, :sidebar)++ expect(result).to have_selector('ul.nav-submenu')+ expect(result).to include('Categories')+ end++ it 'renders dropdown submenu for collapsed sidebar' do+ item = nav.visible_items(helper).first++ result = helper.render_navigation_item(item, :sidebar)++ expect(result).to have_selector('ul.nav-submenu-dropdown')+ end++ it 'hides submenu when item is not active' do+ allow(helper).to receive(:request).and_return(double(path: '/admin/orders', original_fullpath: '/admin/orders'))+ item = nav.visible_items(helper).first++ result = helper.render_navigation_item(item, :sidebar)++ expect(result).to have_selector('ul.nav-submenu.hidden')+ end++ it 'shows submenu when item is active' do+ allow(helper).to receive(:request).and_return(double(path: '/admin/products', original_fullpath: '/admin/products'))+ item = nav.visible_items(helper).first++ result = helper.render_navigation_item(item, :sidebar)++ expect(result).to have_selector('ul.nav-submenu')+ expect(result).not_to have_selector('ul.nav-submenu.hidden')+ end+ end++ context 'with badge' do+ it 'renders badge when present' do+ nav.add :orders, label: 'Orders', url: '/admin/orders', badge: -> { 5 }+ item = nav.visible_items(helper).first++ result = helper.render_navigation_item(item, :sidebar)++ expect(result).to have_selector('span.badge')+ expect(result).to include('5')+ end++ it 'renders badge with custom class' do+ nav.add :orders, label: 'Orders', url: '/admin/orders', badge: -> { 5 }, badge_class: 'badge-danger'+ item = nav.visible_items(helper).first++ result = helper.render_navigation_item(item, :sidebar)++ expect(result).to have_selector('span.badge.badge-danger')+ end+ end++ context 'with tooltip' do+ it 'adds tooltip controller data attribute when tooltip is present' do+ nav.add :dashboard, label: 'Dashboard', url: '/admin', tooltip: 'Tooltip text'+ item = nav.visible_items(helper).first++ result = helper.render_navigation_item(item, :sidebar)++ expect(result).to include('data-controller="tooltip"')+ end+ end+ endend