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 link switcher, and use Awesome Nested Set

Matthew Kennedye68be7c8f4da
diff --git a/backend/app/views/spree/admin/menu_items/_form.html.erb b/backend/app/views/spree/admin/menu_items/_form.html.erb
index 4eb39ba407..e7a4ce07b2 100644
--- a/backend/app/views/spree/admin/menu_items/_form.html.erb
+++ b/backend/app/views/spree/admin/menu_items/_form.html.erb
@@ -54,27 +54,14 @@
<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, humanize_class_name(Spree::MenuItem::LINKED_RESOURCE_TYPE), {include_blank: false}, { class: 'selec2', id: 'LinkToSelect2' }) %>
+ <%= 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>
+ <%= render 'link_input', linked_resource: @menu_item.linked_resource_type, f: f %>
+
<div id="alertToClickUpdate" class="d-none">
<div class="alert alert-warning mt-3">
Click update to load your new link
diff --git a/backend/app/views/spree/admin/menu_items/_link_input.html.erb b/backend/app/views/spree/admin/menu_items/_link_input.html.erb
new file mode 100644
index 0000000000..d204511a17
--- /dev/null
+++ b/backend/app/views/spree/admin/menu_items/_link_input.html.erb
@@ -0,0 +1,8 @@
+<div id="linkResourcePanel" data-panel-type="<%= linked_resource %>">
+ <% if linked_resource == 'URL' %>
+ <%= render 'spree/admin/menu_items/link_inputs/url', f: f %>
+ <% elsif linked_resource == 'Home Page' %>
+ <%= render 'spree/admin/menu_items/link_inputs/home_page', f: f %>
+ <% else %>
+<% end %>
+</div>
diff --git a/backend/app/views/spree/admin/menu_items/link_inputs/_home_page.html.erb b/backend/app/views/spree/admin/menu_items/link_inputs/_home_page.html.erb
new file mode 100644
index 0000000000..49b0261cac
--- /dev/null
+++ b/backend/app/views/spree/admin/menu_items/link_inputs/_home_page.html.erb
@@ -0,0 +1,3 @@
+<div class="alert alert-info mt-3">
+ This link takes you to your stores home page.
+</div>
diff --git a/backend/app/views/spree/admin/menu_items/link_inputs/_url.html.erb b/backend/app/views/spree/admin/menu_items/link_inputs/_url.html.erb
new file mode 100644
index 0000000000..88e2602f9e
--- /dev/null
+++ b/backend/app/views/spree/admin/menu_items/link_inputs/_url.html.erb
@@ -0,0 +1,14 @@
+
+ <%= 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 %>
diff --git a/backend/app/views/spree/admin/menus/edit.html.erb b/backend/app/views/spree/admin/menus/edit.html.erb
index 5520885025..dcbe3d496b 100644
--- a/backend/app/views/spree/admin/menus/edit.html.erb
+++ b/backend/app/views/spree/admin/menus/edit.html.erb
@@ -19,7 +19,6 @@
<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">
@@ -32,35 +31,34 @@
</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>
-
+ <% 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 %>
</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>
</div>
diff --git a/core/app/models/spree/menu_item.rb b/core/app/models/spree/menu_item.rb
index acfffeed0a..00b83266d7 100644
--- a/core/app/models/spree/menu_item.rb
+++ b/core/app/models/spree/menu_item.rb
@@ -2,10 +2,12 @@ module Spree
class MenuItem < Spree::Base
belongs_to :menu
+ acts_as_nested_set
+
# Not frozen so they can be added to
ITEM_TYPE = %w[Link Promotion]
- LINKED_RESOURCE_TYPE = ['Home Page']
- STATIC_RESOURCE_TYPE = ['URL']
+ LINKED_RESOURCE_TYPE = []
+ STATIC_RESOURCE_TYPE = ['URL', 'Home Page']
DYNAMIC_RESOURCE_TYPE = ['Spree::Product', 'Spree::Taxon', 'Spree::Page']
LINKED_RESOURCE_TYPE.unshift(*STATIC_RESOURCE_TYPE)
diff --git a/core/db/migrate/20210408092939_create_spree_menu_items.rb b/core/db/migrate/20210408092939_create_spree_menu_items.rb
index 9b7cc67b98..424af8a55f 100644
--- a/core/db/migrate/20210408092939_create_spree_menu_items.rb
+++ b/core/db/migrate/20210408092939_create_spree_menu_items.rb
@@ -6,13 +6,19 @@ class CreateSpreeMenuItems < ActiveRecord::Migration[6.0]
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_type, :string, default: 'URL'
t.column :linked_resource_id, :integer
- t.column :position, :integer, default: 0
- t.column :parent_id, :integer, default: 0
+ t.column :parent_id, :integer
+ t.column :lft, :integer
+ t.column :rgt, :integer
+
t.belongs_to :menu
t.timestamps
end
+
+ add_index :spree_menu_items, [:parent_id]
+ add_index :spree_menu_items, [:lft]
+ add_index :spree_menu_items, [:rgt]
end
end