From b42cf9735f7bfb9c89e0fbd9e4ab6fd928b89c7b Mon Sep 17 00:00:00 2001 From: David Cook Date: Tue, 10 Oct 2023 21:38:34 +1100 Subject: [PATCH] Only validate an image if it has been changed Best viewed with whitespace ignored. --- app/models/spree/product.rb | 2 +- spec/models/spree/product_spec.rb | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/app/models/spree/product.rb b/app/models/spree/product.rb index 7f13d46c0d..a8cb206056 100755 --- a/app/models/spree/product.rb +++ b/app/models/spree/product.rb @@ -300,7 +300,7 @@ module Spree end def validate_image - return if image.blank? || image.valid? + return if image.blank? || !image.changed? || image.valid? errors.add(:base, I18n.t('spree.admin.products.image_not_processable')) end diff --git a/spec/models/spree/product_spec.rb b/spec/models/spree/product_spec.rb index 09025a033d..558433964f 100644 --- a/spec/models/spree/product_spec.rb +++ b/spec/models/spree/product_spec.rb @@ -292,11 +292,19 @@ module Spree subject(:product) { create(:product_with_image) } context "when the image is invalid" do - before { expect(product.image).to receive(:valid?).and_return(false) } + before { allow(product.image).to receive(:valid?).and_return(false) } - it "adds an error message to the base object" do - expect(product).not_to be_valid - expect(product.errors[:base]).to include('Image attachment is not a valid image.') + context "and has been changed" do + before { expect(product.image).to receive(:changed?).and_return(true) } + + it "adds an error message to the base object" do + expect(product).not_to be_valid + expect(product.errors[:base]).to include('Image attachment is not a valid image.') + end + end + + it "ignores if unchanged" do + expect(product).to be_valid end end