Do not validate the attachment file size ...

when the attachment has not changed
This commit is contained in:
Julian Herrero
2023-08-07 13:35:41 +02:00
committed by Senén Rodero Rodríguez
parent db759bdd08
commit 5f29680186
2 changed files with 45 additions and 1 deletions

View File

@@ -18,7 +18,7 @@ module Attachable
}, },
file_size: { file_size: {
less_than_or_equal_to: ->(record) { record.max_file_size.megabytes }, less_than_or_equal_to: ->(record) { record.max_file_size.megabytes },
if: -> { association_class && attachment.attached? }, if: -> { association_class && attachment.attached? && attachment.new_record? },
message: ->(record, *) do message: ->(record, *) do
I18n.t("#{record.model_name.plural}.errors.messages.in_between", I18n.t("#{record.model_name.plural}.errors.messages.in_between",
min: "0 Bytes", min: "0 Bytes",

View File

@@ -14,4 +14,48 @@ describe Attachable do
expect(build(:image).file_path).to include "storage/tenants/image-master/" expect(build(:image).file_path).to include "storage/tenants/image-master/"
end end
context "file size validation" do
it "is not applied when the image attachment has not changed" do
image = create(:image, :proposal_image)
expect(image.valid?).to be(true)
Setting["uploads.images.max_size"] = 0.1
expect(image.valid?).to be(true)
end
it "is applied when the image attachment changes" do
image = create(:image, :proposal_image)
expect(image.valid?).to be(true)
Setting["uploads.images.max_size"] = 0.1
image.attachment = Rack::Test::UploadedFile.new("spec/fixtures/files/clippy.png")
expect(image.valid?).to be(false)
end
it "is not applied when the document attachment has not changed" do
document = create(:document, :proposal_document)
expect(document.valid?).to be(true)
Setting["uploads.documents.max_size"] = 0.1
expect(document.valid?).to be(true)
end
it "is applied when the document attachment changes" do
document = create(:document, :proposal_document)
expect(document.valid?).to be(true)
Setting["uploads.documents.max_size"] = 0.1
document.attachment = Rack::Test::UploadedFile.new("spec/fixtures/files/clippy.pdf")
expect(document.valid?).to be(false)
end
end
end end