Skip to content

armgit / spree

Public Imported from GitHub
0

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.

Add Spree::Menu and Spree::MenuItem

Matthew Kennedy0bf82661c320
diff --git a/backend/app/assets/images/backend-pages.svg b/backend/app/assets/images/backend-pages.svg
new file mode 100644
index 0000000000..5373e2541c
--- /dev/null
+++ b/backend/app/assets/images/backend-pages.svg
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
+<svg width="100%" height="100%" viewBox="0 0 512 512" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:2;">
+ <g id="_1" transform="matrix(1.19789,0,0,1.19789,-79.2207,-40.1571)">
+ <g transform="matrix(1,0,0,1,2.50441,-1.66961)">
+ <path d="M391.89,450L163,450C135.523,449.967 112.923,427.367 112.89,399.89L112.89,98.11C112.923,70.633 135.523,48.033 163,48L313.1,48C317.077,47.998 320.897,49.578 323.71,52.39L437.61,166.27C440.422,169.083 442.002,172.903 442,176.88L442,399.88C441.973,427.361 419.371,449.967 391.89,450ZM163,78C151.972,78.011 142.901,87.082 142.89,98.11L142.89,399.89C142.901,410.918 151.972,419.989 163,420L391.89,420C402.918,419.989 411.989,410.918 412,399.89L412,183.09L306.91,78L163,78Z" style="fill:currentColor;"/>
+ </g>
+ <path d="M427,191.88L348.23,191.88C320.753,191.847 298.153,169.247 298.12,141.77L298.12,63C298.12,54.771 304.891,48 313.12,48C321.349,48 328.12,54.771 328.12,63L328.12,141.77C328.131,152.798 337.202,161.869 348.23,161.88L427,161.88C435.229,161.88 442,168.651 442,176.88C442,185.109 435.229,191.88 427,191.88Z" style="fill:currentColor;"/>
+ </g>
+</svg>
diff --git a/backend/app/assets/javascripts/spree/backend.js b/backend/app/assets/javascripts/spree/backend.js
index dc10a9c565..76ed9d74bd 100644
--- a/backend/app/assets/javascripts/spree/backend.js
+++ b/backend/app/assets/javascripts/spree/backend.js
@@ -43,6 +43,7 @@
//= require spree/backend/product_picker
//= require spree/backend/progress
//= require spree/backend/promotions
+//= require spree/backend/navigation/menu_item
//= require spree/backend/returns/expedited_exchanges_warning
//= require spree/backend/returns/return_item_selection
//= require spree/backend/shipments
diff --git a/backend/app/assets/javascripts/spree/backend/navigation/menu_item.es6 b/backend/app/assets/javascripts/spree/backend/navigation/menu_item.es6
new file mode 100644
index 0000000000..f114212e51
--- /dev/null
+++ b/backend/app/assets/javascripts/spree/backend/navigation/menu_item.es6
@@ -0,0 +1,24 @@
+document.addEventListener('DOMContentLoaded', function() {
+ $('#LinkToSelect2').select2()
+
+ $('#LinkToSelect2').on('change', function() {
+ const selectedLinkTo = $('#LinkToSelect2').val()
+ const message = document.getElementById('alertToClickUpdate')
+ const activePanel = document.getElementById('linkResourcePanel')
+ const panelType = activePanel.dataset.panelType
+
+ if (selectedLinkTo === panelType) {
+ activePanel.classList = ''
+ activePanel.classList.add('d-block')
+
+ message.classList = ''
+ message.classList.add('d-none')
+ } else {
+ activePanel.classList = ''
+ activePanel.classList.add('d-none')
+
+ message.classList = ''
+ message.classList.add('d-block')
+ }
+ });
+})
diff --git a/backend/app/controllers/spree/admin/menu_items_controller.rb b/backend/app/controllers/spree/admin/menu_items_controller.rb
new file mode 100644
index 0000000000..66647d24d6
--- /dev/null
+++ b/backend/app/controllers/spree/admin/menu_items_controller.rb
@@ -0,0 +1,17 @@
+module Spree
+ module Admin
+ class MenuItemsController < ResourceController
+ belongs_to 'spree/menu'
+
+ def index; end
+
+ def collection_url
+ spree.edit_admin_menu_path(@menu)
+ end
+
+ def location_after_save
+ spree.edit_admin_menu_menu_item_path(@menu, @menu_item)
+ end
+ end
+ end
+end
diff --git a/backend/app/controllers/spree/admin/menus_controller.rb b/backend/app/controllers/spree/admin/menus_controller.rb
new file mode 100644
index 0000000000..e4e808b524
--- /dev/null
+++ b/backend/app/controllers/spree/admin/menus_controller.rb
@@ -0,0 +1,9 @@
+module Spree
+ module Admin
+ class MenusController < ResourceController
+ def index
+ @menus = Spree::Menu.all
+ end
+ end
+ end
+end
diff --git a/backend/app/helpers/spree/admin/menu_helper.rb b/backend/app/helpers/spree/admin/menu_helper.rb
new file mode 100644
index 0000000000..3a570da733
--- /dev/null
+++ b/backend/app/helpers/spree/admin/menu_helper.rb
@@ -0,0 +1,7 @@
+module Spree
+ module Admin
+ module MenuHelper
+
+ end
+ end
+end
diff --git a/backend/app/views/spree/admin/menu_items/_form.html.erb b/backend/app/views/spree/admin/menu_items/_form.html.erb
new file mode 100644
index 0000000000..19d0ac24c8
--- /dev/null
+++ b/backend/app/views/spree/admin/menu_items/_form.html.erb
@@ -0,0 +1,89 @@
+<div data-hook="admin_menu_item_form_fields">
+ <div class="row">
+ <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>
+ </div>
+ <div class="card-body">
+ <%= f.field_container :item_type, class: ['form-group'] do %>
+ <%= f.label :item_type, Spree.t(:item_type) %>
+ <%= f.select(:item_type, Spree::MenuItem::ITEM_TYPE, {include_blank: false}, { class: 'select2' }) %>
+ <%= f.error_message_on :item_type %>
+ <% end %>
+ </div>
+
+ <div class="alert alert-info mx-3 mt-0">
+ <%= raw Spree.t('admin.navigation.menu_item_type_message') %>
+ </div>
+ </div>
+ </div>
+
+ <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>
+ </div>
+ <div class="card-body">
+ <%= f.field_container :name, class: ['form-group'] do %>
+ <%= 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 :subtitle, class: ['form-group'] do %>
+ <%= f.label :subtitle, Spree.t(:subtitle) %>
+ <%= f.text_field :subtitle, class: 'form-control' %>
+ <%= f.error_message_on :subtitle %>
+ <% end %>
+
+ <!-- TODO: ADD LINK TO UPLOAD IMAGE / ICON HERE -->
+ </div>
+ </div>
+ </div>
+ </div>
+
+ <div class="row">
+ <div class="col-12">
+ <div class="card mb-3">
+ <div class="card-header">
+ <h5 class="mb-0"><%= Spree.t('admin.navigation.link_settings') %></h5>
+ </div>
+ <div class="card-body">
+ <div class="row">
+ <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.error_message_on :linked_resource_type %>
+ <% end %>
+ </div>
+ <div class="col-6">
+ <div id="linkResourcePanel" data-panel-type="URL">
+ <!-- TODO: THIS SECTION NEEDS TO BE DYNAMIC BASED OFF THE [LINK TO] SELECTOR -->
+ <%= f.field_container :url, class: ['form-group'] do %>
+ <%= f.label :url, Spree.t(:url) %>
+ <%= f.text_field :url, class: 'form-control' %>
+ <%= f.error_message_on :url %>
+ <% end %>
+
+ <%= f.field_container :new_window do %>
+ <div class="custom-control custom-checkbox">
+ <%= f.check_box :new_window, class: 'custom-control-input' %>
+ <%= f.label :new_window, Spree.t('admin.navigation.open_link_in_new_window'), class: 'custom-control-label' %>
+ <%= f.error_message_on :new_window %>
+ </div>
+ <% end %>
+ </div>
+ <div id="alertToClickUpdate" class="d-none">
+ <div class="alert alert-warning mt-3">
+ Click update to load your new link
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+</div>
diff --git a/backend/app/views/spree/admin/menu_items/edit.html.erb b/backend/app/views/spree/admin/menu_items/edit.html.erb
new file mode 100644
index 0000000000..1c4d60bc20
--- /dev/null
+++ b/backend/app/views/spree/admin/menu_items/edit.html.erb
@@ -0,0 +1,10 @@
+<% content_for :page_title do %>
+ <%= link_to @menu.name, spree.edit_admin_menu_path(@menu) %> / <%= @menu_item.name %>
+<% end %>
+
+<%= render partial: 'spree/admin/shared/error_messages', locals: { target: @menu_items } %>
+
+<%= form_for [:admin, @menu, @menu_item] do |f| %>
+ <%= render partial: 'form', locals: { f: f } %>
+ <%= render partial: 'spree/admin/shared/edit_resource_links' %>
+<% end %>
diff --git a/backend/app/views/spree/admin/menu_items/new.html.erb b/backend/app/views/spree/admin/menu_items/new.html.erb
new file mode 100644
index 0000000000..54c6cb257c
--- /dev/null
+++ b/backend/app/views/spree/admin/menu_items/new.html.erb
@@ -0,0 +1,10 @@
+<% content_for :page_title do %>
+ <%= link_to @menu.name, spree.edit_admin_menu_path(@menu) %> / <%= Spree.t(:new_menu_item) %>
+<% end %>
+
+<%= render partial: 'spree/admin/shared/error_messages', locals: { target: @menu_items } %>
+
+<%= form_for [:admin, @menu, @menu_item] do |f| %>
+ <%= render partial: 'form', locals: { f: f } %>
+ <%= render partial: 'spree/admin/shared/new_resource_links' %>
+<% end %>
diff --git a/backend/app/views/spree/admin/menus/_form.html.erb b/backend/app/views/spree/admin/menus/_form.html.erb
new file mode 100644
index 0000000000..b08977a2d1
--- /dev/null
+++ b/backend/app/views/spree/admin/menus/_form.html.erb
@@ -0,0 +1,9 @@
+<div data-hook="admin_menu_form_fields" class="row">
+ <div class="col-12">
+ <%= 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>
diff --git a/backend/app/views/spree/admin/menus/edit.html.erb b/backend/app/views/spree/admin/menus/edit.html.erb
new file mode 100644
index 0000000000..78b2c33e61
--- /dev/null
+++ b/backend/app/views/spree/admin/menus/edit.html.erb
@@ -0,0 +1,44 @@
+<% content_for :page_title do %>
+ <%= link_to Spree.t(: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' %>
+<% 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' %>
+<% 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>
+<% end %>
diff --git a/backend/app/views/spree/admin/menus/index.html.erb b/backend/app/views/spree/admin/menus/index.html.erb
new file mode 100644
index 0000000000..0c3407ad7b
--- /dev/null
+++ b/backend/app/views/spree/admin/menus/index.html.erb
@@ -0,0 +1,35 @@
+<% content_for :page_title do %>
+ <%= Spree.t('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' } %>
+<% end if can?(:create, Spree::Menu) %>
+
+<% if @menus.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>
+ <% @menus.each do |menu| %>
+ <tr>
+ <td><%= menu.name %></td>
+ <td class="actions" data-hook="admin_menus_index_row_actions">
+ <span class="d-flex justify-content-end">
+ <%= link_to_edit(menu, no_text: true) %>
+ <%= link_to_delete menu, no_text: true, url: spree.admin_menu_path(menu) if can?(:destroy, menu) %>
+ </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::Menu)) %>
+ </div>
+<% end %>
diff --git a/backend/app/views/spree/admin/menus/new.html.erb b/backend/app/views/spree/admin/menus/new.html.erb
new file mode 100644
index 0000000000..2eddd888fa
--- /dev/null
+++ b/backend/app/views/spree/admin/menus/new.html.erb
@@ -0,0 +1,10 @@
+<% content_for :page_title do %>
+ <%= link_to Spree.t(:menus), spree.admin_menus_url %> / <%= Spree.t(:new_menu) %>
+<% 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/new_resource_links' %>
+<% end %>
diff --git a/backend/app/views/spree/admin/shared/_main_menu.html.erb b/backend/app/views/spree/admin/shared/_main_menu.html.erb
index f56f98b8fd..c43f828429 100644
--- a/backend/app/views/spree/admin/shared/_main_menu.html.erb
+++ b/backend/app/views/spree/admin/shared/_main_menu.html.erb
@@ -46,6 +46,13 @@
</ul>
<% end %>
+ <% 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" %>
+ </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.erb
new file mode 100644
index 0000000000..7e551a7dd2
--- /dev/null
+++ b/backend/app/views/spree/admin/shared/sub_menu/_content.html.erb
@@ -0,0 +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') %>
+</ul>
diff --git a/backend/config/routes.rb b/backend/config/routes.rb
index 928fa9a8c0..615897a1d4 100644
--- a/backend/config/routes.rb
+++ b/backend/config/routes.rb
@@ -187,6 +187,10 @@ Spree::Core::Engine.add_routes do
end
resources :store_credits
end
+
+ resources :menus do
+ resources :menu_items
+ end
end
spree_path = Rails.application.routes.url_helpers.try(:spree_path, trailing_slash: true) || '/'
diff --git a/core/app/models/spree/menu.rb b/core/app/models/spree/menu.rb
new file mode 100644
index 0000000000..e510ab6267
--- /dev/null
+++ b/core/app/models/spree/menu.rb
@@ -0,0 +1,6 @@
+module Spree
+ class Menu < Spree::Base
+ has_many :menu_items, dependent: :destroy
+ validates :name, presence: true
+ end
+end
diff --git a/core/app/models/spree/menu_item.rb b/core/app/models/spree/menu_item.rb
new file mode 100644
index 0000000000..acfffeed0a
--- /dev/null
+++ b/core/app/models/spree/menu_item.rb
@@ -0,0 +1,18 @@
+module Spree
+ class MenuItem < Spree::Base
+ belongs_to :menu
+
+ # Not frozen so they can be added to
+ ITEM_TYPE = %w[Link Promotion]
+ LINKED_RESOURCE_TYPE = ['Home Page']
+ STATIC_RESOURCE_TYPE = ['URL']
+ DYNAMIC_RESOURCE_TYPE = ['Spree::Product', 'Spree::Taxon', 'Spree::Page']
+
+ LINKED_RESOURCE_TYPE.unshift(*STATIC_RESOURCE_TYPE)
+ LINKED_RESOURCE_TYPE.push(*DYNAMIC_RESOURCE_TYPE)
+
+ validates :name, presence: true
+ validates :item_type, inclusion: { in: ITEM_TYPE }
+ validates :linked_resource_type, inclusion: { in: LINKED_RESOURCE_TYPE }
+ end
+end
diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml
index a84da6c25c..f318e3172f 100644
--- a/core/config/locales/en.yml
+++ b/core/config/locales/en.yml
@@ -571,6 +571,15 @@ en:
store_credits: Store Credits
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'
+ menu_item_type: Menu Item Type
+ menu_item_basic_details: Basic Details
+ link_settings: Link Settings
+ link_to: Link To
+ open_link_in_new_window: Open this link in a new window
+ menu_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"
administration: Administration
advertise: Advertise
agree_to_privacy_policy: Agree to Privacy Policy
@@ -697,6 +706,7 @@ en:
confirm_password: Password Confirmation
contact_phone: Contact phone
contact_us: Contact us
+ content: Content
continue: Continue
continue_as_guest: Continue as a guest
continue_shopping: Continue shopping
@@ -1023,6 +1033,7 @@ en:
name: Name
name_on_card: Name on card
name_or_sku: Name or SKU (enter at least first 3 characters of product name)
+ navigation: Navigation
nav_bar:
admin_panel: Admin Panel
change_country: Change country
diff --git a/core/db/migrate/20210407200948_create_spree_menus.rb b/core/db/migrate/20210407200948_create_spree_menus.rb
new file mode 100644
index 0000000000..c237ef7107
--- /dev/null
+++ b/core/db/migrate/20210407200948_create_spree_menus.rb
@@ -0,0 +1,9 @@
+class CreateSpreeMenus < ActiveRecord::Migration[6.0]
+ def change
+ create_table :spree_menus do |t|
+ t.column :name, :string
+
+ t.timestamps
+ end
+ end
+end
diff --git a/core/db/migrate/20210408092939_create_spree_menu_items.rb b/core/db/migrate/20210408092939_create_spree_menu_items.rb
new file mode 100644
index 0000000000..9b7cc67b98
--- /dev/null
+++ b/core/db/migrate/20210408092939_create_spree_menu_items.rb
@@ -0,0 +1,18 @@
+class CreateSpreeMenuItems < ActiveRecord::Migration[6.0]
+ def change
+ create_table :spree_menu_items do |t|
+ t.column :name, :string
+ t.column :subtitle, :string
+ t.column :url, :string
+ t.column :new_window, :boolean, default: false
+ t.column :item_type, :string
+ t.column :linked_resource_type, :string
+ t.column :linked_resource_id, :integer
+ t.column :position, :integer, default: 0
+ t.column :parent_id, :integer, default: 0
+ t.belongs_to :menu
+
+ t.timestamps
+ end
+ end
+end