Fix spree product touch supplier

Take into account import product scenario, in this case the variant
doesn't have an assigned supplier yet.
This commit is contained in:
Gaetan Craig-Riou
2024-05-26 14:16:38 +10:00
parent 2d707e8acb
commit 3dc7c2bf56
2 changed files with 15 additions and 1 deletions

View File

@@ -77,6 +77,7 @@ module Spree
after_update :touch_supplier, if: :saved_change_to_primary_taxon_id?
around_destroy :destruction
after_save :update_units
after_touch :touch_supplier
# -- Scopes
scope :with_properties, ->(*property_ids) {
@@ -333,7 +334,12 @@ module Spree
# 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
first_variant = variants.first
# The variant is invalid if no supplier is present, but this method can be triggered when
# importing product. In this scenario the variant has not been updated with the supplier yet
# hence the check.
first_variant.supplier.touch if first_variant.supplier.present?
end
def touch_distributors

View File

@@ -334,6 +334,14 @@ module Spree
expect { product.touch }
.to change { supplier.reload.updated_at }
end
context "when the first variant is missing supplier" do
it "doesn't blow up" do
product.variants.first.update_attribute(:supplier_id, nil)
expect { product.touch }.to_not raise_error
end
end
end
it "updates units when saved change to variant unit" do