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
Move menu items to a dedicated root item.
Matthew Kennedy408b3ef157b7
diff --git a/api/app/controllers/spree/api/v2/platform/menu_items_controller.rb b/api/app/controllers/spree/api/v2/platform/menu_items_controller.rbindex c2445d3541..83894d5516 100644--- a/api/app/controllers/spree/api/v2/platform/menu_items_controller.rb+++ b/api/app/controllers/spree/api/v2/platform/menu_items_controller.rb@@ -11,7 +11,11 @@ module Spreescope.find(params[:new_parent_id])end- @moved_item.move_with_index(params[:new_position_idx].to_i, @new_parent)+ if @moved_item && @new_parent+ @moved_item.move_to_child_with_index(@new_parent, params[:new_position_idx].to_i)+ else+ head :bad_request+ endif @moved_item.savehead :no_contentdiff --git a/backend/app/views/spree/admin/menu_items/_form.html.erb b/backend/app/views/spree/admin/menu_items/_form.html.erbindex cfd10ff29b..8049ad5025 100644--- a/backend/app/views/spree/admin/menu_items/_form.html.erb+++ b/backend/app/views/spree/admin/menu_items/_form.html.erb@@ -23,7 +23,7 @@<%= f.field_container :parent_id, class: ['form-group'] do %><%= f.label :parent_id, Spree.t('admin.navigation.nested_under') %>- <%= f.select :parent_id, nested_set_options(@menu.menu_items, @menu_item) {|i| "#{'-' * i.level} #{i.name}" }, {include_blank: true}, {class: 'select2-clear'} %>+ <%= f.select :parent_id, nested_set_options(@menu.menu_items, @menu_item) {|i| "#{'-' * i.level} #{i.name}" }, {include_blank: false}, {class: 'select2'} %><%= f.error_message_on :parent_id %><small class="form-text text-muted"><%= Spree.t('admin.navigation.nested_under_info') %>diff --git a/backend/app/views/spree/admin/menus/edit.html.erb b/backend/app/views/spree/admin/menus/edit.html.erbindex 93b1b3d784..d771c2c088 100644--- a/backend/app/views/spree/admin/menus/edit.html.erb+++ b/backend/app/views/spree/admin/menus/edit.html.erb@@ -36,19 +36,18 @@</div></div>- <div class="card-body p-0 bg-light">- <div id="primaryMenuContainer" class="menu-container">- <% if @menu_items.root.present? %>- <% @menu_items.roots.each do |item| %>- <%= build_menu_item(@menu, item) %>- <% end %>- <% else %>- <div class="alert alert-info no-objects-found m-5">- <%= raw Spree.t('admin.navigation.no_menu_items', menu: @menu.name) %>- </div>- <% end %>+ <% if @menu_items.root.present? %>+ <% @menu_items.roots.each do |item| %>+ <div class="card-body p-0 bg-light">+ <div id="primaryMenuContainer" class="menu-container" data-parent-id="<%= item.id %>">++ <% item.children.each do |c| %>+ <%= build_menu_item(@menu, c) %>+ <% end %>+ </div></div>- </div>+ <% end %>+ <% end %></div></div></div>diff --git a/core/app/helpers/spree/base_helper.rb b/core/app/helpers/spree/base_helper.rbindex 2ef7b1056a..db6d0088ab 100644--- a/core/app/helpers/spree/base_helper.rb+++ b/core/app/helpers/spree/base_helper.rb@@ -31,17 +31,17 @@ module Spreeoutput_locale = if current_locale == current_store.default_locale''else- current_locale.to_s+ "/#{current_locale}"endif Spree::MenuItem::DYNAMIC_RESOURCE_TYPE.include? item.linked_resource_type- "/#{output_locale + item.destination}"+ "#{output_locale + item.destination}"elsecase item.linked_resource_typewhen 'URL'item.destinationwhen 'Home Page'- "/#{output_locale}"+ output_localeendendenddiff --git a/core/app/models/awesome_nested_set/movable_decorator.rb b/core/app/models/awesome_nested_set/movable_decorator.rbdeleted file mode 100644index 7ab731687d..0000000000--- a/core/app/models/awesome_nested_set/movable_decorator.rb+++ /dev/null@@ -1,39 +0,0 @@-module AwesomeNestedSet- module MovableDecorator- # Had to monkey patch AwesomeNestedSet, adding the ability to sort root items.-- # AwesomeNestedSet only give you .move_to_child_with_index,- # this works a treat for sorting any item that is nested inside a parent,- # but if you have items at the root level, you get nothing out of the box for re-ordering position.-- # Pass the new index of the item and its new parent node.- # if no node is passed (node = nil), it assumes you are going to / or already are at the root level and passes the- # process to .move_to_root_with_index, if a node is present it uses .move_to_child_with_index from AwesomeNestedSet- def move_with_index(index, node = nil)- if node.nil?- move_to_root_with_index(index)- else- move_to_child_with_index(node, index)- end- end-- def move_to_root_with_index(index)- # If we are already at level 0 the item should be root? => true- # and we can skip moving the item to root.- move_to_root unless level == 0 && root?-- # Sort the positioning of the item- # at root level based on its siblings.- my_position = siblings.to_a.index(self)- if my_position && my_position < index- move_to_right_of(siblings[index])- elsif my_position && my_position == index- # do nothing. already there.- else- move_to_left_of(siblings[index])- end- end- end-end--CollectiveIdea::Acts::NestedSet::Model::Movable.prepend AwesomeNestedSet::MovableDecoratordiff --git a/core/app/models/spree/menu.rb b/core/app/models/spree/menu.rbindex f322625f0b..b8e67e6966 100644--- a/core/app/models/spree/menu.rb+++ b/core/app/models/spree/menu.rb@@ -3,10 +3,27 @@ module Spreehas_many :menu_items, dependent: :destroyhas_and_belongs_to_many :stores+ after_create :set_root+ after_save :set_root_menu_item_name+validates :name, presence: truevalidates :unique_code, presence: true, uniqueness: truescope :by_store, ->(store) { joins(:stores).where('store_id = ?', store) }scope :by_unique_code, ->(menu_code) { where unique_code: menu_code }++ has_one :root, -> { where parent_id: nil }, class_name: 'Spree::MenuItem', dependent: :destroy++ private++ def set_root+ code_name = name.parameterize++ self.root ||= MenuItem.create!(menu_id: id, name: name, item_type: 'Container', code: "#{code_name}-root")+ end++ def set_root_menu_item_name+ root.update(name: name)+ endendenddiff --git a/frontend/app/views/spree/shared/_main_nav_bar.html.erb b/frontend/app/views/spree/shared/_main_nav_bar.html.erbindex d9278ae4bc..c7a78d733b 100644--- a/frontend/app/views/spree/shared/_main_nav_bar.html.erb+++ b/frontend/app/views/spree/shared/_main_nav_bar.html.erb@@ -1,12 +1,11 @@<% if menu_by_code('spree-all-main').present? %>-<% main_menu_items = menu_by_code('spree-all-main').menu_items.order(:lft) %>+<% main_menu_items = menu_by_code('spree-all-main') %><% 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.each do |root| %>- <% if root.root? %>+ <% main_menu_items.root.children.each do |root| %><li class="p-0 nav-item main-nav-bar-category"><% has_dropdown = root.children.present? %><% dropdown_toggle_class = has_dropdown ? "dropdown-toggle" : "" %>@@ -67,7 +66,7 @@<% end %></li>- <% end %>+<% end %></ul></div>diff --git a/sample/db/samples/menu_items.rb b/sample/db/samples/menu_items.rbindex 0ca9682740..e16714a0c6 100644--- a/sample/db/samples/menu_items.rb+++ b/sample/db/samples/menu_items.rb@@ -3,7 +3,7 @@ footer_menu = Spree::Menu.find_by!(name: 'Footer Menu')MENUS = [main_menu, footer_menu]-MENUS.each do |menu_id|+MENUS.each do |menu|############### Root Items ###############@@ -12,7 +12,8 @@ MENUS.each do |menu_id|name: 'Women',item_type: 'Link',linked_resource_type: 'Spree::Taxon',- menu_id: menu_id+ menu_id: menu,+ parent_id: menu.root.id).first_or_create!menu_root_women.linked_resource_id = woman_taxon.idmenu_root_women.save!@@ -22,7 +23,8 @@ MENUS.each do |menu_id|name: 'Men',item_type: 'Link',linked_resource_type: 'Spree::Taxon',- menu_id: menu_id+ menu_id: menu,+ parent_id: menu.root.id).first_or_create!menu_root_men.linked_resource_id = men_taxon.idmenu_root_men.save!@@ -32,7 +34,8 @@ MENUS.each do |menu_id|name: 'Sportsware',item_type: 'Link',linked_resource_type: 'Spree::Taxon',- menu_id: menu_id+ menu_id: menu,+ parent_id: menu.root.id).first_or_create!menu_root_sw.linked_resource_id = sw_taxon.idmenu_root_sw.save!@@ -44,7 +47,7 @@ MENUS.each do |menu_id|name: 'Categories',item_type: 'Container',code: 'category',- menu_id: menu_id,+ menu_id: menu,parent_id: menu_root_women).first_or_create!@@ -52,7 +55,7 @@ MENUS.each do |menu_id|name: 'Categories',item_type: 'Container',code: 'category',- menu_id: menu_id,+ menu_id: menu,parent_id: menu_root_men).first_or_create!@@ -60,51 +63,57 @@ MENUS.each do |menu_id|name: 'Categories',item_type: 'Container',code: 'category',- menu_id: menu_id,+ menu_id: menu,parent_id: menu_root_sw).first_or_create!############### Promotions ###############- menu_promo_women = Spree::MenuItem.where(- name: 'Promos',- item_type: 'Container',- code: 'promo',- menu_id: menu_id,- parent_id: menu_root_women- ).first_or_create!+ if menu.unique_code == 'spree-all-main'+ menu_promo_women = Spree::MenuItem.where(+ name: 'Promos',+ item_type: 'Container',+ code: 'promo',+ menu_id: menu,+ parent_id: menu_root_women+ ).first_or_create!- menu_promo_men = Spree::MenuItem.where(- name: 'Promos',- item_type: 'Container',- code: 'promo',- menu_id: menu_id,- parent_id: menu_root_men- ).first_or_create!+ menu_promo_men = Spree::MenuItem.where(+ name: 'Promos',+ item_type: 'Container',+ code: 'promo',+ menu_id: menu,+ parent_id: menu_root_men+ ).first_or_create!- menu_promo_sw = Spree::MenuItem.where(- name: 'Promos',- item_type: 'Container',- code: 'promo',- menu_id: menu_id,- parent_id: menu_root_sw- ).first_or_create!+ menu_promo_sw = Spree::MenuItem.where(+ name: 'Promos',+ item_type: 'Container',+ code: 'promo',+ menu_id: menu,+ parent_id: menu_root_sw+ ).first_or_create!++ promos = [menu_promo_women, menu_promo_men, menu_promo_sw]+ else+ promos = []+ end- promos = [menu_promo_women, menu_promo_men, menu_promo_sw]summer = Spree::Taxon.find_by!(permalink: 'new-collection/summer-2021')offers = Spree::Taxon.find_by!(permalink: 'special-offers/30-percent-off')###################### Links For: PROMOS ######################+promos.each do |promo|summer_promo = Spree::MenuItem.where(name: 'New Collection',subtitle: "Summer #{Date.today.year}",linked_resource_type: 'Spree::Taxon',item_type: 'Promotion',- menu_id: menu_id,+ menu_id: menu,parent_id: promo).first_or_create!summer_promo.linked_resource_id = summer.id@@ -115,7 +124,7 @@ MENUS.each do |menu_id|subtitle: 'Get up to 30% OFF',linked_resource_type: 'Spree::Taxon',item_type: 'Promotion',- menu_id: menu_id,+ menu_id: menu,parent_id: promo).first_or_create!special_offer.linked_resource_id = offers.id@@ -130,7 +139,7 @@ MENUS.each do |menu_id|name: 'Skirts',linked_resource_type: 'Spree::Taxon',item_type: 'Link',- menu_id: menu_id,+ menu_id: menu,parent_id: menu_cat_women).first_or_create!women_skirts.linked_resource_id = women_skirts_t.id@@ -141,7 +150,7 @@ MENUS.each do |menu_id|name: 'Dresses',linked_resource_type: 'Spree::Taxon',item_type: 'Link',- menu_id: menu_id,+ menu_id: menu,parent_id: menu_cat_women).first_or_create!women_dresses.linked_resource_id = women_dresses_t.id@@ -152,7 +161,7 @@ MENUS.each do |menu_id|name: 'Shirts And Blouses',linked_resource_type: 'Spree::Taxon',item_type: 'Link',- menu_id: menu_id,+ menu_id: menu,parent_id: menu_cat_women).first_or_create!women_s_b.linked_resource_id = women_s_b_t.id@@ -163,7 +172,7 @@ MENUS.each do |menu_id|name: 'Sweaters',linked_resource_type: 'Spree::Taxon',item_type: 'Link',- menu_id: menu_id,+ menu_id: menu,parent_id: menu_cat_women).first_or_create!women_sweaters.linked_resource_id = women_sweaters_t.id@@ -174,7 +183,7 @@ MENUS.each do |menu_id|name: 'Tops and T-Shirts',linked_resource_type: 'Spree::Taxon',item_type: 'Link',- menu_id: menu_id,+ menu_id: menu,parent_id: menu_cat_women).first_or_create!women_tops_tees.linked_resource_id = women_tops_tees_t.id@@ -185,7 +194,7 @@ MENUS.each do |menu_id|name: 'Jackets and Coats',linked_resource_type: 'Spree::Taxon',item_type: 'Link',- menu_id: menu_id,+ menu_id: menu,parent_id: menu_cat_women).first_or_create!women_j_c.linked_resource_id = women_j_c_t.id@@ -199,7 +208,7 @@ MENUS.each do |menu_id|name: 'Shirts',linked_resource_type: 'Spree::Taxon',item_type: 'Link',- menu_id: menu_id,+ menu_id: menu,parent_id: menu_cat_men).first_or_create!men_shirts.linked_resource_id = men_shirts_t.id@@ -210,7 +219,7 @@ MENUS.each do |menu_id|name: 'T-Shirts',linked_resource_type: 'Spree::Taxon',item_type: 'Link',- menu_id: menu_id,+ menu_id: menu,parent_id: menu_cat_men).first_or_create!men_t_shirts.linked_resource_id = men_t_shirts_t.id@@ -221,7 +230,7 @@ MENUS.each do |menu_id|name: 'Sweaters',linked_resource_type: 'Spree::Taxon',item_type: 'Link',- menu_id: menu_id,+ menu_id: menu,parent_id: menu_cat_men).first_or_create!men_sweaters.linked_resource_id = men_sweaters_t.id@@ -232,7 +241,7 @@ MENUS.each do |menu_id|name: 'Jackets and Coats',linked_resource_type: 'Spree::Taxon',item_type: 'Link',- menu_id: menu_id,+ menu_id: menu,parent_id: menu_cat_men).first_or_create!men_j_c.linked_resource_id = men_j_c_t.id@@ -246,7 +255,7 @@ MENUS.each do |menu_id|name: 'Tops',linked_resource_type: 'Spree::Taxon',item_type: 'Link',- menu_id: menu_id,+ menu_id: menu,parent_id: menu_cat_sw).first_or_create!sw_tops.linked_resource_id = sw_tops_t.id@@ -257,7 +266,7 @@ MENUS.each do |menu_id|name: 'Sweatshirts',linked_resource_type: 'Spree::Taxon',item_type: 'Link',- menu_id: menu_id,+ menu_id: menu,parent_id: menu_cat_sw).first_or_create!sw_sweatshirts.linked_resource_id = sw_sweatshirts_t.id@@ -268,7 +277,7 @@ MENUS.each do |menu_id|name: 'Pants',linked_resource_type: 'Spree::Taxon',item_type: 'Link',- menu_id: menu_id,+ menu_id: menu,parent_id: menu_cat_sw).first_or_create!sw_pants.linked_resource_id = sw_pants_t.id