Do not commit to db unchanged products is bulk save

This commit is contained in:
isidzukuri
2024-05-24 11:56:16 +03:00
committed by David Cook
parent a64bf68d54
commit 7565825b61
2 changed files with 43 additions and 3 deletions

View File

@@ -67,11 +67,12 @@ module Sets
product.assign_attributes(product_related_attrs)
return true unless product.changed?
validate_presence_of_unit_value_in_product(product)
changed = product.changed?
success = product.errors.empty? && product.save
count_result(success && changed)
count_result(success)
success
end
@@ -104,7 +105,8 @@ module Sets
def create_or_update_variant(product, variant_attributes)
variant = find_model(product.variants, variant_attributes[:id])
if variant.present?
variant.update(variant_attributes.except(:id))
variant.assign_attributes(variant_attributes.except(:id))
variant.update(variant_attributes.except(:id)) if variant.changed?
else
variant = create_variant(product, variant_attributes)
end

View File

@@ -134,6 +134,27 @@ RSpec.describe Sets::ProductSet do
end
end
end
context "when product attributes are not changed" do
let(:collection_hash) {
{ 0 => { id: product.id, name: product.name } }
}
it 'returns true' do
is_expected.to eq true
end
it 'does not increase saved_count' do
subject
expect(product_set.saved_count).to eq 0
end
it 'does not update any product by calling save' do
expect_any_instance_of(Spree::Product).not_to receive(:save)
subject
end
end
end
describe "updating a product's variants" do
@@ -190,6 +211,23 @@ RSpec.describe Sets::ProductSet do
include_examples "nothing saved"
end
context "when attributes are not changed" do
let(:variant_attributes) { { sku: variant.sku } }
before { variant }
it 'updates product by calling save' do
expect_any_instance_of(Spree::Variant).not_to receive(:save)
subject
end
it 'does not increase saved_count' do
subject
expect(product_set.saved_count).to eq 0
end
end
context "when products attributes are also updated" do
let(:product_attributes) {
{ sku: "prod_sku" }