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 ability to select Product or Taxon and generate destination output.

Matthew Kennedy669718aff43c
diff --git a/backend/app/assets/javascripts/spree/backend/navigation/menu.es6 b/backend/app/assets/javascripts/spree/backend/navigation/menu.es6
index 787d71e0aa..c4e95373dd 100644
--- a/backend/app/assets/javascripts/spree/backend/navigation/menu.es6
+++ b/backend/app/assets/javascripts/spree/backend/navigation/menu.es6
@@ -15,7 +15,7 @@ document.addEventListener('DOMContentLoaded', function() {
animation: 350,
forceFallback: true,
onEnd: function (evt) {
- handleMenuMove(evt)
+ handleMenuItemMove(evt)
}
}
@@ -27,7 +27,7 @@ document.addEventListener('DOMContentLoaded', function() {
}
})
-function handleMenuMove (evt) {
+function handleMenuItemMove (evt) {
const newParentId = evt.to.dataset.parentId
const position = evt.newIndex
diff --git a/backend/app/assets/stylesheets/spree/backend/shared/_sortable_js_tree.scss b/backend/app/assets/stylesheets/spree/backend/shared/_sortable_js_tree.scss
index 66d91181b6..9c41513978 100644
--- a/backend/app/assets/stylesheets/spree/backend/shared/_sortable_js_tree.scss
+++ b/backend/app/assets/stylesheets/spree/backend/shared/_sortable_js_tree.scss
@@ -3,11 +3,14 @@ $main-items-border-color: rgba(0, 0, 0, 0.125);
$nestable-background-color: rgb(214, 214, 214);
#menuArea {
- .space-buttons a {
- margin: 2px;
+ .space-buttons {
+ box-shadow: -11px 0 16px 6px white;
+ a {
+ margin: 2px;
+ }
}
-
.container-item-row {
+ overflow: hidden;
border: 1px solid $main-items-border-color;
margin: 2px;
background-color: white;
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
index af537053a5..d18a0e5cd8 100644
--- a/backend/app/views/spree/admin/menu_items/_link_input.html.erb
+++ b/backend/app/views/spree/admin/menu_items/_link_input.html.erb
@@ -1,9 +1,12 @@
<div id="linkResourcePanel" data-panel-type="<%= linked_resource %>">
- <% if linked_resource == 'URL' %>
+ <% case linked_resource %>
+ <% when '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 %>
- <%= render 'spree/admin/menu_items/link_inputs/search', linked_resource: linked_resource, f: f %>
+ <% when 'Home Page' %>
+ <%= render 'spree/admin/menu_items/link_inputs/home_page', f: f %>
+ <% when 'Taxon' %>
+ <%= render 'spree/admin/menu_items/link_inputs/taxon', linked_resource: linked_resource, f: f %>
+ <% when 'Product' %>
+ <%= render 'spree/admin/menu_items/link_inputs/product', linked_resource: linked_resource, f: f %>
<% end %>
</div>
diff --git a/backend/app/views/spree/admin/menu_items/link_inputs/_product.html.erb b/backend/app/views/spree/admin/menu_items/link_inputs/_product.html.erb
new file mode 100644
index 0000000000..14b47af16e
--- /dev/null
+++ b/backend/app/views/spree/admin/menu_items/link_inputs/_product.html.erb
@@ -0,0 +1,6 @@
+<%= f.field_container :linked_resource_id, class: ['form-group'] do %>
+ <%= f.label :linked_resource_id, Spree.t('product') %>
+ <%= f.select :linked_resource_id, options_from_collection_for_select(Spree::Product.all, :id, :name, @menu_item.linked_resource_id), {include_blank: true}, multiple: false, class: 'select2-clear'%>
+ <%= f.error_message_on :linked_resource_id %>
+ <small class="form-text text-muted"><%= @menu_item.destination %></small>
+<% end %>
diff --git a/backend/app/views/spree/admin/menu_items/link_inputs/_search.html.erb b/backend/app/views/spree/admin/menu_items/link_inputs/_search.html.erb
deleted file mode 100644
index 78e0b24cd1..0000000000
--- a/backend/app/views/spree/admin/menu_items/link_inputs/_search.html.erb
+++ /dev/null
@@ -1,5 +0,0 @@
- <%= f.field_container :url, class: ['form-group'] do %>
- <%= f.label :url, Spree.t('seach') + ': ' +linked_resource %>
- <%= f.text_field :url, class: 'form-control' %>
- <%= f.error_message_on :url %>
- <% end %>
diff --git a/backend/app/views/spree/admin/menu_items/link_inputs/_taxon.html.erb b/backend/app/views/spree/admin/menu_items/link_inputs/_taxon.html.erb
new file mode 100644
index 0000000000..cb5daee3ec
--- /dev/null
+++ b/backend/app/views/spree/admin/menu_items/link_inputs/_taxon.html.erb
@@ -0,0 +1,6 @@
+<%= f.field_container :linked_resource_id, class: ['form-group'] do %>
+ <%= f.label :linked_resource_id, Spree.t('taxon') %>
+ <%= f.select :linked_resource_id, options_from_collection_for_select(Spree::Taxon.all, :id, :pretty_name, @menu_item.linked_resource_id), {include_blank: true}, multiple: false, class: 'select2-clear' %>
+ <%= f.error_message_on :linked_resource_id %>
+ <small class="form-text text-muted"><%= @menu_item.destination %></small>
+<% end %>
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
index 88e2602f9e..160f8476fb 100644
--- 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
@@ -2,6 +2,7 @@
<%= f.field_container :url, class: ['form-group'] do %>
<%= f.label :url, Spree.t(:url) %>
<%= f.text_field :url, class: 'form-control' %>
+ <small class="form-text text-muted"><%= @menu_item.destination %></small>
<%= f.error_message_on :url %>
<% end %>
diff --git a/backend/app/views/spree/admin/menus/edit.html.erb b/backend/app/views/spree/admin/menus/edit.html.erb
index 7083b01e35..375b423b7b 100644
--- a/backend/app/views/spree/admin/menus/edit.html.erb
+++ b/backend/app/views/spree/admin/menus/edit.html.erb
@@ -38,11 +38,17 @@
<div class="card-body p-0 bg-light">
<div id="primaryMenuContainer" class="menu-container">
+ <% if @menu_items_in_order.any? %>
<% nested_set_options(@menu_items_in_order) do |item| %>
<% if item.root? %>
<%= build_menu_item(@menu, item) %>
<% end %>
<% 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 %>
</div>
</div>
</div>
diff --git a/core/app/models/spree/menu_item.rb b/core/app/models/spree/menu_item.rb
index a1c6d4196f..9a06ffdc8e 100644
--- a/core/app/models/spree/menu_item.rb
+++ b/core/app/models/spree/menu_item.rb
@@ -4,16 +4,19 @@ module Spree
acts_as_nested_set dependent: :destroy
+ before_save :reset_link_attributes
+
after_save :touch_ancestors
after_touch :touch_ancestors
has_one_attached :image_asset
# Not frozen so they can be added to
- ITEM_TYPE = %w[Link Promotion]
+ ITEM_TYPE = %w[Link Promotion Container]
+
LINKED_RESOURCE_TYPE = []
STATIC_RESOURCE_TYPE = ['URL', 'Home Page']
- DYNAMIC_RESOURCE_TYPE = ['Spree::Product', 'Spree::Taxon', 'Spree::Page']
+ DYNAMIC_RESOURCE_TYPE = ['Product', 'Taxon']
LINKED_RESOURCE_TYPE.unshift(*STATIC_RESOURCE_TYPE)
LINKED_RESOURCE_TYPE.push(*DYNAMIC_RESOURCE_TYPE)
@@ -22,20 +25,37 @@ module Spree
validates :item_type, inclusion: { in: ITEM_TYPE }
validates :linked_resource_type, inclusion: { in: LINKED_RESOURCE_TYPE }
- def cached_self_and_descendants_ids
- Rails.cache.fetch("#{cache_key_with_version}/descendant-ids") do
- self_and_descendants.ids
+ def destination
+ if DYNAMIC_RESOURCE_TYPE.include? linked_resource_type
+ return if linked_resource_id.nil?
+
+ case linked_resource_type
+ when 'Taxon'
+ permalink = Spree::Taxon.find(linked_resource_id).permalink || 'summer'
+ Spree::Core::Engine.routes.url_helpers.nested_taxons_path(permalink)
+ when 'Product'
+ product = Spree::Product.find(linked_resource_id)
+ Spree::Core::Engine.routes.url_helpers.product_path(product)
+ end
+ else
+ case linked_resource_type
+ when 'URL'
+ url
+ when 'Home Page'
+ Spree::Core::Engine.routes.url_helpers.root_path
+ end
end
end
- def cached_self_and_descendants
- Rails.cache.fetch("#{cache_key_with_version}/descendant-ids") do
- self_and_descendants
+ private
+
+ def reset_link_attributes
+ if linked_resource_type_changed?
+ self.linked_resource_id = nil
+ self.url = nil
end
end
- private
-
def touch_ancestors
ancestors.update_all(updated_at: Time.current)
end
diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml
index ecb7c585d8..a29340b88d 100644
--- a/core/config/locales/en.yml
+++ b/core/config/locales/en.yml
@@ -583,6 +583,7 @@ en:
link_to: Link To
new_menu: New Menu
new_menu_item: New Menu Item
+ no_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.
item_type: Item Type
open_link_in_new_window: Open this link in a new window
diff --git a/sample/db/samples/menu_items.rb b/sample/db/samples/menu_items.rb
new file mode 100644
index 0000000000..457271986d
--- /dev/null
+++ b/sample/db/samples/menu_items.rb
@@ -0,0 +1,234 @@
+main_menu = Spree::Menu.find_by!(name: 'Main Menu')
+
+##############
+# Root Items #
+##############
+main_menu_root_women = Spree::MenuItem.where(
+ name: 'Women',
+ item_type: 'Link',
+ menu_id: main_menu
+).first_or_create!
+
+main_menu_root_men = Spree::MenuItem.where(
+ name: 'Men',
+ item_type: 'Link',
+ menu_id: main_menu
+).first_or_create!
+
+main_menu_root_sw = Spree::MenuItem.where(
+ name: 'Sportsware',
+ item_type: 'Link',
+ menu_id: main_menu
+).first_or_create!
+
+##############
+# Catagories #
+##############
+main_menu_cat_women = Spree::MenuItem.where(
+ name: 'Catagories',
+ item_type: 'Container',
+ menu_id: main_menu,
+ parent_id: main_menu_root_women
+).first_or_create!
+
+main_menu_cat_men = Spree::MenuItem.where(
+ name: 'Catagories',
+ item_type: 'Container',
+ menu_id: main_menu,
+ parent_id: main_menu_root_men
+).first_or_create!
+
+main_menu_cat_sw = Spree::MenuItem.where(
+ name: 'Catagories',
+ item_type: 'Container',
+ menu_id: main_menu,
+ parent_id: main_menu_root_sw
+).first_or_create!
+
+##############
+# Promotions #
+##############
+main_menu_promo_women = Spree::MenuItem.where(
+ name: 'Promos',
+ item_type: 'Container',
+ menu_id: main_menu,
+ parent_id: main_menu_root_women
+).first_or_create!
+
+main_menu_promo_men = Spree::MenuItem.where(
+ name: 'Promos',
+ item_type: 'Container',
+ menu_id: main_menu,
+ parent_id: main_menu_root_men
+).first_or_create!
+
+main_menu_promo_sw = Spree::MenuItem.where(
+ name: 'Promos',
+ item_type: 'Container',
+ menu_id: main_menu,
+ parent_id: main_menu_root_sw
+).first_or_create!
+
+PROMOS = [main_menu_promo_women, main_menu_promo_men, main_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|
+ Spree::MenuItem.where(
+ name: 'New Collection',
+ subtitle: "Summer #{Date.today.year}",
+ linked_resource_type: 'Taxon',
+ linked_resource_id: summer.id,
+ item_type: 'Promotion',
+ menu_id: main_menu,
+ parent_id: promo
+ ).first_or_create!
+
+ Spree::MenuItem.where(
+ name: 'Special Offers',
+ subtitle: 'Get up to 30% OFF',
+ linked_resource_type: 'Taxon',
+ linked_resource_id: offers.id,
+ item_type: 'Promotion',
+ menu_id: main_menu,
+ parent_id: promo
+ ).first_or_create!
+end
+
+#################################
+# Links For: WOMEN / CATAGORIES #
+#################################
+Spree::MenuItem.where(
+ name: 'Skirts',
+ linked_resource_type: 'URL',
+ url: '/t/women/skirts',
+ item_type: 'Link',
+ menu_id: main_menu,
+ parent_id: main_menu_cat_women
+).first_or_create!
+
+Spree::MenuItem.where(
+ name: 'Dresses',
+ linked_resource_type: 'URL',
+ url: '/t/women/dresses',
+ item_type: 'Link',
+ menu_id: main_menu,
+ parent_id: main_menu_cat_women
+).first_or_create!
+
+Spree::MenuItem.where(
+ name: 'Shirts And Blouses',
+ linked_resource_type: 'URL',
+ url: '/t/women/shirts-and-blouses',
+ item_type: 'Link',
+ menu_id: main_menu,
+ parent_id: main_menu_cat_women
+).first_or_create!
+
+Spree::MenuItem.where(
+ name: 'Shirts And Blouses',
+ linked_resource_type: 'URL',
+ url: '/t/women/shirts-and-blouses',
+ item_type: 'Link',
+ menu_id: main_menu,
+ parent_id: main_menu_cat_women
+).first_or_create!
+
+Spree::MenuItem.where(
+ name: 'Sweaters',
+ linked_resource_type: 'URL',
+ url: '/t/women/sweaters',
+ item_type: 'Link',
+ menu_id: main_menu,
+ parent_id: main_menu_cat_women
+).first_or_create!
+
+Spree::MenuItem.where(
+ name: 'Tops and T-Shirts',
+ linked_resource_type: 'URL',
+ url: '/t/women/tops-and-t-shirts',
+ item_type: 'Link',
+ menu_id: main_menu,
+ parent_id: main_menu_cat_women
+).first_or_create!
+
+Spree::MenuItem.where(
+ name: 'Jackets and Coats',
+ linked_resource_type: 'URL',
+ url: '/t/women/jackets-and-coats',
+ item_type: 'Link',
+ menu_id: main_menu,
+ parent_id: main_menu_cat_women
+).first_or_create!
+
+###############################
+# Links For: MEN / CATAGORIES #
+###############################
+Spree::MenuItem.where(
+ name: 'Shirts',
+ linked_resource_type: 'URL',
+ url: '/t/men/shirts',
+ item_type: 'Link',
+ menu_id: main_menu,
+ parent_id: main_menu_cat_men
+).first_or_create!
+
+Spree::MenuItem.where(
+ name: 'T-Shirts',
+ linked_resource_type: 'URL',
+ url: '/t/men/t-shirts',
+ item_type: 'Link',
+ menu_id: main_menu,
+ parent_id: main_menu_cat_men
+).first_or_create!
+
+Spree::MenuItem.where(
+ name: 'Sweaters',
+ linked_resource_type: 'URL',
+ url: '/t/men/sweaters',
+ item_type: 'Link',
+ menu_id: main_menu,
+ parent_id: main_menu_cat_men
+).first_or_create!
+
+Spree::MenuItem.where(
+ name: 'Jackets and Coats',
+ linked_resource_type: 'URL',
+ url: '/t/men/jackets-and-coats',
+ item_type: 'Link',
+ menu_id: main_menu,
+ parent_id: main_menu_cat_men
+).first_or_create!
+
+######################################
+# Links For: SPORTSWARE / CATAGORIES #
+######################################
+Spree::MenuItem.where(
+ name: 'Tops',
+ linked_resource_type: 'URL',
+ url: '/t/sportswear/tops',
+ item_type: 'Link',
+ menu_id: main_menu,
+ parent_id: main_menu_cat_sw
+).first_or_create!
+
+Spree::MenuItem.where(
+ name: 'Sweatshirts',
+ linked_resource_type: 'URL',
+ url: '/t/sportswear/sweatshirts',
+ item_type: 'Link',
+ menu_id: main_menu,
+ parent_id: main_menu_cat_sw
+).first_or_create!
+
+Spree::MenuItem.where(
+ name: 'Pants',
+ linked_resource_type: 'URL',
+ url: '/t/sportswear/pants',
+ item_type: 'Link',
+ menu_id: main_menu,
+ parent_id: main_menu_cat_sw
+).first_or_create!
diff --git a/sample/db/samples/menus.rb b/sample/db/samples/menus.rb
index 57ebf31db8..7d2351b83b 100644
--- a/sample/db/samples/menus.rb
+++ b/sample/db/samples/menus.rb
@@ -1,8 +1,13 @@
-menus = [
- { name: 'Main Menu' },
- { name: 'Footer' }
-]
+main_menu = Spree::Menu.where(
+ name: 'Main Menu'
+).first_or_create!
-menus.each do |menu_attrs|
- Spree::Menu.where(menu_attrs).first_or_create!
-end
+main_menu.store_ids = Spree::Store.ids
+main_menu.save!
+
+footer_menu = Spree::Menu.where(
+ name: 'Footer Menu'
+).first_or_create!
+
+footer_menu.store_ids = Spree::Store.ids
+footer_menu.save!
diff --git a/sample/lib/spree_sample.rb b/sample/lib/spree_sample.rb
index 7cb9e9f258..78e3a94ad6 100644
--- a/sample/lib/spree_sample.rb
+++ b/sample/lib/spree_sample.rb
@@ -7,7 +7,6 @@ module SpreeSample
# Needs to be here so we can access it inside the tests
def self.load_samples
- Spree::Sample.load_sample('menus')
Spree::Sample.load_sample('addresses')
Spree::Sample.load_sample('zones')
Spree::Sample.load_sample('payment_methods')
@@ -17,6 +16,7 @@ module SpreeSample
Spree::Sample.load_sample('tax_rates')
Spree::Sample.load_sample('taxonomies')
Spree::Sample.load_sample('promotions')
+ Spree::Sample.load_sample('menus')
Spree::Sample.load_sample('products')
Spree::Sample.load_sample('taxons')
@@ -26,6 +26,7 @@ module SpreeSample
Spree::Sample.load_sample('prototypes')
Spree::Sample.load_sample('variants')
Spree::Sample.load_sample('stock')
+ Spree::Sample.load_sample('menu_items')
Spree::Sample.load_sample('orders')
Spree::Sample.load_sample('adjustments')