From 564ea0bd4923ffd0d41c6f9dfe945a4a788916ad Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Wed, 22 May 2024 22:07:22 +1000 Subject: [PATCH] Fix enterprise touching when associated record updated --- app/models/spree/product.rb | 2 ++ app/models/spree/property.rb | 8 ++++++++ spec/models/enterprise_caching_spec.rb | 9 ++++----- spec/models/spree/product_spec.rb | 27 +++++++++++++++++++------- 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/app/models/spree/product.rb b/app/models/spree/product.rb index 8cb1cfa041..56e6150621 100755 --- a/app/models/spree/product.rb +++ b/app/models/spree/product.rb @@ -329,6 +329,8 @@ module Spree end def touch_supplier + return if variants.empty? + # Assume the product supplier is the supplier of the first variant # Will breack if product has mutiple variants with different supplier variants.first.supplier.touch diff --git a/app/models/spree/property.rb b/app/models/spree/property.rb index 9f45270a78..0aae0d04dd 100644 --- a/app/models/spree/property.rb +++ b/app/models/spree/property.rb @@ -6,6 +6,8 @@ module Spree has_many :products, through: :product_properties has_many :producer_properties, dependent: :destroy + after_touch :touch_producer_properties + validates :name, :presentation, presence: true scope :sorted, -> { order(:name) } @@ -13,5 +15,11 @@ module Spree def property self end + + private + + def touch_producer_properties + producer_properties.each(&:touch) + end end end diff --git a/spec/models/enterprise_caching_spec.rb b/spec/models/enterprise_caching_spec.rb index 625d92ca9d..d2c8321690 100644 --- a/spec/models/enterprise_caching_spec.rb +++ b/spec/models/enterprise_caching_spec.rb @@ -11,15 +11,14 @@ RSpec.describe Enterprise do let(:supplier2) { create(:supplier_enterprise) } describe "with a supplied product" do - let(:product) { create(:simple_product, primary_taxon_id: taxon.id) } + let(:product) { + create(:simple_product, primary_taxon_id: taxon.id, supplier_id: enterprise.id) + } let(:property) { product.product_properties.last } let(:producer_property) { enterprise.producer_properties.last } - let(:variant) { create(:variant, product:, supplier: enterprise) } + let(:variant) { product.variants.first } before do - product.variants = [] - product.variants << variant - product.set_property 'Organic', 'NASAA 12345' enterprise.set_producer_property 'Biodynamic', 'ASDF 4321' end diff --git a/spec/models/spree/product_spec.rb b/spec/models/spree/product_spec.rb index b9dce1b3b1..1106ecd92c 100644 --- a/spec/models/spree/product_spec.rb +++ b/spec/models/spree/product_spec.rb @@ -283,7 +283,6 @@ module Spree let(:product) { create(:simple_product) } describe "touching affected enterprises when the product is deleted" do - let(:product) { create(:simple_product) } let(:supplier) { create(:supplier_enterprise) } let(:distributor) { create(:distributor_enterprise) } let!(:oc) { @@ -308,19 +307,33 @@ module Spree end describe "after updating primary taxon" do - let(:product) { create(:simple_product) } + let(:product) { create(:simple_product, supplier_id: supplier.id) } let(:supplier) { create(:supplier_enterprise) } let(:new_taxon) { create(:taxon) } - before do - product.variants = [] - product.variants << create(:variant, product:, supplier:) - end - it "touches the supplier" do expect { product.update(primary_taxon_id: new_taxon.id) } .to change { supplier.reload.updated_at } end + + context "when product has no variant" do + it "doesn't blow up" do + product.variants = [] + product.save! + + expect { product.update(primary_taxon_id: new_taxon.id) }.to_not raise_error + end + end + end + + describe "after touching the product" do + let(:product) { create(:simple_product, supplier_id: supplier.id) } + let(:supplier) { create(:supplier_enterprise) } + + it "touches the supplier" do + expect { product.touch } + .to change { supplier.reload.updated_at } + end end it "updates units when saved change to variant unit" do