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.

Fixed cache expiration for new Nav CMS

Damian Legawiec5aadfd42a612
diff --git a/core/app/models/spree/menu.rb b/core/app/models/spree/menu.rb
index 754a419333..6b426f5da2 100644
--- a/core/app/models/spree/menu.rb
+++ b/core/app/models/spree/menu.rb
@@ -9,11 +9,12 @@ module Spree
end
has_many :menu_items, dependent: :destroy
- belongs_to :store
+ belongs_to :store, touch: true
before_validation :paremeterize_location
after_create :set_root
after_save :update_root_name
+ after_touch :touch_store
validates :name, :store, :locale, presence: true
validates :location, uniqueness: { scope: [:store, :locale] }
@@ -49,5 +50,9 @@ module Spree
root.update(name: name)
end
+
+ def touch_store
+ store.touch
+ end
end
end
diff --git a/core/app/models/spree/menu_item.rb b/core/app/models/spree/menu_item.rb
index 2f6d197e15..8533bf68c9 100644
--- a/core/app/models/spree/menu_item.rb
+++ b/core/app/models/spree/menu_item.rb
@@ -2,7 +2,7 @@ module Spree
class MenuItem < Spree::Base
acts_as_nested_set dependent: :destroy
- belongs_to :menu
+ belongs_to :menu, touch: true
belongs_to :linked_resource, polymorphic: true
before_create :ensure_item_belongs_to_root
@@ -71,7 +71,7 @@ module Spree
def ensure_item_belongs_to_root
if menu.try(:root).present? && parent_id.nil?
- self.parent_id = menu.root.id
+ self.parent = menu.root
end
end
diff --git a/core/lib/spree/testing_support/factories/menu_item.rb b/core/lib/spree/testing_support/factories/menu_item_factory.rb
similarity index 100%
rename from core/lib/spree/testing_support/factories/menu_item.rb
rename to core/lib/spree/testing_support/factories/menu_item_factory.rb
diff --git a/core/spec/models/spree/menu_item_spec.rb b/core/spec/models/spree/menu_item_spec.rb
index 3ce78b90e9..d7539fde6e 100644
--- a/core/spec/models/spree/menu_item_spec.rb
+++ b/core/spec/models/spree/menu_item_spec.rb
@@ -2,44 +2,44 @@ require 'spec_helper'
describe Spree::MenuItem, type: :model do
let!(:store) { create(:store) }
- let!(:menu) { create(:menu, name: 'Main Menu', store_id: store.id) }
+ let!(:menu) { create(:menu, name: 'Main Menu', store: store) }
describe 'validates name' do
it 'returns presence error when no name is given' do
- expect(described_class.new(name: '', menu_id: menu.id, parent_id: menu.root.id, item_type: 'Link')).not_to be_valid
+ expect(described_class.new(name: '', menu: menu, parent: menu.root, item_type: 'Link')).not_to be_valid
end
end
describe 'validates menu_id presence and integer_only' do
it 'returns error when no menu id is passed' do
- expect(described_class.new(name: 'Menu Item', item_type: 'Link', parent_id: menu.root.id)).not_to be_valid
+ expect(described_class.new(name: 'Menu Item', item_type: 'Link', parent: menu.root)).not_to be_valid
end
it 'returns error when no menu id is not an integer' do
- expect(described_class.new(name: 'Menu Item', menu_id: 'string', item_type: 'Link', parent_id: menu.root.id)).not_to be_valid
+ expect(described_class.new(name: 'Menu Item', menu_id: 'string', item_type: 'Link', parent: menu.root)).not_to be_valid
end
it 'returns success when no menu id an integer' do
- expect(described_class.new(name: 'Menu Item', item_type: 'Link', menu_id: menu.id, parent_id: menu.root.id)).to be_valid
+ expect(described_class.new(name: 'Menu Item', item_type: 'Link', menu: menu, parent: menu.root)).to be_valid
end
end
describe 'validates item_type' do
it 'returns error when the item_type is not in the list' do
- expect(described_class.new(name: 'Menu Item', menu_id: menu.id, item_type: 'Linkker', parent_id: menu.root.id)).not_to be_valid
+ expect(described_class.new(name: 'Menu Item', menu: menu, item_type: 'Linkker', parent: menu.root)).not_to be_valid
end
end
describe 'validates linked_resource_type' do
it 'returns error when the linked_resource_type is not in the list' do
- expect(described_class.new(name: 'Menu Item', menu_id: menu.id, item_type: 'Link', linked_resource_type: 'Purple', parent_id: menu.root.id)).not_to be_valid
+ expect(described_class.new(name: 'Menu Item', menu: menu, item_type: 'Link', linked_resource_type: 'Purple', parent: menu.root)).not_to be_valid
end
end
describe '.reset_link_attributes from URL to Taxon' do
let(:i_a) do
- create(:menu_item, name: 'Home', item_type: 'Link', menu_id: menu.id,
- parent_id: menu.root.id, linked_resource_type: 'URL', destination: 'http://somewhere.com', new_window: true, linked_resource_id: 5)
+ 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
@@ -57,8 +57,8 @@ describe Spree::MenuItem, type: :model do
describe '.reset_link_attributes from URL to Home Page' do
let(:i_b) do
- create(:menu_item, name: 'Home', item_type: 'Link', menu_id: menu.id,
- parent_id: menu.root.id, linked_resource_type: 'URL', destination: 'http://somewhere.com', new_window: true, linked_resource_id: 5)
+ 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
@@ -72,8 +72,8 @@ describe Spree::MenuItem, type: :model do
describe '.reset_link_attributes from Link to Container' do
let(:i_c) do
- create(:menu_item, name: 'Home', item_type: 'Link', menu_id: menu.id,
- parent_id: menu.root.id, linked_resource_type: 'URL', destination: 'http://somewhere.com', new_window: true, linked_resource_id: 5)
+ 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
@@ -88,32 +88,32 @@ describe Spree::MenuItem, type: :model do
describe '#build_path' do
let(:product) { create(:product) }
let(:taxon) { create(:taxon) }
- let(:item_url) { create(:menu_item, name: 'URL To Random Site', item_type: 'Link', menu_id: menu.id, parent_id: menu.root.id, linked_resource_type: 'URL', destination: 'https://some-other-website.com') }
- let(:item_home) { create(:menu_item, name: 'Home', item_type: 'Link', menu_id: menu.id, parent_id: menu.root.id, linked_resource_type: 'Home Page') }
- let(:item_product) { create(:menu_item, name: product.name, item_type: 'Link', menu_id: menu.id, parent_id: menu.root.id, linked_resource_type: 'Spree::Product') }
- let(:item_taxon) { create(:menu_item, name: taxon.name, item_type: 'Link', menu_id: menu.id, parent_id: menu.root.id, linked_resource_type: 'Spree::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') }
+ let(:item_home) { create(:menu_item, name: 'Home', item_type: 'Link', menu: menu, parent: menu.root, linked_resource_type: 'Home Page') }
+ 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
- item_taxon.update(linked_resource_id: taxon.id)
+ item_taxon.update(linked_resource: taxon)
expect(item_taxon.destination).to eql "/t/#{taxon.permalink}"
end
it 'returns nil for destination when taxon is removed' do
- item_taxon.update(linked_resource_id: taxon.id)
+ item_taxon.update(linked_resource: taxon)
item_taxon.update(linked_resource_id: nil)
expect(item_taxon.destination).to eql nil
end
it 'returns product path' do
- item_product.update(linked_resource_id: product.id)
+ item_product.update(linked_resource: product)
expect(item_product.destination).to eql "/products/#{product.slug}"
end
it 'returns nil for destination when product is removed' do
- item_product.update(linked_resource_id: product.id)
+ item_product.update(linked_resource: product)
item_product.update(linked_resource_id: nil)
expect(item_product.destination).to eql nil
@@ -129,7 +129,7 @@ describe Spree::MenuItem, type: :model do
end
describe '.paremeterize_code' do
- let(:item) { create(:menu_item, name: 'URL', item_type: 'Link', menu_id: menu.id, parent_id: menu.root.id, linked_resource_type: 'URL', code: 'My Fantastic Code') }
+ let(:item) { create(:menu_item, name: 'URL', item_type: 'Link', menu: menu, parent: menu.root, linked_resource_type: 'URL', code: 'My Fantastic Code') }
it 'paramatizes a code when one is given' do
expect(item.code).to eql 'my-fantastic-code'
@@ -137,10 +137,17 @@ describe Spree::MenuItem, type: :model do
end
describe '.ensure_item_belongs_to_root' do
- let(:item_x) { create(:menu_item, name: 'URL', item_type: 'Link', menu_id: menu.id, parent_id: menu.root.id, linked_resource_type: 'URL', code: 'My Fantastic Code') }
+ let(:item_x) { create(:menu_item, name: 'URL', item_type: 'Link', menu: menu, parent: menu.root, linked_resource_type: 'URL', code: 'My Fantastic Code') }
it 'Sets new items parent_id to root.id' do
expect(item_x.parent_id).to eql menu.root.id
end
end
+
+ describe 'touch menu and store' do
+ let(:menu_item) { build(:menu_item, menu: menu) }
+
+ it { expect { menu_item.save! }.to change(menu, :updated_at) }
+ it { expect { menu_item.save! }.to change(store, :updated_at) }
+ end
end
diff --git a/core/spec/models/spree/menu_spec.rb b/core/spec/models/spree/menu_spec.rb
index 281b39ecec..489d21ce78 100644
--- a/core/spec/models/spree/menu_spec.rb
+++ b/core/spec/models/spree/menu_spec.rb
@@ -13,10 +13,10 @@ describe Spree::Menu, type: :model do
let(:store_1) { create(:store) }
let(:store_2) { create(:store) }
let(:store_3) { create(:store) }
- let!(:menu) { create(:menu, name: 'Footer Menu', location: 'Footer', store_id: store_1.id) }
+ let!(:menu) { create(:menu, name: 'Footer Menu', location: 'Footer', store: store_1) }
it 'validates presence of name' do
- expect(described_class.new(name: '', location: 'Header', store_id: store_3.id)).not_to be_valid
+ expect(described_class.new(name: '', location: 'Header', store: store_3)).not_to be_valid
end
it 'validates presence of store' do
@@ -24,14 +24,14 @@ describe Spree::Menu, type: :model do
end
it 'validates presence of locale' do
- expect(described_class.new(name: 'No Locale For Me', location: 'Header', locale: nil, store_id: store_2.id)).not_to be_valid
+ expect(described_class.new(name: 'No Locale For Me', location: 'Header', locale: nil, store: store_2)).not_to be_valid
end
it 'validates uniqueness of location within scope of language and store' do
- expect(described_class.new(name: 'BBB', location: 'Footer', locale: 'en', store_id: store_1.id)).not_to be_valid
- expect(described_class.new(name: 'BBB', location: 'Footer', locale: 'fr', store_id: store_1.id)).to be_valid
- expect(described_class.new(name: 'BBB', location: 'Header', locale: 'en', store_id: store_1.id)).to be_valid
- expect(described_class.new(name: 'BBB', location: 'Footer', locale: 'en', store_id: store_2.id)).to be_valid
+ expect(described_class.new(name: 'BBB', location: 'Footer', locale: 'en', store: store_1)).not_to be_valid
+ expect(described_class.new(name: 'BBB', location: 'Footer', locale: 'fr', store: store_1)).to be_valid
+ expect(described_class.new(name: 'BBB', location: 'Header', locale: 'en', store: store_1)).to be_valid
+ expect(described_class.new(name: 'BBB', location: 'Footer', locale: 'en', store: store_2)).to be_valid
end
it '.paremeterize_location parametizes the location' do
@@ -47,7 +47,7 @@ describe Spree::Menu, type: :model do
describe 'updating the menu name' do
let(:store_a) { create(:store) }
- let(:m_x) { create(:menu, name: 'Main Menu', location: 'Header', store_id: store_a.id) }
+ let(:m_x) { create(:menu, name: 'Main Menu', location: 'Header', store: store_a) }
before do
m_x.update!(name: 'Super Menu')
@@ -57,4 +57,11 @@ describe Spree::Menu, type: :model do
expect(m_x.root.name).to eql('Super Menu')
end
end
+
+ describe 'touch store' do
+ let!(:store) { create(:store) }
+ let(:menu) { build(:menu, store: store) }
+
+ it { expect { menu.save! }.to change(store, :updated_at) }
+ end
end
diff --git a/frontend/app/helpers/spree/navigation_helper.rb b/frontend/app/helpers/spree/navigation_helper.rb
index b4181cffc7..96d452adcd 100644
--- a/frontend/app/helpers/spree/navigation_helper.rb
+++ b/frontend/app/helpers/spree/navigation_helper.rb
@@ -20,12 +20,28 @@ module Spree
end
def spree_nav_cache_key(section = 'header')
+ ActiveSupport::Deprecation.warn(<<-DEPRECATION, caller)
+ NavigationHelper#spree_nav_cache_key is deprecated and will be removed in Spree 5.0.
+ Please migrate to the new navigation cms system.
+ DEPRECATION
+
@spree_nav_cache_key = begin
- keys = base_cache_key + [current_store, spree_navigation_data_cache_key, Spree::Config[:logo], stores&.cache_key, section]
+ keys = base_cache_key + [current_store, spree_navigation_data_cache_key, Spree::Config[:logo], stores&.cache_key_with_version, section]
Digest::MD5.hexdigest(keys.join('-'))
end
end
+ def spree_menu_cache_key(section = 'header')
+ keys = base_cache_key + [
+ current_store.cache_key_with_version,
+ spree_menu(section)&.cache_key_with_version,
+ Spree::Config[:logo],
+ stores&.cache_key_with_version,
+ section
+ ]
+ Digest::MD5.hexdigest(keys.join('-'))
+ end
+
def main_nav_image(image_path, title = '')
image_url = asset_path(asset_exists?(image_path) ? image_path : 'noimage/plp.png')
diff --git a/frontend/app/views/spree/shared/_footer.html.erb b/frontend/app/views/spree/shared/_footer.html.erb
index 5946bc2cc4..daa2f17487 100644
--- a/frontend/app/views/spree/shared/_footer.html.erb
+++ b/frontend/app/views/spree/shared/_footer.html.erb
@@ -1,4 +1,4 @@
-<% cache spree_nav_cache_key('footer') do %>
+<% cache spree_menu_cache_key('footer') do %>
<footer id="footer" class="pt-2 mx-auto border-top footer-spree" data-turbolinks-permanent>
<div class="container">
<div class="row d-xl-flex justify-content-xl-around pb-xl-5">
diff --git a/frontend/app/views/spree/shared/_header.html.erb b/frontend/app/views/spree/shared/_header.html.erb
index 0d9e3c8ddc..3febd143b6 100644
--- a/frontend/app/views/spree/shared/_header.html.erb
+++ b/frontend/app/views/spree/shared/_header.html.erb
@@ -1,5 +1,5 @@
<%# leaving #spree-header div for legacy support %>
-<% cache spree_nav_cache_key('header') do %>
+<% cache spree_menu_cache_key('header') do %>
<div id="spree-header" data-turbolinks-permanent>
<header id="header" class="d-flex align-items-center header-spree border-bottom">
<div class="container-fluid h-100 header-spree-fluid">