Add document model validations and model shared specs.
This commit is contained in:
@@ -1,8 +1,14 @@
|
||||
class Document < ActiveRecord::Base
|
||||
has_attached_file :attachment
|
||||
|
||||
belongs_to :user
|
||||
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_type, presence: true
|
||||
|
||||
|
||||
@@ -369,7 +369,9 @@ FactoryGirl.define do
|
||||
end
|
||||
|
||||
factory :document do
|
||||
sequence(:title) { |n| "Document title #{n}" }
|
||||
association :user, factory: :user
|
||||
attachment { File.new("spec/fixtures/files/empty.pdf") }
|
||||
|
||||
trait :proposal_document do
|
||||
association :documentable, factory: :proposal
|
||||
|
||||
BIN
spec/fixtures/files/empty.pdf
vendored
Normal file
BIN
spec/fixtures/files/empty.pdf
vendored
Normal file
Binary file not shown.
@@ -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
|
||||
52
spec/shared/models/document_validations.rb
Normal file
52
spec/shared/models/document_validations.rb
Normal 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
|
||||
Reference in New Issue
Block a user