We were using helper methods inside the model; we might as well include them in the model and use them from anywhere else. Note we're using a different logic for images and documents methods. That's because for images the logic was defined in the helper methods, but for documents the logic is defined in the Documentable concern. In the past, different documentable classes allowed different content types, while imageable classes have always allowed the same content types. I'm not sure which method is better; for now, I'm leaving it the way it was (except for the fact that we're removing the helper methods).
59 lines
1.6 KiB
Ruby
59 lines
1.6 KiB
Ruby
shared_examples "document validations" do |documentable_factory|
|
|
let!(:document) { build(:document, documentable_factory.to_sym) }
|
|
let!(:maxfilesize) { document.max_file_size }
|
|
let!(:acceptedcontenttypes) { document.accepted_content_types }
|
|
|
|
it "is valid" do
|
|
expect(document).to be_valid
|
|
end
|
|
|
|
it "is not valid without a title" do
|
|
document.title = nil
|
|
|
|
expect(document).not_to be_valid
|
|
end
|
|
|
|
it "is not valid without an attachment" do
|
|
document.attachment = nil
|
|
|
|
expect(document).not_to be_valid
|
|
end
|
|
|
|
it "is valid for all accepted content types" do
|
|
acceptedcontenttypes.each do |content_type|
|
|
extension = content_type.split("/").last
|
|
document.attachment = File.new("spec/fixtures/files/empty.#{extension}")
|
|
|
|
expect(document).to be_valid
|
|
end
|
|
end
|
|
|
|
it "is not valid for attachments larger than documentable max_file_size definition" do
|
|
allow(document).to receive(:attachment_file_size).and_return(maxfilesize.megabytes + 1.byte)
|
|
max_size_error_message = "must be in between 0 Bytes and #{maxfilesize} MB"
|
|
|
|
expect(document).not_to be_valid
|
|
expect(document.errors[:attachment]).to include max_size_error_message
|
|
end
|
|
|
|
it "is not valid without a user_id" do
|
|
document.user_id = nil
|
|
|
|
expect(document).not_to be_valid
|
|
end
|
|
|
|
it "is not valid without a documentable_id" do
|
|
document.save!
|
|
document.documentable_id = nil
|
|
|
|
expect(document).not_to be_valid
|
|
end
|
|
|
|
it "is not valid without a documentable_type" do
|
|
document.save!
|
|
document.documentable_type = nil
|
|
|
|
expect(document).not_to be_valid
|
|
end
|
|
end
|