Validate image on creating product from products/new

This commit is contained in:
saunmanoj888
2023-04-18 10:36:30 +05:30
committed by Maikel Linke
parent c3d274c84f
commit 97f51d24b8
5 changed files with 30 additions and 3 deletions

View File

@@ -114,6 +114,7 @@ module Spree
presence: { if: ->(p) { %w(weight volume).include? p.variant_unit } }
validates :variant_unit_name,
presence: { if: ->(p) { p.variant_unit == 'items' } }
validate :validate_image_for_master
attr_accessor :option_values_hash
@@ -474,5 +475,11 @@ module Spree
requested = permalink.presence || permalink_was.presence || name.presence || 'product'
self.permalink = create_unique_permalink(requested.parameterize)
end
def validate_image_for_master
return if master.images.all?(&:valid?)
errors.add(:base, I18n.t('spree.admin.products.image_not_processable'))
end
end
end

View File

@@ -4140,6 +4140,7 @@ See the %{link} to find out more about %{sitename}'s features and to start using
no_payment_via_admin_backend: Paypal payments cannot be captured in the Backoffice
products:
image_upload_error: "Please upload the image in JPG, PNG, GIF, SVG or WEBP format."
image_not_processable: "Image attachment is not a valid image."
new:
title: "New Product"
new_product: "New Product"

View File

@@ -25,7 +25,7 @@ describe Api::V0::ProductImagesController, type: :controller do
}
expect(response.status).to eq 201
expect(product_without_image.images.first.id).to eq json_response['id']
expect(product_without_image.reload.images.first.id).to eq json_response['id']
end
it "updates an existing product image" do
@@ -34,7 +34,7 @@ describe Api::V0::ProductImagesController, type: :controller do
}
expect(response.status).to eq 200
expect(product_with_image.images.first.id).to eq json_response['id']
expect(product_with_image.reload.images.first.id).to eq json_response['id']
end
it "reports errors when saving fails" do

View File

@@ -460,6 +460,25 @@ module Spree
expect(product).not_to be_valid
end
end
describe "#validate_image_for_master" do
let(:product) { build_stubbed(:simple_product) }
context "when the image attached to the master variant is invalid" do
before { product.master.images.new.errors.add(:image_not_processable, "invalid") }
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
context "when master variant is valid" do
it "returns true" do
expect(product).to be_valid
end
end
end
end
describe "callbacks" do

View File

@@ -15,7 +15,7 @@ describe ImageImporter do
}.by(1)
expect(product.images.count).to eq 1
expect(product.images.first.attachment_blob.byte_size).to eq 6274
expect(product.reload.images.first.attachment_blob.byte_size).to eq 6274
end
end
end