Skip to content

Commit

Permalink
enable more possible upload file formats
Browse files Browse the repository at this point in the history
  • Loading branch information
asmega committed Nov 23, 2023
1 parent 2056be4 commit 235e8b8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
10 changes: 8 additions & 2 deletions app/validators/file_upload_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@ class FileUploadValidator < ActiveModel::EachValidator
".avif" => "image/avif",
".doc" => "application/msword",
".docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
".eml" => "message/rfc822",
".gif" => "image/gif",
".heic" => "image/heic",
".heif" => "image/heif",
".jpg" => "image/jpeg",
".jpeg" => "image/jpeg",
".m4a" => "audio/m4a",
".mov" => "video/quicktime",
".mp3" => "audio/mpeg",
".mp4" => "video/mp4",
".mov" => "video/quicktime",
".msg" => "application/vnd.ms-outlook",
".pdf" => "application/pdf",
".png" => "image/png",
".rtf" => "application/rtf",
".txt" => "text/plain",
".webp" => "image/webp"
".webp" => "image/webp",
".xls" => "application/vnd.ms-excel",
".xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
}.freeze

def validate_each(record, attribute, uploaded_files)
Expand Down
40 changes: 32 additions & 8 deletions spec/validators/file_upload_validator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
require "rails_helper"

RSpec.describe FileUploadValidator do
subject(:model) { Validatable.new }

let(:files) { nil }
subject(:model) do
instance = klass.new
instance.files = files
instance
end

before do
stub_const("Validatable", Class.new).class_eval do
let(:klass) do
Class.new do
include ActiveModel::Validations
attr_accessor :files
validates :files, file_upload: true
end
model.files = files
end

context "with a valid file" do
Expand Down Expand Up @@ -60,9 +61,32 @@
it { is_expected.to be_invalid }
end

it "supports common media and document file types" do
it "supports selected media and document file types" do
expect(described_class::CONTENT_TYPES.keys).to eq(
%w[.apng .avif .doc .docx .gif .heic .heif .jpg .jpeg .mp3 .mp4 .mov .pdf .png .txt .webp]
%w[
.apng
.avif
.doc
.docx
.eml
.gif
.heic
.heif
.jpg
.jpeg
.m4a
.mov
.mp3
.mp4
.msg
.pdf
.png
.rtf
.txt
.webp
.xls
.xlsx
]
)
end
end

0 comments on commit 235e8b8

Please sign in to comment.