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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
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.rbindex 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 Spreeenddef resource- scope.find_by(unique_code: params[:unique_code])+ scope.find_by(location: params[:location])enddef resource_serializer@@ -31,10 +31,6 @@ module Spreedef model_classSpree::Menuend-- def scope- super.by_store(current_store)- endendendenddiff --git a/api/app/serializers/spree/v2/storefront/menu_item_serializer.rb b/api/app/serializers/spree/v2/storefront/menu_item_serializer.rbindex 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 Spreeclass MenuItemSerializer < BaseSerializerset_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?+ endattribute :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.erbindex 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.erbindex 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.rbindex 60f71f1c8f..a1e15eb60c 100644--- a/core/app/models/spree/menu_item.rb+++ b/core/app/models/spree/menu_item.rb@@ -6,7 +6,7 @@ module Spreebelongs_to :linked_resource, polymorphic: truebefore_create :ensure_item_belongs_to_root- before_save :reset_link_attributes, :build_path, :paremeterize_code+ before_save :reset_link_attributes, :paremeterize_codeafter_save :touch_ancestors_and_menuafter_touch :touch_ancestors_and_menu@@ -28,10 +28,6 @@ module Spreehas_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 Spreeendend- private-- def build_path+ def linkcase linked_resource_typewhen '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}"+ endwhen '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}"+ endwhen 'Home Page'- self.destination = '/'+ '/'+ when 'URL'+ destinationendend+ private+def reset_link_attributesif linked_resource_type_changed? || item_type == 'Container'self.linked_resource_id = nil@@ -80,10 +73,6 @@ module Spreeendend- def frontend_available?- Spree::Core::Engine.frontend_available?- end-def ensure_item_belongs_to_rootif menu.try(:root).present? && parent_id.nil?self.parent = menu.rootdiff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rbindex f31fc43295..4fab91c901 100644--- a/core/app/models/spree/product.rb+++ b/core/app/models/spree/product.rb@@ -96,9 +96,6 @@ module Spreeafter_save :reset_memoized_dataafter_commit :reset_memoized_data- after_save :sync_menu_item_paths- after_commit :sync_menu_item_paths-before_validation :normalize_slug, on: :updatebefore_validation :validate_master@@ -329,12 +326,6 @@ module Spreeprivate- def sync_menu_item_paths- return unless saved_change_to_slug?-- Spree::MenuItem.refresh_paths(self)- end-def add_associations_from_prototypeif 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.rbindex 2571b85fa4..9898ed2c25 100644--- a/core/app/models/spree/taxon.rb+++ b/core/app/models/spree/taxon.rb@@ -35,9 +35,6 @@ module Spreeafter_save :touch_ancestors_and_taxonomyafter_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 Spreeprivate- 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.rbindex 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 doendend- 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?' dolet(: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 doendit 'sets destination to /' do- expect(i_b.destination).to eql('/')+ expect(i_b.link).to eql('/')endend@@ -123,7 +104,7 @@ describe Spree::MenuItem, type: :model doendend- describe '#build_path' do+ describe '#link' dolet(: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 dolet(: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' doitem_taxon.update(linked_resource: taxon)- expect(item_taxon.destination).to eql "/t/#{taxon.permalink}"+ expect(item_taxon.link).to eql "/t/#{taxon.permalink}"endit 'returns nil for destination when taxon is removed' do@@ -144,25 +125,25 @@ describe Spree::MenuItem, type: :model doexpect(item_taxon.destination).to be nilend- it 'returns product path' do+ it 'returns correct product path' doitem_product.update(linked_resource: product)- expect(item_product.destination).to eql "/products/#{product.slug}"+ expect(item_product.link).to eql "/products/#{product.slug}"endit 'returns nil for destination when product is removed' doitem_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 nilend- 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'endenddiff --git a/frontend/app/helpers/spree/navigation_helper.rb b/frontend/app/helpers/spree/navigation_helper.rbindex 30803f3f35..7f104bc6e0 100644--- a/frontend/app/helpers/spree/navigation_helper.rb+++ b/frontend/app/helpers/spree/navigation_helper.rb@@ -62,18 +62,18 @@ module Spreeenddef spree_localized_item_link(item)- return if item.destination.nil?+ return if item.link.nil?output_locale = if locale_param"/#{I18n.locale}"endif Spree::MenuItem::DYNAMIC_RESOURCE_TYPE.include? item.linked_resource_type- output_locale.to_s + item.destination+ output_locale.to_s + item.linkelsif item.linked_resource_type == 'Home Page'"/#{locale_param}"else- item.destination+ item.linkendenddiff --git a/frontend/config/routes.rb b/frontend/config/routes.rbindex 195670e10c..b6dffcdfb8 100644--- a/frontend/config/routes.rb+++ b/frontend/config/routes.rb@@ -3,9 +3,9 @@ Spree::Core::Engine.add_routes doroot 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_taxonsget '/product_carousel/:id', to: 'taxons#product_carousel'# non-restful checkout stuffdiff --git a/sample/db/samples/menu_items.rb b/sample/db/samples/menu_items.rbindex 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+ endwomen_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+ endmen_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+ endsw_tops_t = Spree::Taxon.find_by!(permalink: 'sportswear/tops')sw_tops = Spree::MenuItem.where(