diff --git a/app/services/sets/product_set.rb b/app/services/sets/product_set.rb index ba3fb40240..d72e391beb 100644 --- a/app/services/sets/product_set.rb +++ b/app/services/sets/product_set.rb @@ -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 diff --git a/spec/services/sets/product_set_spec.rb b/spec/services/sets/product_set_spec.rb index b60dd0f2a5..3d2181c493 100644 --- a/spec/services/sets/product_set_spec.rb +++ b/spec/services/sets/product_set_spec.rb @@ -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" }