Do not validate the attachment file content type ...
when the attachment has not changed
This commit is contained in:
@@ -9,7 +9,7 @@ module Attachable
|
||||
presence: true,
|
||||
file_content_type: {
|
||||
allow: ->(record) { record.accepted_content_types },
|
||||
if: -> { association_class && attachment.attached? },
|
||||
if: -> { association_class && attachment.attached? && attachment.new_record? },
|
||||
message: ->(record, *) do
|
||||
I18n.t("#{record.model_name.plural}.errors.messages.wrong_content_type",
|
||||
content_type: record.attachment_content_type,
|
||||
|
||||
@@ -58,4 +58,48 @@ describe Attachable do
|
||||
expect(document.valid?).to be(false)
|
||||
end
|
||||
end
|
||||
|
||||
context "file content types 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.content_types"] = "image/gif"
|
||||
|
||||
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.content_types"] = "image/gif"
|
||||
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.content_types"] = "text/csv"
|
||||
|
||||
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.content_types"] = "text/csv"
|
||||
document.attachment = Rack::Test::UploadedFile.new("spec/fixtures/files/clippy.pdf")
|
||||
|
||||
expect(document.valid?).to be(false)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user