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.

Use dynamic links

Matthew Kennedyb3de484de7b0
diff --git a/api/app/controllers/spree/api/v2/storefront/menus_controller.rb b/api/app/controllers/spree/api/v2/storefront/menus_controller.rb
index cc0c4dc7ea..5619498399 100644
--- a/api/app/controllers/spree/api/v2/storefront/menus_controller.rb
+++ b/api/app/controllers/spree/api/v2/storefront/menus_controller.rb
@@ -13,7 +13,7 @@ module Spree
end
def resource
- scope.find_by(unique_code: params[:unique_code])
+ scope.find_by(location: params[:location])
end
def resource_serializer
@@ -31,10 +31,6 @@ module Spree
def model_class
Spree::Menu
end
-
- def scope
- super.by_store(current_store)
- end
end
end
end
diff --git a/api/app/serializers/spree/v2/storefront/menu_item_serializer.rb b/api/app/serializers/spree/v2/storefront/menu_item_serializer.rb
index f155f4f55a..e7ea668620 100644
--- a/api/app/serializers/spree/v2/storefront/menu_item_serializer.rb
+++ b/api/app/serializers/spree/v2/storefront/menu_item_serializer.rb
@@ -4,7 +4,11 @@ module Spree
class MenuItemSerializer < BaseSerializer
set_type :menu_item
- attributes :item_type, :code, :name, :subtitle, :destination, :new_window, :lft, :rgt, :depth
+ attributes :code, :name, :subtitle, :link, :new_window, :lft, :rgt, :depth
+
+ attribute :is_container do |menu_item|
+ menu_item.container?
+ end
attribute :is_root do |menu_item|
menu_item.root?
diff --git a/backend/app/views/spree/admin/menu_items/link_inputs/_resource.html.erb b/backend/app/views/spree/admin/menu_items/link_inputs/_resource.html.erb
index 08be4b7aff..0a4f754ac0 100644
--- a/backend/app/views/spree/admin/menu_items/link_inputs/_resource.html.erb
+++ b/backend/app/views/spree/admin/menu_items/link_inputs/_resource.html.erb
@@ -49,5 +49,5 @@
<%= f.label :linked_resource_id, label_text %>
<%= f.select :linked_resource_id, options_from_collection_for_select(resorce_holder, id_holder, name_holder, active_id_holder), { include_blank: true }, class: 'autocomplete' %>
<%= f.error_message_on :linked_resource_id %>
- <small class="form-text text-muted"><%= @menu_item.destination %></small>
+ <small class="form-text text-muted"><%= @menu_item.link %></small>
<% end %>
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 9b3d4d188d..6551d4cbca 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 overflow-hidden w-100">
<div class="overflow-hidden">
<%= item.name %>
- <% if item.destination %>
- <span class="badge badge-light badge-light-muted-text">-> <%= item.destination %></span>
+ <% if item.link %>
+ <span class="badge badge-light badge-light-muted-text">-> <%= item.link %></span>
<% end %>
<small class="form-text text-muted mt-0">
<%= "#{Spree.t('admin.navigation.type')}: #{item.item_type}" %>
diff --git a/core/app/models/spree/menu_item.rb b/core/app/models/spree/menu_item.rb
index 60f71f1c8f..a1e15eb60c 100644
--- a/core/app/models/spree/menu_item.rb
+++ b/core/app/models/spree/menu_item.rb
@@ -6,7 +6,7 @@ module Spree
belongs_to :linked_resource, polymorphic: true
before_create :ensure_item_belongs_to_root
- before_save :reset_link_attributes, :build_path, :paremeterize_code
+ before_save :reset_link_attributes, :paremeterize_code
after_save :touch_ancestors_and_menu
after_touch :touch_ancestors_and_menu
@@ -28,10 +28,6 @@ module Spree
has_one :icon, as: :viewable, dependent: :destroy, class_name: 'Spree::Icon'
accepts_nested_attributes_for :icon, reject_if: :all_blank
- def self.refresh_paths(resorce)
- where(linked_resource_type: resorce.class.name, linked_resource_id: resorce.id).each(&:save!)
- end
-
def container?
item_type == 'Container'
end
@@ -44,32 +40,29 @@ module Spree
end
end
- private
-
- def build_path
+ def link
case linked_resource_type
when 'Spree::Taxon'
- self.destination = if linked_resource.nil?
- nil
- elsif frontend_available?
- Spree::Core::Engine.routes.url_helpers.nested_taxons_path(linked_resource.permalink)
- else
- "/#{Spree::Config[:storefront_taxon_path]}/#{linked_resource.permalink}"
- end
+ if linked_resource.nil?
+ nil
+ else
+ "/#{Spree::Config[:storefront_taxon_path]}/#{linked_resource.permalink}"
+ end
when 'Spree::Product'
- self.destination = if linked_resource.nil?
- nil
- elsif frontend_available?
- Spree::Core::Engine.routes.url_helpers.product_path(linked_resource)
- else
- "/#{Spree::Config[:storefront_product_path]}/#{linked_resource.slug}"
- end
-
+ if linked_resource.nil?
+ nil
+ else
+ "/#{Spree::Config[:storefront_product_path]}/#{linked_resource.slug}"
+ end
when 'Home Page'
- self.destination = '/'
+ '/'
+ when 'URL'
+ destination
end
end
+ private
+
def reset_link_attributes
if linked_resource_type_changed? || item_type == 'Container'
self.linked_resource_id = nil
@@ -80,10 +73,6 @@ module Spree
end
end
- def frontend_available?
- Spree::Core::Engine.frontend_available?
- end
-
def ensure_item_belongs_to_root
if menu.try(:root).present? && parent_id.nil?
self.parent = menu.root
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index f31fc43295..4fab91c901 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -96,9 +96,6 @@ module Spree
after_save :reset_memoized_data
after_commit :reset_memoized_data
- after_save :sync_menu_item_paths
- after_commit :sync_menu_item_paths
-
before_validation :normalize_slug, on: :update
before_validation :validate_master
@@ -329,12 +326,6 @@ module Spree
private
- def sync_menu_item_paths
- return unless saved_change_to_slug?
-
- Spree::MenuItem.refresh_paths(self)
- end
-
def add_associations_from_prototype
if prototype_id && prototype = Spree::Prototype.find_by(id: prototype_id)
prototype.properties.each do |property|
diff --git a/core/app/models/spree/taxon.rb b/core/app/models/spree/taxon.rb
index 2571b85fa4..9898ed2c25 100644
--- a/core/app/models/spree/taxon.rb
+++ b/core/app/models/spree/taxon.rb
@@ -35,9 +35,6 @@ module Spree
after_save :touch_ancestors_and_taxonomy
after_touch :touch_ancestors_and_taxonomy
- after_save :sync_menu_item_paths
- after_commit :sync_menu_item_paths
-
has_one :icon, as: :viewable, dependent: :destroy, class_name: 'Spree::TaxonImage'
self.whitelisted_ransackable_associations = %w[taxonomy]
@@ -97,12 +94,6 @@ module Spree
private
- def sync_menu_item_paths
- return unless saved_change_to_permalink?
-
- Spree::MenuItem.refresh_paths(self)
- end
-
def touch_ancestors_and_taxonomy
# Touches all ancestors at once to avoid recursive taxonomy touch, and reduce queries.
ancestors.update_all(updated_at: Time.current)
diff --git a/core/spec/models/spree/menu_item_spec.rb b/core/spec/models/spree/menu_item_spec.rb
index fbd0acaa19..52ac5e3c67 100644
--- a/core/spec/models/spree/menu_item_spec.rb
+++ b/core/spec/models/spree/menu_item_spec.rb
@@ -36,25 +36,6 @@ describe Spree::MenuItem, type: :model do
end
end
- describe '#reset_link_attributes from URL to Taxon' do
- let(:i_a) do
- create(:menu_item, name: 'Home', item_type: 'Link', menu: menu,
- parent: menu.root, linked_resource_type: 'URL', destination: 'http://somewhere.com', new_window: true, linked_resource_id: 5)
- end
-
- before do
- i_a.update!(linked_resource_type: 'Spree::Taxon')
- end
-
- it 'sets resets the new_window to false' do
- expect(i_a.new_window).to be false
- end
-
- it 'sets resets the destination to nil' do
- expect(i_a.destination).to be nil
- end
- end
-
describe '#container?' do
let(:container_item){ create(:menu_item, name: 'Link 1', item_type: 'Container', menu: menu) }
let(:link_item) { create(:menu_item, name: 'Home', item_type: 'Link', menu: menu) }
@@ -104,7 +85,7 @@ describe Spree::MenuItem, type: :model do
end
it 'sets destination to /' do
- expect(i_b.destination).to eql('/')
+ expect(i_b.link).to eql('/')
end
end
@@ -123,7 +104,7 @@ describe Spree::MenuItem, type: :model do
end
end
- describe '#build_path' do
+ describe '#link' do
let(:product) { create(:product) }
let(:taxon) { create(:taxon) }
let(:item_url) { create(:menu_item, name: 'URL To Random Site', item_type: 'Link', menu: menu, parent: menu.root, linked_resource_type: 'URL', destination: 'https://some-other-website.com') }
@@ -131,10 +112,10 @@ describe Spree::MenuItem, type: :model do
let(:item_product) { create(:menu_item, name: product.name, item_type: 'Link', menu: menu, parent: menu.root, linked_resource_type: 'Spree::Product') }
let(:item_taxon) { create(:menu_item, name: taxon.name, item_type: 'Link', menu: menu, parent: menu.root, linked_resource_type: 'Spree::Taxon') }
- it 'returns taxon path' do
+ it 'returns correct taxon path' do
item_taxon.update(linked_resource: taxon)
- expect(item_taxon.destination).to eql "/t/#{taxon.permalink}"
+ expect(item_taxon.link).to eql "/t/#{taxon.permalink}"
end
it 'returns nil for destination when taxon is removed' do
@@ -144,25 +125,25 @@ describe Spree::MenuItem, type: :model do
expect(item_taxon.destination).to be nil
end
- it 'returns product path' do
+ it 'returns correct product path' do
item_product.update(linked_resource: product)
- expect(item_product.destination).to eql "/products/#{product.slug}"
+ expect(item_product.link).to eql "/products/#{product.slug}"
end
it 'returns nil for destination when product is removed' do
item_product.update(linked_resource: product)
item_product.update(linked_resource_id: nil)
- expect(item_product.destination).to be nil
+ expect(item_product.link).to be nil
end
- it 'returns root path' do
- expect(item_home.destination).to eql '/'
+ it 'returns correct root path' do
+ expect(item_home.link).to eql '/'
end
- it 'returns URL path' do
- expect(item_url.destination).to eql 'https://some-other-website.com'
+ it 'returns correct URL path' do
+ expect(item_url.link).to eql 'https://some-other-website.com'
end
end
diff --git a/frontend/app/helpers/spree/navigation_helper.rb b/frontend/app/helpers/spree/navigation_helper.rb
index 30803f3f35..7f104bc6e0 100644
--- a/frontend/app/helpers/spree/navigation_helper.rb
+++ b/frontend/app/helpers/spree/navigation_helper.rb
@@ -62,18 +62,18 @@ module Spree
end
def spree_localized_item_link(item)
- return if item.destination.nil?
+ return if item.link.nil?
output_locale = if locale_param
"/#{I18n.locale}"
end
if Spree::MenuItem::DYNAMIC_RESOURCE_TYPE.include? item.linked_resource_type
- output_locale.to_s + item.destination
+ output_locale.to_s + item.link
elsif item.linked_resource_type == 'Home Page'
"/#{locale_param}"
else
- item.destination
+ item.link
end
end
diff --git a/frontend/config/routes.rb b/frontend/config/routes.rb
index 195670e10c..b6dffcdfb8 100644
--- a/frontend/config/routes.rb
+++ b/frontend/config/routes.rb
@@ -3,9 +3,9 @@ Spree::Core::Engine.add_routes do
root to: 'home#index'
resources :products, only: [:index, :show]
- get '/products/:id/related', to: 'products#related'
+ get "/#{Spree::Config[:storefront_product_path]}/:id/related", to: 'products#related'
# route globbing for pretty nested taxon and product paths
- get '/t/*id', to: 'taxons#show', as: :nested_taxons
+ get "/#{Spree::Config[:storefront_taxon_path]}/*id", to: 'taxons#show', as: :nested_taxons
get '/product_carousel/:id', to: 'taxons#product_carousel'
# non-restful checkout stuff
diff --git a/sample/db/samples/menu_items.rb b/sample/db/samples/menu_items.rb
index 005ef65b5e..2921b2519a 100644
--- a/sample/db/samples/menu_items.rb
+++ b/sample/db/samples/menu_items.rb
@@ -110,7 +110,6 @@ MENUS.each do |menu|
promo_b_subtitle = 'Obtenga hasta un 30% de descuento'
end
-
##############
# Root Items #
##############
@@ -220,7 +219,7 @@ MENUS.each do |menu|
name: promo_a_name,
subtitle: promo_a_subtitle,
linked_resource_type: 'Spree::Taxon',
- item_type: 'Promotion',
+ item_type: 'Container',
menu_id: menu,
parent_id: promo
).first_or_create!
@@ -231,7 +230,7 @@ MENUS.each do |menu|
name: promo_b_name,
subtitle: promo_b_subtitle,
linked_resource_type: 'Spree::Taxon',
- item_type: 'Promotion',
+ item_type: 'Container',
menu_id: menu,
parent_id: promo
).first_or_create!
@@ -244,10 +243,10 @@ MENUS.each do |menu|
#################################
women_link_parent_id = if menu.location == 'header'
- menu_cat_women
- else
- menu_root_women
- end
+ menu_cat_women
+ else
+ menu_root_women
+ end
women_skirts_t = Spree::Taxon.find_by!(permalink: 'women/skirts')
women_skirts = Spree::MenuItem.where(
@@ -320,10 +319,10 @@ MENUS.each do |menu|
###############################
men_link_parent_id = if menu.location == 'header'
- menu_cat_men
- else
- menu_root_men
- end
+ menu_cat_men
+ else
+ menu_root_men
+ end
men_shirts_t = Spree::Taxon.find_by!(permalink: 'men/shirts')
men_shirts = Spree::MenuItem.where(
@@ -374,10 +373,10 @@ MENUS.each do |menu|
######################################
sw_link_parent_id = if menu.location == 'header'
- menu_cat_sw
- else
- menu_root_sw
- end
+ menu_cat_sw
+ else
+ menu_root_sw
+ end
sw_tops_t = Spree::Taxon.find_by!(permalink: 'sportswear/tops')
sw_tops = Spree::MenuItem.where(