diff --git a/app/models/document.rb b/app/models/document.rb index e8dd92b56..688540c09 100644 --- a/app/models/document.rb +++ b/app/models/document.rb @@ -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 diff --git a/spec/factories.rb b/spec/factories.rb index 0a82fa365..7b1f6a417 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -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 diff --git a/spec/fixtures/files/empty.pdf b/spec/fixtures/files/empty.pdf new file mode 100644 index 000000000..de2e38148 Binary files /dev/null and b/spec/fixtures/files/empty.pdf differ diff --git a/spec/shared/models/document_validatable.rb b/spec/shared/models/document_validatable.rb deleted file mode 100644 index 2c6cdb568..000000000 --- a/spec/shared/models/document_validatable.rb +++ /dev/null @@ -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 diff --git a/spec/shared/models/document_validations.rb b/spec/shared/models/document_validations.rb new file mode 100644 index 000000000..a5cff53c5 --- /dev/null +++ b/spec/shared/models/document_validations.rb @@ -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