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 multiple stores support to Spree::Menu
Matthew Kennedyadc45b115901
diff --git a/backend/app/controllers/spree/admin/menus_controller.rb b/backend/app/controllers/spree/admin/menus_controller.rbindex e4e808b524..4c55368d89 100644--- a/backend/app/controllers/spree/admin/menus_controller.rb+++ b/backend/app/controllers/spree/admin/menus_controller.rb@@ -1,8 +1,18 @@module Spreemodule Adminclass MenusController < ResourceController- def index- @menus = Spree::Menu.all+ before_action :load_data++ def index; end++ private++ def location_after_save+ spree.edit_admin_menu_path(@menu)+ end++ def load_data+ @stores = Spree::Store.allendendenddiff --git a/backend/app/helpers/spree/admin/menu_helper.rb b/backend/app/helpers/spree/admin/menu_helper.rbindex 3a570da733..79804d919f 100644--- a/backend/app/helpers/spree/admin/menu_helper.rb+++ b/backend/app/helpers/spree/admin/menu_helper.rb@@ -1,7 +1,17 @@module Spreemodule Adminmodule MenuHelper-+ def humanize_class_name(object)+ if object.is_a? Array+ object.map do |obj|+ obj.split('::').last+ end+ elsif object.is_a? String+ object.split('::').last+ else+ 'Pass me a String or an Array'+ end+ endendendenddiff --git a/backend/app/views/spree/admin/menu_items/_form.html.erb b/backend/app/views/spree/admin/menu_items/_form.html.erbindex 19d0ac24c8..4eb39ba407 100644--- a/backend/app/views/spree/admin/menu_items/_form.html.erb+++ b/backend/app/views/spree/admin/menu_items/_form.html.erb@@ -7,7 +7,7 @@</div><div class="card-body"><%= f.field_container :item_type, class: ['form-group'] do %>- <%= f.label :item_type, Spree.t(:item_type) %>+ <%= f.label :item_type, Spree.t('admin.navigation.item_type') %><%= f.select(:item_type, Spree::MenuItem::ITEM_TYPE, {include_blank: false}, { class: 'select2' }) %><%= f.error_message_on :item_type %><% end %>@@ -32,7 +32,7 @@<% end %><%= f.field_container :subtitle, class: ['form-group'] do %>- <%= f.label :subtitle, Spree.t(:subtitle) %>+ <%= f.label :subtitle, Spree.t('admin.navigation.subtitle') %><%= f.text_field :subtitle, class: 'form-control' %><%= f.error_message_on :subtitle %><% end %>@@ -54,7 +54,7 @@<div class="col-6"><%= f.field_container :linked_resource_type, class: ['form-group'] do %><%= f.label :linked_resource_type, Spree.t('admin.navigation.link_to') %>- <%= f.select(:linked_resource_type, Spree::MenuItem::LINKED_RESOURCE_TYPE, {include_blank: false}, { class: 'selec2', id: 'LinkToSelect2' }) %>+ <%= f.select(:linked_resource_type, humanize_class_name(Spree::MenuItem::LINKED_RESOURCE_TYPE), {include_blank: false}, { class: 'selec2', id: 'LinkToSelect2' }) %><%= f.error_message_on :linked_resource_type %><% end %></div>diff --git a/backend/app/views/spree/admin/menu_items/new.html.erb b/backend/app/views/spree/admin/menu_items/new.html.erbindex 54c6cb257c..1e9600b4e6 100644--- a/backend/app/views/spree/admin/menu_items/new.html.erb+++ b/backend/app/views/spree/admin/menu_items/new.html.erb@@ -1,5 +1,5 @@<% content_for :page_title do %>- <%= link_to @menu.name, spree.edit_admin_menu_path(@menu) %> / <%= Spree.t(:new_menu_item) %>+ <%= link_to @menu.name, spree.edit_admin_menu_path(@menu) %> / <%= Spree.t('admin.navigation.new_menu_item') %><% end %><%= render partial: 'spree/admin/shared/error_messages', locals: { target: @menu_items } %>diff --git a/backend/app/views/spree/admin/menus/_form.html.erb b/backend/app/views/spree/admin/menus/_form.html.erbindex b08977a2d1..6ea5057105 100644--- a/backend/app/views/spree/admin/menus/_form.html.erb+++ b/backend/app/views/spree/admin/menus/_form.html.erb@@ -1,9 +1,15 @@<div data-hook="admin_menu_form_fields" class="row">- <div class="col-12">+ <div class="col-12 col-md-6"><%= f.field_container :name, class: ['form-group'] do %><%= f.label :name, Spree.t(:name) %><%= f.text_field :name, class: 'form-control' %><%= f.error_message_on :name %><% end %></div>+ <div class="col-12 col-md-6">+ <div class="form-group ">+ <%= label_tag :menu_stores, Spree.t(:stores) %>+ <%= collection_select(:menu, :store_ids, @stores, :id, :unique_name, {}, { multiple: true, class: 'select2' }) %>+ </div>+ </div></div>diff --git a/backend/app/views/spree/admin/menus/edit.html.erb b/backend/app/views/spree/admin/menus/edit.html.erbindex 78b2c33e61..5520885025 100644--- a/backend/app/views/spree/admin/menus/edit.html.erb+++ b/backend/app/views/spree/admin/menus/edit.html.erb@@ -1,44 +1,66 @@<% content_for :page_title do %>- <%= link_to Spree.t(:menus), spree.admin_menus_url %> / <%= @menu.name %>+ <%= link_to Spree.t('admin.navigation.menus'), spree.admin_menus_url %> / <%= @menu.name %><% end %><% content_for :page_actions do %>- <%= button_link_to Spree.t('admin.navigation.add_new_menu_item', menu: @menu.name), spree.new_admin_menu_menu_item_path(@menu), icon: 'add.svg' %>+ <%= button_link_to Spree.t('admin.navigation.back_to_all_menus', menu: @menu.name), spree.admin_menus_url, icon: 'chevron-left.svg' %><% end %><%= render partial: 'spree/admin/shared/error_messages', locals: { target: @menu } %><%= form_for [:admin, @menu] do |f| %><%= render partial: 'form', locals: { f: f } %>- <%= render partial: 'spree/admin/shared/edit_resource_links' %>+ <div class="d-flex justify-content-center mb-5">+ <%= render partial: 'spree/admin/shared/edit_resource_links' %>+ </div><% end %><hr>-<% if @menu.menu_items.any? %>-<div class="table-responsive">- <table class="table">- <thead>- <th><%= Spree.t(:name) %></th>- <th data-hook="admin_menus_index_header_actions" class="actions"></th>- <thead>- <tbody>- <% @menu.menu_items.each do |menu_item| %>- <tr>- <td><%= menu_item.name %></td>- <td class="actions" data-hook="admin_menus_index_row_actions">- <span class="d-flex justify-content-end">- <%= link_to_edit(menu_item, no_text: true, url: spree.edit_admin_menu_menu_item_path(@menu, menu_item)) %>- <%= link_to_delete(menu_item, no_text: true, url: spree.admin_menu_menu_item_path(@menu, menu_item)) if can?(:destroy, menu_item) %>- </span>- </td>- </tr>- <% end %>- </tbody>- </table>-</div>-<% else %>- <div class="alert alert-info no-objects-found">- <%= Spree.t(:no_resource_found, resource: plural_resource_name(Spree::MenuItem)) %>+<div class="row mt-5">+ <div class="col-12">+ <% if @menu.menu_items.any? %>+ <div class="card mb-3">+ <div class="card-header">+ <div class="row pb-0">+ <div class="col d-flex">+ <h5 class="mb-0 align-self-center"><%= Spree.t('admin.navigation.menu_items') %></h5>+ </div>+ <div class="col d-flex justify-content-end">+ <%= button_link_to Spree.t('admin.navigation.add_new_item', menu: @menu.name), spree.new_admin_menu_menu_item_path(@menu), icon: 'add.svg', class: 'no-wrap btn btn-success align-self-center' %>+ </div>+ </div>+ </div>+ <div class="card-body">++ <div class="table-responsive">+ <table class="table">+ <thead>+ <th><%= Spree.t(:name) %></th>+ <th data-hook="admin_menus_index_header_actions" class="actions"></th>+ <thead>+ <tbody>+ <% @menu.menu_items.each do |menu_item| %>+ <tr>+ <td><%= menu_item.name %></td>+ <td class="actions" data-hook="admin_menus_index_row_actions">+ <span class="d-flex justify-content-end">+ <%= link_to_edit(menu_item, no_text: true, url: spree.edit_admin_menu_menu_item_path(@menu, menu_item)) %>+ <%= link_to_delete(menu_item, no_text: true, url: spree.admin_menu_menu_item_path(@menu, menu_item)) if can?(:destroy, menu_item) %>+ </span>+ </td>+ </tr>+ <% end %>+ </tbody>+ </table>+ </div>++ </div>+ </div>+ <% else %>+ <div class="alert alert-info no-objects-found">+ <%= Spree.t(:no_resource_found, resource: plural_resource_name(Spree::MenuItem)) %>+ </div>+ <% end %></div>-<% end %>+</div>diff --git a/backend/app/views/spree/admin/menus/index.html.erb b/backend/app/views/spree/admin/menus/index.html.erbindex 0c3407ad7b..2738688b85 100644--- a/backend/app/views/spree/admin/menus/index.html.erb+++ b/backend/app/views/spree/admin/menus/index.html.erb@@ -1,9 +1,9 @@<% content_for :page_title do %>- <%= Spree.t('menus') %>+ <%= Spree.t('admin.navigation.menus') %><% end %><% content_for :page_actions do %>- <%= button_link_to Spree.t(:new_menu), new_object_url, { class: "btn-success", icon: 'add.svg', id: 'new_menu' } %>+ <%= button_link_to Spree.t('admin.navigation.new_menu'), new_object_url, { class: "btn-success", icon: 'add.svg', id: 'new_menu' } %><% end if can?(:create, Spree::Menu) %><% if @menus.any? %>diff --git a/backend/app/views/spree/admin/shared/_main_menu.html.erb b/backend/app/views/spree/admin/shared/_main_menu.html.erbindex c43f828429..b8ce902d1f 100644--- a/backend/app/views/spree/admin/shared/_main_menu.html.erb+++ b/backend/app/views/spree/admin/shared/_main_menu.html.erb@@ -48,11 +48,10 @@<% if can? :admin, Spree::Menu %><ul class="nav nav-sidebar border-bottom" id="sidebarMenu">- <%= main_menu_tree Spree.t(:content), icon: "pages.svg", sub_menu: "content", url: "#sidebar-content" %>+ <%= main_menu_tree Spree.t('admin.tab.content'), icon: "pages.svg", sub_menu: "content", url: "#sidebar-content" %></ul><% end %>-<% if can? :admin, current_store %><ul class="nav nav-sidebar border-bottom" id="sidebarConfiguration"><%= main_menu_tree Spree.t(:configurations), icon: "settings.svg", sub_menu: "configuration", url: "#sidebar-configuration" %>diff --git a/backend/app/views/spree/admin/shared/sub_menu/_content.html.erb b/backend/app/views/spree/admin/shared/sub_menu/_content.html.erbindex 7e551a7dd2..77b4d80e0a 100644--- a/backend/app/views/spree/admin/shared/sub_menu/_content.html.erb+++ b/backend/app/views/spree/admin/shared/sub_menu/_content.html.erb@@ -1,3 +1,3 @@<ul id="sidebar-content" class="border-top bg-white collapse nav nav-pills nav-stacked" data-hook="admin_content">- <%= tab :menus, label: Spree.t('navigation') %>+ <%= tab :menus, match_path: '/menus', label: Spree.t('admin.tab.navigation') %></ul>diff --git a/core/app/models/spree/menu.rb b/core/app/models/spree/menu.rbindex e510ab6267..b7b6c33d3c 100644--- a/core/app/models/spree/menu.rb+++ b/core/app/models/spree/menu.rb@@ -1,6 +1,8 @@module Spreeclass Menu < Spree::Basehas_many :menu_items, dependent: :destroy+ has_and_belongs_to_many :stores+validates :name, presence: trueendenddiff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rbindex 4298e09805..efc793a0ab 100644--- a/core/app/models/spree/store.rb+++ b/core/app/models/spree/store.rb@@ -2,6 +2,7 @@ module Spreeclass Store < Spree::Basehas_many :orders, class_name: 'Spree::Order'has_many :payment_methods, class_name: 'Spree::PaymentMethod'+ has_many :menus, class_name: 'Spree::Menu'belongs_to :default_country, class_name: 'Spree::Country'belongs_to :checkout_zone, class_name: 'Spree::Zone'diff --git a/core/config/locales/en.yml b/core/config/locales/en.ymlindex f318e3172f..97ec5cd4c2 100644--- a/core/config/locales/en.yml+++ b/core/config/locales/en.yml@@ -534,6 +534,7 @@ en:admin:tab:configuration: Configuration+ content: Contentoption_types: Option Typesorders: Ordersoverview: Overview@@ -543,6 +544,7 @@ en:properties: Propertiesprototypes: Prototypesreports: Reports+ navigation: Navigationtaxonomies: Taxonomiestaxons: Taxonsusers: Users@@ -572,14 +574,21 @@ en:no_store_credit: User has no Store Credit available.available_store_credit: User has %{amount} in Store Credit available.navigation:- add_new_menu_item: 'Add A New Menu Item'+ add_new_item: 'Add New Item'+ back_to_all_menus: Back To All Menusmenu_item_type: Menu Item Typemenu_item_basic_details: Basic Detailslink_settings: Link Settingslink_to: Link To+ new_menu: New Menu+ new_menu_item: New Menu Item+ item_type: Item Typeopen_link_in_new_window: Open this link in a new windowmenu_item_type_message: "Set your <b>Menu Item Type</b>, for more information about using existing Types or adding custom Types<a href='https://guides.spreecommerce.org/user/configuration/configuring_geography.html#zones' target='_blank' class='alert-link'>click here</a> to see the documentation"+ menus: Menus+ menu_items: Menu Items+ subtitle: Subtitleadministration: Administrationadvertise: Advertiseagree_to_privacy_policy: Agree to Privacy Policy@@ -706,7 +715,6 @@ en:confirm_password: Password Confirmationcontact_phone: Contact phonecontact_us: Contact us- content: Contentcontinue: Continuecontinue_as_guest: Continue as a guestcontinue_shopping: Continue shopping@@ -1033,7 +1041,6 @@ en:name: Namename_on_card: Name on cardname_or_sku: Name or SKU (enter at least first 3 characters of product name)- navigation: Navigationnav_bar:admin_panel: Admin Panelchange_country: Change country@@ -1682,6 +1689,7 @@ en:successfully_removed: ! '%{resource} has been successfully removed!'successfully_updated: ! '%{resource} has been successfully updated!'summary: Summary+ support: Supportsupported_currencies: Supported Currenciessupported_currencies_long: A comma separated list of supported currenciessupported_locales: Supported Localesdiff --git a/core/db/migrate/20210409172956_create_spree_menus_stores.rb b/core/db/migrate/20210409172956_create_spree_menus_stores.rbnew file mode 100644index 0000000000..046291b18d--- /dev/null+++ b/core/db/migrate/20210409172956_create_spree_menus_stores.rb@@ -0,0 +1,10 @@+class CreateSpreeMenusStores < ActiveRecord::Migration[6.0]+ def change+ create_table :spree_menus_stores, id: false do |t|+ t.belongs_to :menu+ t.belongs_to :store+ end++ add_index :spree_menus_stores, [:menu_id, :store_id], unique: true, name: 'menu_id_store_id_unique_index'+ end+end