Only validate an image if it has been changed

Best viewed with whitespace ignored.
This commit is contained in:
David Cook
2023-10-10 21:38:34 +11:00
parent 31b5be73f9
commit b42cf9735f
2 changed files with 13 additions and 5 deletions

View File

@@ -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

View File

@@ -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