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.

Touch products on Option Type update (#12174)

Damian Legawiecaac1a0890f99
diff --git a/core/app/models/spree/option_type.rb b/core/app/models/spree/option_type.rb
index 5c30754b3c..db3f86f32c 100644
--- a/core/app/models/spree/option_type.rb
+++ b/core/app/models/spree/option_type.rb
@@ -64,6 +64,8 @@ module Spree
# Callbacks
#
after_touch :touch_all_products
+ after_update :touch_all_products, if: -> { saved_changes.key?(:presentation) }
+ after_destroy :touch_all_products
# legacy, name itself is now parameterized before saving
def filter_param
diff --git a/core/app/models/spree/option_value.rb b/core/app/models/spree/option_value.rb
index 8d790893a2..bda381c88a 100644
--- a/core/app/models/spree/option_value.rb
+++ b/core/app/models/spree/option_value.rb
@@ -56,8 +56,8 @@ module Spree
# Callbacks
#
after_touch :touch_all_variants
- after_update :touch_products, if: -> { saved_change_to_presentation? }
- after_touch :touch_products
+ after_update :touch_all_products, if: -> { saved_changes.key?(:presentation) }
+ after_touch :touch_all_products
delegate :name, :presentation, to: :option_type, prefix: true, allow_nil: true
@@ -67,7 +67,7 @@ module Spree
variants.touch_all
end
- def touch_products
+ def touch_all_products
products.touch_all
end
end
diff --git a/core/app/models/spree/property.rb b/core/app/models/spree/property.rb
index 7ff77a690f..70fce5b865 100644
--- a/core/app/models/spree/property.rb
+++ b/core/app/models/spree/property.rb
@@ -37,6 +37,7 @@ module Spree
scope :filterable, -> { where(filterable: true) }
after_touch :touch_all_products
+ after_update :touch_all_products, if: -> { saved_changes.key?(:presentation) }
after_save :ensure_product_properties_have_filter_params
self.whitelisted_ransackable_attributes = ['presentation', 'filterable']
@@ -52,7 +53,7 @@ module Spree
private
def touch_all_products
- products.update_all(updated_at: Time.current)
+ products.touch_all
end
def with_uniq_values_cache_key(product_properties_scope, &block)
diff --git a/core/spec/models/spree/option_type_spec.rb b/core/spec/models/spree/option_type_spec.rb
index 994932490c..caa4319c57 100644
--- a/core/spec/models/spree/option_type_spec.rb
+++ b/core/spec/models/spree/option_type_spec.rb
@@ -60,13 +60,20 @@ describe Spree::OptionType, type: :model do
end
context 'touching' do
- it 'touches a product' do
- product_option_type = create(:product_option_type)
- option_type = product_option_type.option_type
- product = product_option_type.product
+ let(:option_type) { create(:option_type) }
+ let(:product) { create(:product) }
+ let!(:product_option_type) { create(:product_option_type, option_type: option_type, product: product) }
+
+ before do
product.update_column(:updated_at, 1.day.ago)
- option_type.touch
- expect(product.reload.updated_at).to be_within(3.seconds).of(Time.current)
+ end
+
+ it 'touches a product on touch' do
+ expect { option_type.touch }.to change { product.reload.updated_at }
+ end
+
+ it 'touches a product on update' do
+ expect { option_type.update!(presentation: 'New Presentation') }.to change { product.reload.updated_at }
end
end
diff --git a/core/spec/models/spree/option_value_spec.rb b/core/spec/models/spree/option_value_spec.rb
index 491f1467b1..ef40231528 100644
--- a/core/spec/models/spree/option_value_spec.rb
+++ b/core/spec/models/spree/option_value_spec.rb
@@ -26,7 +26,7 @@ describe Spree::OptionValue, type: :model do
end
end
- describe '#touch_products' do
+ describe '#touch_all_products' do
let!(:option_value) { create(:option_value) }
let!(:product1) { create(:product) }
let!(:product2) { create(:product) }
@@ -39,7 +39,7 @@ describe Spree::OptionValue, type: :model do
end
it 'touches all products associated with the option value' do
- expect { option_value.send(:touch_products) }.to change { [product1.reload.updated_at, product2.reload.updated_at, product3.reload.updated_at] }
+ expect { option_value.send(:touch_all_products) }.to change { [product1.reload.updated_at, product2.reload.updated_at, product3.reload.updated_at] }
end
end
end