Add document model validations and model shared specs.

This commit is contained in:
Senén Rodero Rodríguez
2017-07-21 11:27:40 +02:00
parent a141c82e33
commit 34d06dad04
5 changed files with 61 additions and 28 deletions

View File

@@ -1,8 +1,14 @@
class Document < ActiveRecord::Base class Document < ActiveRecord::Base
has_attached_file :attachment
belongs_to :user belongs_to :user
belongs_to :documentable, polymorphic: true belongs_to :documentable, polymorphic: true
validates :user_id, presence: true validates_attachment :attachment, presence: true,
content_type: { content_type: "application/pdf" },
size: { in: 0..3.megabytes }
validates :title, presence: true
validates :user, presence: true
validates :documentable_id, presence: true validates :documentable_id, presence: true
validates :documentable_type, presence: true validates :documentable_type, presence: true

View File

@@ -369,7 +369,9 @@ FactoryGirl.define do
end end
factory :document do factory :document do
sequence(:title) { |n| "Document title #{n}" }
association :user, factory: :user association :user, factory: :user
attachment { File.new("spec/fixtures/files/empty.pdf") }
trait :proposal_document do trait :proposal_document do
association :documentable, factory: :proposal association :documentable, factory: :proposal

BIN
spec/fixtures/files/empty.pdf vendored Normal file

Binary file not shown.

View File

@@ -1,27 +0,0 @@
shared_examples "document validations" do |documentable_factory|
let(:documentable) { build(:document, documentable_factory.to_sym) }
it "should be valid" do
expect(documentable).to be_valid
end
it "should not be valid without a user_id" do
documentable.user_id = nil
expect(documentable).to_not be_valid
end
it "should not be valid without a documentable_id" do
documentable.documentable_id = nil
expect(documentable).to_not be_valid
end
it "should not be valid without a documentable_type" do
documentable.documentable_type = nil
expect(documentable).to_not be_valid
end
end

View File

@@ -0,0 +1,52 @@
shared_examples "document validations" do |documentable_factory|
let(:document) { build(:document, documentable_factory.to_sym) }
it "should be valid" do
expect(document).to be_valid
end
it "should not be valid without a title" do
document.title = nil
expect(document).to_not be_valid
end
it "should not be valid without an attachment" do
document.attachment = nil
expect(document).to_not be_valid
end
it "should not be valid for attachment images" do
document.attachment = File.new("spec/fixtures/files/logo_header.png")
expect(document).to_not be_valid
end
it "should not be valid for attachment 3MB" do
document.stub(:attachment_file_size).and_return(3.1.megabytes)
document.should_not be_valid
expect(document.errors[:attachment]).to include "must be in between 0 Bytes and 3 MB"
end
it "should not be valid without a user_id" do
document.user_id = nil
expect(document).to_not be_valid
end
it "should not be valid without a documentable_id" do
document.documentable_id = nil
expect(document).to_not be_valid
end
it "should not be valid without a documentable_type" do
document.documentable_type = nil
expect(document).to_not be_valid
end
end