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
Improve using menu items in frontend.
Matthew Kennedy56aec0fbc42a
diff --git a/backend/spec/controllers/spree/admin/menu_items_controller_spec.rb b/backend/spec/controllers/spree/admin/menu_items_controller_spec.rbnew file mode 100644index 0000000000..c0466db136--- /dev/null+++ b/backend/spec/controllers/spree/admin/menu_items_controller_spec.rb@@ -0,0 +1,16 @@+require 'spec_helper'++describe Spree::Admin::MenusController, type: :controller do+ stub_authorization!++ let!(:menu) { create(:menu, unique_code: 'xyz') }+ let(:image_file) { Rack::Test::UploadedFile.new(Spree::Backend::Engine.root.join('spec', 'fixtures', 'thinking-cat.jpg')) }+ let(:menu_item) { create(:menu_item, menu_id: menu.id, parent_id: menu.root.id, image_asset: image_file) }++ describe 'GET index' do+ it 'is ok' do+ get :index+ expect(response).to be_ok+ end+ end+enddiff --git a/backend/spec/controllers/spree/admin/menus_controller_spec.rb b/backend/spec/controllers/spree/admin/menus_controller_spec.rbnew file mode 100644index 0000000000..7c5d2273e9--- /dev/null+++ b/backend/spec/controllers/spree/admin/menus_controller_spec.rb@@ -0,0 +1,12 @@+require 'spec_helper'++describe Spree::Admin::MenusController, type: :controller do+ stub_authorization!++ describe 'GET index' do+ it 'is ok' do+ get :index+ expect(response).to be_ok+ end+ end+enddiff --git a/core/lib/spree/core/controller_helpers/store.rb b/core/lib/spree/core/controller_helpers/store.rbindex 19137de9f8..54480ef7d6 100644--- a/core/lib/spree/core/controller_helpers/store.rb+++ b/core/lib/spree/core/controller_helpers/store.rb@@ -7,12 +7,17 @@ module Spreeincluded dohelper_method :current_storehelper_method :current_price_options+ helper_method :available_menusenddef current_store@current_store ||= Spree::Store.current(request.env['SERVER_NAME'])end+ def available_menus+ @available_menus ||= Spree::Menu.by_store(current_store)+ end+def store_localecurrent_store.default_localeenddiff --git a/frontend/app/controllers/spree/store_controller.rb b/frontend/app/controllers/spree/store_controller.rbindex 95b49303ca..ded0804d1c 100644--- a/frontend/app/controllers/spree/store_controller.rb+++ b/frontend/app/controllers/spree/store_controller.rb@@ -31,11 +31,32 @@ module Spreerender json: current_order(create_order_if_necessary: true) # force creation of order if doesn't existsend- def menu_by_code(code)- menu = Spree::Menu.by_store(current_store).by_unique_code(code)+ # Find a menu by its unique_code+ # this method will only return a menu if it is+ # available for use in the current store.+ def menu(unique_code)+ menu = available_menus.by_unique_code(unique_code)menu[0]end- helper_method :menu_by_code+ helper_method :menu++ # Returns the root for the menu by unique_code.+ # You can use .children to retrieve the top level of menu items,+ # or .descendants to retrieve all menu items.+ def root_item_for_menu(unique_code)+ if menu(unique_code).present?+ menu(unique_code).root+ end+ end+ helper_method :root_item_for_menu++ # Returns only the top level items for the menu by unique_code+ def top_level_items_for_menu(unique_code)+ if menu(unique_code).present?+ menu(unique_code).root.children+ end+ end+ helper_method :top_level_items_for_menuprotecteddiff --git a/frontend/app/views/spree/shared/_footer.html.erb b/frontend/app/views/spree/shared/_footer.html.erbindex 4dc7b85d9a..ef87170285 100644--- a/frontend/app/views/spree/shared/_footer.html.erb+++ b/frontend/app/views/spree/shared/_footer.html.erb@@ -49,15 +49,13 @@</div><div class="row d-none d-xl-flex border-top py-5 text-uppercase footer-spree-menu-links">- <% if menu_by_code('spree-all-footer').present? %>- <% footer_menu_root = menu_by_code('spree-all-footer').root %>-- <% footer_menu_root.children.each do |root| %>+ <% if top_level_items_for_menu('spree-all-footer').present? %>+ <% top_level_items_for_menu('spree-all-footer').each do |parent| %><div class="col-3"><div class="footer-spree-label">- <%= link_to root.name, localized_item_link(root, current_locale) %>+ <%= link_to parent.name, localized_item_link(parent, current_locale) %></div>- <% root.children.each do |child| %>+ <% parent.children.each do |child| %><% if child.code == 'category' && child.item_type == 'Container' %><% if child.children.present? %><% child.children.each do |category_link| %>diff --git a/frontend/app/views/spree/shared/_main_nav_bar.html.erb b/frontend/app/views/spree/shared/_main_nav_bar.html.erbindex af3bd82572..d31e039513 100644--- a/frontend/app/views/spree/shared/_main_nav_bar.html.erb+++ b/frontend/app/views/spree/shared/_main_nav_bar.html.erb@@ -1,22 +1,21 @@-<% if menu_by_code('spree-all-main').present? %>-<% main_menu_items = menu_by_code('spree-all-main') %>-<% category = 'category' %>-<% promo = 'promo' %>+<% if top_level_items_for_menu('spree-all-main').present? %>+ <% category = 'category' %>+ <% promo = 'promo' %><div class="h-100" role="navigation" aria-label="<%= Spree.t('nav_bar.desktop') %>"><ul class="nav h-100 main-nav-bar">- <% main_menu_items.root.children.each do |root| %>+ <% top_level_items_for_menu('spree-all-main').each do |parent| %><li class="p-0 nav-item main-nav-bar-category">- <% has_dropdown = root.children.present? %>+ <% has_dropdown = parent.children.present? %><% dropdown_toggle_class = has_dropdown ? "dropdown-toggle" : "" %><% data_attr = has_dropdown ? { toggle: "dropdown" } : {} %><% aria_attr = has_dropdown ? { haspopup: true, expanded: false } : {} %>- <%= link_to(root.name, localized_item_link(root, current_locale), class: "nav-link main-nav-bar-item main-nav-bar-category-button #{dropdown_toggle_class}", data: data_attr, aria: aria_attr) %>+ <%= link_to(parent.name, localized_item_link(parent, current_locale), class: "nav-link main-nav-bar-item main-nav-bar-category-button #{dropdown_toggle_class}", data: data_attr, aria: aria_attr) %><% if has_dropdown %><div class="dropdown-menu w-100 shadow main-nav-bar-category-dropdown"><div class="container p-0 d-flex justify-content-xl-around mx-auto"><div class="row px-5">- <% root.children.each do |child| %>+ <% parent.children.each do |child| %><!-- Categories --><% if child.code == category && child.item_type == 'Container' %>diff --git a/frontend/app/views/spree/shared/_mobile_navigation.html.erb b/frontend/app/views/spree/shared/_mobile_navigation.html.erbindex f56931a40d..ce33d86745 100644--- a/frontend/app/views/spree/shared/_mobile_navigation.html.erb+++ b/frontend/app/views/spree/shared/_mobile_navigation.html.erb@@ -1,6 +1,4 @@-<% if menu_by_code('spree-all-main').present? %>-<% main_menu_root = menu_by_code('spree-all-main').root %>-+<% if top_level_items_for_menu('spree-all-main').present? %><div class="position-fixed text-uppercase d-xl-none mobile-navigation" role="navigation" aria-label="<%= Spree.t('nav_bar.mobile') %>"><div class="container d-flex align-items-center justify-content-between header-spree" data-hook><button class="m-0 p-0" id="mobile-navigation-back-button" aria-label="<%= Spree.t('nav_bar.go_to_previous_menu') %>">@@ -20,22 +18,22 @@</div><ul class="list-unstyled position-relative h-100 mobile-navigation-list">- <% main_menu_root.children.each do |root| %>+ <% top_level_items_for_menu('spree-all-main').each do |parent| %><li class="d-flex justify-content-between align-items-center mobile-navigation-list-item">- <%= link_to root.name, localized_item_link(root, current_locale), class: 'w-75' %>- <% if root.children.present? %>- <a class="w-25 text-right mobile-navigation-category-link" data-category="<%= root.name.parameterize %>" href="#" aria-label="<%= Spree.t('go_to_category') %>">+ <%= link_to parent.name, localized_item_link(parent, current_locale), class: 'w-75' %>+ <% if parent.children.present? %>+ <a class="w-25 text-right mobile-navigation-category-link" data-category="<%= parent.name.parameterize %>" href="#" aria-label="<%= Spree.t('go_to_category') %>"><%= icon(name: 'arrow-right',classes: 'd-inline spree-icon-arrow spree-icon-arrow-right',width: 14,height: 27) %></a>- <ul class="list-unstyled position-absolute mobile-navigation-sublist" data-category="<%= root.name.parameterize %>">+ <ul class="list-unstyled position-absolute mobile-navigation-sublist" data-category="<%= parent.name.parameterize %>"><li class="text-center font-weight-bold mobile-navigation-sublist-header">- <%= root.name %>+ <%= parent.name %></li>- <% root.children.each do |child| %>+ <% parent.children.each do |child| %><% if child.code == 'category' && child.item_type == 'Container' %><li class="text-muted mt-2"><%= child.name %></li><% if child.children.present? %>