mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Fix enterprise touching when associated record updated
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user