Fix enterprise touching when associated record updated

This commit is contained in:
Gaetan Craig-Riou
2024-05-22 22:07:22 +10:00
parent 1c6d10d4e7
commit 564ea0bd49
4 changed files with 34 additions and 12 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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