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
Add :unique_code to Spree::Menu and :code to Spree::MenuItem allowing for sturdy targeting, even if the :name is translated, or simply changed.
Matthew Kennedy7d8daabe04b5
diff --git a/backend/app/views/spree/admin/menu_items/_form.html.erb b/backend/app/views/spree/admin/menu_items/_form.html.erbindex bd02bf0dd4..96307dc4c1 100644--- a/backend/app/views/spree/admin/menu_items/_form.html.erb+++ b/backend/app/views/spree/admin/menu_items/_form.html.erb@@ -3,7 +3,7 @@<div class="col-12 col-md-6"><div class="card mb-3"><div class="card-header">- <h5 class="mb-0"><%= Spree.t('admin.navigation.menu_item_type') %></h5>+ <h5 class="mb-0"><%= Spree.t('admin.navigation.settings') %></h5></div><div class="card-body"><%= f.field_container :item_type, class: ['form-group'] do %>@@ -12,11 +12,15 @@<%= f.error_message_on :item_type %><% end %>- <div class="alert alert-info mt-3 mb-0">- <%= raw Spree.t('admin.navigation.menu_item_type_message') %>- </div>+ <%= f.field_container :code, class: ['form-group'] do %>+ <%= f.label :code, Spree.t('admin.navigation.code') %>+ <%= f.text_field :code, class: 'form-control' %>+ <%= f.error_message_on :code %>+ <small class="form-text text-muted">+ <%= Spree.t('admin.navigation.set_a_code') %>+ </small>+ <% end %>- <hr><%= 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'} %>@@ -25,6 +29,13 @@<%= Spree.t('admin.navigation.nested_under_info') %></small><% end %>++ <hr>++ <div class="alert alert-info mt-3 mb-0">+ <%= raw Spree.t('admin.navigation.menu_item_type_message') %>+ </div>+</div></div></div>@@ -32,7 +43,7 @@<div class="col-12 col-md-6"><div class="card mb-3"><div class="card-header">- <h5 class="mb-0"><%= Spree.t('admin.navigation.menu_item_basic_details') %></h5>+ <h5 class="mb-0"><%= Spree.t('admin.navigation.public_details') %></h5></div><div class="card-body"><%= f.field_container :name, class: ['form-group'] do %>diff --git a/backend/app/views/spree/admin/menus/_form.html.erb b/backend/app/views/spree/admin/menus/_form.html.erbindex 227468139e..8e025697c5 100644--- a/backend/app/views/spree/admin/menus/_form.html.erb+++ b/backend/app/views/spree/admin/menus/_form.html.erb@@ -1,10 +1,17 @@<div data-hook="admin_menu_form_fields" class="row"><div class="col-12 col-md-6"><%= f.field_container :name, class: ['form-group'] do %>- <%= f.label :name, Spree.t(:name) %>+ <%= f.label :name, raw(Spree.t(:name) + required_span_tag) %><%= f.text_field :name, class: 'form-control', required: true %><%= f.error_message_on :name %><% end %>++ <%= f.field_container :unique_code, class: ['form-group'] do %>+ <%= f.label :unique_code, raw(Spree.t('admin.navigation.unique_code') + required_span_tag) %>+ <%= f.text_field :unique_code, class: 'form-control', required: true %>+ <%= f.error_message_on :unique_code %>+ <small class="form-text text-muted"><%= Spree.t('admin.navigation.give_your_menu_a_unique_name') %></small>+ <% end %></div><div class="col-12 col-md-6"><div class="form-group">diff --git a/backend/app/views/spree/admin/menus/index.html.erb b/backend/app/views/spree/admin/menus/index.html.erbindex 0d259da4a6..fb9afdcbd8 100644--- a/backend/app/views/spree/admin/menus/index.html.erb+++ b/backend/app/views/spree/admin/menus/index.html.erb@@ -17,7 +17,12 @@<tbody><% @menus.each do |menu| %><tr>- <td><%= menu.name %></td>+ <td>+ <%= menu.name %> <br>+ <small class="form-text text-muted">+ <%= menu.unique_code %>+ </small>+ </td><td><% menu.store_ids.each do |store| %><span class="badge badge-secondary"><%= @stores.find(store).unique_name %></span>diff --git a/core/app/models/spree/menu.rb b/core/app/models/spree/menu.rbindex 806b2c4943..1d1948cbba 100644--- a/core/app/models/spree/menu.rb+++ b/core/app/models/spree/menu.rb@@ -3,7 +3,8 @@ module Spreehas_many :menu_items, dependent: :destroyhas_and_belongs_to_many :stores- validates :name, presence: true, uniqueness: true+ validates :name, presence: true+ validates :unique_code, presence: true, uniqueness: truescope :by_store, ->(store) { joins(:stores).where('store_id = ?', store) }scope :by_name, ->(menu_name) { where name: menu_name }diff --git a/core/config/locales/en.yml b/core/config/locales/en.ymlindex 91d55f116d..3f1c50f5be 100644--- a/core/config/locales/en.yml+++ b/core/config/locales/en.yml@@ -581,8 +581,7 @@ en:back_to_all_menus: Back To All Menusclick_create_to_load_new_link: 'Click the <b>Create</b> button below to save your new Menu Item, and update the link.'click_update_to_load_new_link: 'Click the <b>Update</b> button below to change the link.'- menu_item_type: Menu Item Type- menu_item_basic_details: Basic Details+ give_your_menu_a_unique_name: 'Use the unique code to identify your menu: spree-eu-main'link_settings: Link Settingslink_to: Link Toimage_asset: Add a Promotion image or Link icon.@@ -591,15 +590,19 @@ en:new_menu: New Menunested_under: Nested underno_menu_items: '<b>%{menu}</b> has no items. Click the <b>Add New Item</b> button to begin adding links to this menu.'- nested_under_info: Quickly nest your link in the menu.+ nested_under_info: Quickly nest your Menu Item if you already know where it needs to be.item_type: Item Typeopen_link_in_new_window: Open this link in a new window- menu_item_type_message: "For more information about Menu Item Types and nesting Menu Items, visit the documentation by+ menu_item_type_message: "For more information about Menu Item Types, Nesting and using Codes, visit the documentation by<a href='https://guides.spreecommerce.org/user/configuration/configuring_geography.html#zones' target='_blank' class='alert-link'>clicking here</a>."menus: Menusmenu_items: '%{menu_name} Items'+ public_details: Public Detailssubtitle: Subtitle+ settings: Settings+ set_a_code: Give your Menu Item a code, this is optional, but can be useful if you need to identify this menu item.used_in: Used In+ unique_code: Unique Codeyou_have_no_menus: You have no Menus, click the Add New Menu button to create your first Menu.this_link_takes_you_to_your_stores_home_page: This link takes you to your stores home page.administration: Administrationdiff --git a/core/db/migrate/20210407200948_create_spree_menus.rb b/core/db/migrate/20210407200948_create_spree_menus.rbindex 36b2cbb88d..b55d9545b3 100644--- a/core/db/migrate/20210407200948_create_spree_menus.rb+++ b/core/db/migrate/20210407200948_create_spree_menus.rb@@ -2,10 +2,12 @@ class CreateSpreeMenus < ActiveRecord::Migration[6.0]def changecreate_table :spree_menus do |t|t.column :name, :string+ t.column :unique_code, :string, null: falset.timestampsend- add_index :spree_menus, :name, unique: true+ add_index :spree_menus, :name+ add_index :spree_menus, :unique_code, unique: trueendenddiff --git a/core/db/migrate/20210408092939_create_spree_menu_items.rb b/core/db/migrate/20210408092939_create_spree_menu_items.rbindex 12838f7475..72a00e983d 100644--- a/core/db/migrate/20210408092939_create_spree_menu_items.rb+++ b/core/db/migrate/20210408092939_create_spree_menu_items.rb@@ -1,16 +1,19 @@class CreateSpreeMenuItems < ActiveRecord::Migration[6.0]def changecreate_table :spree_menu_items do |t|- t.column :name, :string+ t.column :name, :string, null: falset.column :subtitle, :stringt.column :url, :stringt.column :new_window, :boolean, default: falset.column :item_type, :stringt.column :linked_resource_type, :string, default: 'URL't.column :linked_resource_id, :integer+ t.column :code, :string+t.column :parent_id, :integer- t.column :lft, :integer- t.column :rgt, :integer+ t.column :lft, :integer, null: false+ t.column :rgt, :integer, null: false+ t.column :depth, :integer, null: false, default: 0t.belongs_to :menu@@ -21,6 +24,7 @@ class CreateSpreeMenuItems < ActiveRecord::Migration[6.0]add_index :spree_menu_items, :rgtadd_index :spree_menu_items, :parent_idadd_index :spree_menu_items, :item_type+ add_index :spree_menu_items, :codeadd_index :spree_menu_items, [:linked_resource_type, :linked_resource_id], name: 'index_spree_menu_items_on_linked_resource'endenddiff --git a/frontend/app/views/spree/shared/_main_nav_bar.html.erb b/frontend/app/views/spree/shared/_main_nav_bar.html.erbindex 624419bcdd..ebc8a2af4b 100644--- a/frontend/app/views/spree/shared/_main_nav_bar.html.erb+++ b/frontend/app/views/spree/shared/_main_nav_bar.html.erb@@ -18,7 +18,7 @@<% item.children.each do |child| %><!-- Categories -->- <% if child.name == 'Categories' && child.item_type == 'Container' %>+ <% if child.code == 'category' && child.item_type == 'Container' %><div class="category-links"><div class="category-links-header text-uppercase"><%= child.name %>@@ -36,7 +36,7 @@<% end %><!-- Promos -->- <% if child.name == 'Promos' && child.item_type == 'Container' %>+ <% if child.code == 'promo' && child.item_type == 'Container' %><% if child.children.present? %><% child.children.each do |promo_banner| %><%= link_to menu_item_link_to(promo_banner), class: "dropdown-item" do %>diff --git a/sample/db/samples/menu_items.rb b/sample/db/samples/menu_items.rbindex 472949da99..0ca9682740 100644--- a/sample/db/samples/menu_items.rb+++ b/sample/db/samples/menu_items.rb@@ -43,6 +43,7 @@ MENUS.each do |menu_id|menu_cat_women = Spree::MenuItem.where(name: 'Categories',item_type: 'Container',+ code: 'category',menu_id: menu_id,parent_id: menu_root_women).first_or_create!@@ -50,6 +51,7 @@ MENUS.each do |menu_id|menu_cat_men = Spree::MenuItem.where(name: 'Categories',item_type: 'Container',+ code: 'category',menu_id: menu_id,parent_id: menu_root_men).first_or_create!@@ -57,6 +59,7 @@ MENUS.each do |menu_id|menu_cat_sw = Spree::MenuItem.where(name: 'Categories',item_type: 'Container',+ code: 'category',menu_id: menu_id,parent_id: menu_root_sw).first_or_create!@@ -67,6 +70,7 @@ MENUS.each do |menu_id|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!@@ -74,6 +78,7 @@ MENUS.each do |menu_id|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!@@ -81,6 +86,7 @@ MENUS.each do |menu_id|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!diff --git a/sample/db/samples/menus.rb b/sample/db/samples/menus.rbindex 7d2351b83b..b75b37a774 100644--- a/sample/db/samples/menus.rb+++ b/sample/db/samples/menus.rb@@ -1,12 +1,14 @@main_menu = Spree::Menu.where(- name: 'Main Menu'+ name: 'Main Menu',+ unique_code: 'spree-all-main').first_or_create!main_menu.store_ids = Spree::Store.idsmain_menu.save!footer_menu = Spree::Menu.where(- name: 'Footer Menu'+ name: 'Footer Menu',+ unique_code: 'spree-all-footer').first_or_create!footer_menu.store_ids = Spree::Store.ids