Remove product variants from all Order Cycles if supplier is changed

This commit is contained in:
luisramos0
2019-10-17 18:13:02 +01:00
parent 49f98422fd
commit 8857404ddf
2 changed files with 35 additions and 1 deletions

View File

@@ -46,13 +46,15 @@ class Spree::ProductSet < ModelSet
return true if product_related_attrs.blank?
original_supplier = product.supplier_id
product.assign_attributes(product_related_attrs)
product.variants.each do |variant|
validate_presence_of_unit_value(product, variant)
end
product.save if errors.empty?
remove_product_from_order_cycles(product) if original_supplier != product.supplier_id
end
def validate_presence_of_unit_value(product, variant)
@@ -62,6 +64,13 @@ class Spree::ProductSet < ModelSet
product.errors.add(:unit_value, "can't be blank")
end
def remove_product_from_order_cycles(product)
variant_ids = product.variants.map(&:id)
ExchangeVariant.
where(variant_id: variant_ids).
delete_all
end
def update_product_variants(product, attributes)
return true unless attributes[:variants_attributes]
update_variants_attributes(product, attributes[:variants_attributes])

View File

@@ -75,6 +75,31 @@ describe Spree::ProductSet do
end
end
context 'when a different supplier is passed' do
let!(:producer) { create(:enterprise) }
let!(:product) { create(:simple_product) }
let(:collection_hash) do
{
0 => {
id: product.id,
supplier_id: producer.id
}
}
end
let(:distributor) { create(:distributor_enterprise) }
let!(:order_cycle) { create(:simple_order_cycle, variants: [product.variants.first], coordinator: distributor, distributors: [distributor]) }
it 'updates the product and removes the product from order cycles' do
product_set.save
expect(product.reload.attributes).to include(
'supplier_id' => producer.id
)
expect(order_cycle.reload.distributed_variants).to_not include product.variants.first
end
end
context 'when :master_attributes is passed' do
let!(:product) { create(:simple_product) }
let(:collection_hash) { { 0 => { id: product.id } } }