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.

Update to use destination

Matthew Kennedyf9098cab33e1
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
index ea9056ac9e..a32d2846bc 100644
--- 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
@@ -2,5 +2,5 @@
<%= f.label :linked_resource_id, Spree.t('product') %>
<%= f.select :linked_resource_id, options_from_collection_for_select(@products, :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_link_to(@menu_item) %></small>
+ <small class="form-text text-muted"><%= @menu_item.destination %></small>
<% 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
index 53e76eca8f..bcdbe66b99 100644
--- 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
@@ -2,5 +2,5 @@
<%= f.label :linked_resource_id, Spree.t('taxon') %>
<%= f.select :linked_resource_id, options_from_collection_for_select(@taxons, :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_link_to(@menu_item) %></small>
+ <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 ed3f2cf92f..b4acde8e29 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
@@ -1,7 +1,7 @@
-<%= 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 %>
+<%= f.field_container :destination, class: ['form-group'] do %>
+ <%= f.label :destination, Spree.t(:url) %>
+ <%= f.text_field :destination, class: 'form-control' %>
+ <%= f.error_message_on :destination %>
<% end %>
<%= f.field_container :new_window do %>
diff --git a/backend/app/views/spree/admin/menus/nested_menu_items/_item_bar.html.erb b/backend/app/views/spree/admin/menus/nested_menu_items/_item_bar.html.erb
index 45382e8ea0..8482225d65 100644
--- a/backend/app/views/spree/admin/menus/nested_menu_items/_item_bar.html.erb
+++ b/backend/app/views/spree/admin/menus/nested_menu_items/_item_bar.html.erb
@@ -6,8 +6,8 @@
<div class="d-flex align-items-center w-100 truncate">
<div>
<%= item.name %>
- <% if menu_item_link_to(item) != '#' %>
- <small class="text-muted mt-0">-></small> <span class="badge badge-light badge-light-muted-text"><%= menu_item_link_to(item) %></span>
+ <% if item.destination %>
+ <small class="text-muted mt-0">-></small> <span class="badge badge-light badge-light-muted-text"><%= item.destination %></span>
<% end %>
<small class="form-text text-muted mt-0">
<%= "#{Spree.t('admin.navigation.type')}: #{item.item_type}" %>
diff --git a/core/app/helpers/spree/base_helper.rb b/core/app/helpers/spree/base_helper.rb
index b4a17287dc..7bfad08180 100644
--- a/core/app/helpers/spree/base_helper.rb
+++ b/core/app/helpers/spree/base_helper.rb
@@ -25,27 +25,6 @@ module Spree
to_html
end
- def menu_item_link_to(item)
- if Spree::MenuItem::DYNAMIC_RESOURCE_TYPE.include? item.linked_resource_type
- return if item.path.nil?
-
- item.path
- else
- case item.linked_resource_type
- when 'URL'
- if item.url.present?
- item.url
- else
- '#'
- end
- when 'Home Page'
- '/'
- else
- '#'
- end
- end
- end
-
def display_compare_at_price(product_or_variant)
product_or_variant.
price_in(current_currency).
diff --git a/core/app/models/spree/menu_item.rb b/core/app/models/spree/menu_item.rb
index 55d4e7e7d8..84a6dcb6f1 100644
--- a/core/app/models/spree/menu_item.rb
+++ b/core/app/models/spree/menu_item.rb
@@ -33,38 +33,43 @@ module Spree
def build_path
if DYNAMIC_RESOURCE_TYPE.include? linked_resource_type
- return if linked_resource_id.nil?
+ return if linked_resource.nil?
case linked_resource_type
when 'Spree::Taxon'
- self.path = if frontend_available?
- Spree::Core::Engine.routes.url_helpers.nested_taxons_path(linked_resource.permalink)
- else
- "/#{Spree::Config[:api_taxon_path]}/#{linked_resource.permalink}"
- end
+ self.destination = if frontend_available?
+ Spree::Core::Engine.routes.url_helpers.nested_taxons_path(linked_resource.permalink)
+ else
+ "/#{Spree::Config[:api_taxon_path]}/#{linked_resource.permalink}"
+ end
when 'Spree::Product'
- self.path = if frontend_available?
- Spree::Core::Engine.routes.url_helpers.product_path(linked_resource)
- else
- "/#{Spree::Config[:api_product_path]}/#{linked_resource.slug}"
- end
+ self.destination = if frontend_available?
+ Spree::Core::Engine.routes.url_helpers.product_path(linked_resource)
+ else
+ "/#{Spree::Config[:api_product_path]}/#{linked_resource.slug}"
+ end
+
+ end
+ else
+ case linked_resource_type
+ when 'Home Page'
+ self.destination = '/'
end
end
end
def check_for_image
self.has_attached_image = if image_asset.attached?
- true
- else
- false
- end
+ true
+ else
+ false
+ end
end
def reset_link_attributes
if linked_resource_type_changed?
self.linked_resource_id = nil
- self.url = nil
- self.path = nil
+ self.destination = nil
self.new_window = false
end
end
diff --git a/core/db/migrate/20210408092939_create_spree_menu_items.rb b/core/db/migrate/20210408092939_create_spree_menu_items.rb
index 0dd2d24b67..1739e7b6c7 100644
--- a/core/db/migrate/20210408092939_create_spree_menu_items.rb
+++ b/core/db/migrate/20210408092939_create_spree_menu_items.rb
@@ -3,8 +3,7 @@ class CreateSpreeMenuItems < ActiveRecord::Migration[6.0]
create_table :spree_menu_items do |t|
t.column :name, :string, null: false
t.column :subtitle, :string
- t.column :url, :string
- t.column :path, :string
+ t.column :destination, :string
t.column :new_window, :boolean, default: false
t.column :has_attached_image, :boolean, default: false
t.column :item_type, :string
diff --git a/frontend/app/views/spree/shared/_footer.html.erb b/frontend/app/views/spree/shared/_footer.html.erb
index 5eed31d771..5a357fea1e 100644
--- a/frontend/app/views/spree/shared/_footer.html.erb
+++ b/frontend/app/views/spree/shared/_footer.html.erb
@@ -50,17 +50,19 @@
<div class="row d-none d-xl-flex border-top py-5 text-uppercase footer-spree-menu-links">
<% if menu_by_code('spree-all-footer').present? %>
- <% menu_by_code('spree-all-footer').menu_items.each do |root| %>
+ <% footer_menu_items = menu_by_code('spree-all-footer').menu_items.order(:lft) %>
+
+ <% footer_menu_items.each do |root| %>
<% if root.root? %>
<div class="col-3">
<div class="footer-spree-label">
- <%= link_to root.name, menu_item_link_to(root) %>
+ <%= link_to root.name, root.destination %>
</div>
<% root.children.each do |child| %>
<% if child.code == 'category' && child.item_type == 'Container' %>
<% if child.children.present? %>
<% child.children.each do |category_link| %>
- <div class="pt-2"><%= link_to category_link.name, menu_item_link_to(category_link) %></div>
+ <div class="pt-2"><%= link_to category_link.name, category_link.destination %></div>
<% end %>
<% end %>
<% end %>
diff --git a/frontend/app/views/spree/shared/_main_nav_bar.html.erb b/frontend/app/views/spree/shared/_main_nav_bar.html.erb
index 4fb39def2a..e21804f357 100644
--- a/frontend/app/views/spree/shared/_main_nav_bar.html.erb
+++ b/frontend/app/views/spree/shared/_main_nav_bar.html.erb
@@ -1,17 +1,18 @@
+<% if menu_by_code('spree-all-main').present? %>
+<% main_menu_items = menu_by_code('spree-all-main').menu_items.order(:lft) %>
<% category = 'category' %>
<% promo = 'promo' %>
-<% if menu_by_code('spree-all-main').present? %>
<div class="h-100" role="navigation" aria-label="<%= Spree.t('nav_bar.desktop') %>">
<ul class="nav h-100 main-nav-bar">
- <% menu_by_code('spree-all-main').menu_items.each do |root| %>
+ <% main_menu_items.each do |root| %>
<% if root.root? %>
<li class="p-0 nav-item main-nav-bar-category">
<% has_dropdown = root.children.present? %>
<% dropdown_toggle_class = has_dropdown ? "dropdown-toggle" : "" %>
<% data_attr = has_dropdown ? { toggle: "dropdown" } : {} %>
<% aria_attr = has_dropdown ? { haspopup: true, expanded: false } : {} %>
- <%= link_to(root.name, menu_item_link_to(root), class: "nav-link main-nav-bar-item main-nav-bar-category-button #{dropdown_toggle_class}", data: data_attr, aria: aria_attr) %>
+ <%= link_to(root.name, root.destination, class: "nav-link main-nav-bar-item main-nav-bar-category-button #{dropdown_toggle_class}", data: data_attr, aria: aria_attr) %>
<% if has_dropdown %>
<div class="dropdown-menu w-100 shadow main-nav-bar-category-dropdown">
<div class="container p-0 d-flex justify-content-xl-around mx-auto">
@@ -28,7 +29,7 @@
<ul class="pl-0">
<% child.children.each do |category_link| %>
<li>
- <%= link_to category_link.name, menu_item_link_to(category_link), class: "text-uppercase main-nav-bar-category-links dropdown-item truncate" %>
+ <%= link_to category_link.name, category_link.destination, class: "text-uppercase main-nav-bar-category-links dropdown-item truncate" %>
</li>
<% end %>
</ul>
@@ -40,7 +41,7 @@
<% 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 %>
+ <%= link_to promo_banner.destination, class: "dropdown-item" do %>
<div class="category-image text-center my-4 mx-3">
<% if promo_banner.has_attached_image %>
<%= image_tag main_app.url_for(promo_banner.image_asset), title: promo_banner.subtitle %>
diff --git a/frontend/app/views/spree/shared/_mobile_navigation.html.erb b/frontend/app/views/spree/shared/_mobile_navigation.html.erb
index 06931bd637..b5dd552c7f 100644
--- a/frontend/app/views/spree/shared/_mobile_navigation.html.erb
+++ b/frontend/app/views/spree/shared/_mobile_navigation.html.erb
@@ -1,4 +1,6 @@
<% if menu_by_code('spree-all-main').present? %>
+<% main_menu_items = menu_by_code('spree-all-main').menu_items.order(:lft) %>
+
<div class="position-fixed text-uppercase d-xl-none mobile-navigation" role="navigation" aria-label="<%= Spree.t('nav_bar.mobile') %>">
<div class="container d-flex align-items-center justify-content-between header-spree" data-hook>
<button class="m-0 p-0" id="mobile-navigation-back-button" aria-label="<%= Spree.t('nav_bar.go_to_previous_menu') %>">
@@ -18,10 +20,10 @@
</div>
<ul class="list-unstyled position-relative h-100 mobile-navigation-list">
- <% menu_by_code('spree-all-main').menu_items.each do |root| %>
+ <% main_menu_items.each do |root| %>
<% if root.root? %>
<li class="d-flex justify-content-between align-items-center mobile-navigation-list-item">
- <%= link_to root.name, menu_item_link_to(root), class: 'w-75' %>
+ <%= link_to root.name, root.destination, class: 'w-75' %>
<% if root.children.present? %>
<a class="w-25 text-right mobile-navigation-category-link" data-category="<%= root.name.parameterize %>" href="#" aria-label="<%= Spree.t('go_to_category') %>">
<%= icon(name: 'arrow-right',
@@ -39,7 +41,7 @@
<% if child.children.present? %>
<% child.children.each do |category_link| %>
<li class="d-flex justify-content-between align-items-center mobile-navigation-list-item">
- <%= link_to category_link.name, menu_item_link_to(category_link), class: 'w-75' %>
+ <%= link_to category_link.name, category_link.destination, class: 'w-75' %>
</li>
<% end %>
<% end %>