diff --git a/app/models/concerns/documentable.rb b/app/models/concerns/documentable.rb new file mode 100644 index 000000000..82c6c0b6e --- /dev/null +++ b/app/models/concerns/documentable.rb @@ -0,0 +1,8 @@ +module Documentable + extend ActiveSupport::Concern + + included do + has_many :documents, as: :documentable, dependent: :destroy + end + +end diff --git a/app/models/document.rb b/app/models/document.rb new file mode 100644 index 000000000..e8dd92b56 --- /dev/null +++ b/app/models/document.rb @@ -0,0 +1,9 @@ +class Document < ActiveRecord::Base + belongs_to :user + belongs_to :documentable, polymorphic: true + + validates :user_id, presence: true + validates :documentable_id, presence: true + validates :documentable_type, presence: true + +end diff --git a/db/migrate/20170720092638_create_documents.rb b/db/migrate/20170720092638_create_documents.rb new file mode 100644 index 000000000..b7d1e287c --- /dev/null +++ b/db/migrate/20170720092638_create_documents.rb @@ -0,0 +1,14 @@ +class CreateDocuments < ActiveRecord::Migration + def change + create_table :documents do |t| + t.string :title + t.attachment :attachment + t.references :user, index: true, foreign_key: true + t.references :documentable, polymorphic: true, index: true + + t.timestamps null: false + end + + add_index :documents, [:user_id, :documentable_type, :documentable_id], name: "access_documents" + end +end diff --git a/db/schema.rb b/db/schema.rb index 91a26adeb..cccf10b9e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170719174326) do +ActiveRecord::Schema.define(version: 20170720092638) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -298,6 +298,23 @@ ActiveRecord::Schema.define(version: 20170719174326) do t.datetime "updated_at", null: false end + create_table "documents", force: :cascade do |t| + t.string "title" + t.string "attachment_file_name" + t.string "attachment_content_type" + t.integer "attachment_file_size" + t.datetime "attachment_updated_at" + t.integer "user_id" + t.integer "documentable_id" + t.string "documentable_type" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_index "documents", ["documentable_type", "documentable_id"], name: "index_documents_on_documentable_type_and_documentable_id", using: :btree + add_index "documents", ["user_id", "documentable_type", "documentable_id"], name: "access_documents", using: :btree + add_index "documents", ["user_id"], name: "index_documents_on_user_id", using: :btree + create_table "failed_census_calls", force: :cascade do |t| t.integer "user_id" t.string "document_number" @@ -919,7 +936,7 @@ ActiveRecord::Schema.define(version: 20170719174326) do t.boolean "email_digest", default: true t.boolean "email_on_direct_message", default: true t.boolean "official_position_badge", default: false - t.datetime "password_changed_at", default: '2017-06-22 11:21:30', null: false + t.datetime "password_changed_at", default: '2017-07-20 09:31:51', null: false t.boolean "created_from_signature", default: false t.integer "failed_email_digests_count", default: 0 t.text "former_users_data_log", default: "" @@ -1014,6 +1031,7 @@ ActiveRecord::Schema.define(version: 20170719174326) do add_foreign_key "administrators", "users" add_foreign_key "annotations", "legacy_legislations" add_foreign_key "annotations", "users" + add_foreign_key "documents", "users" add_foreign_key "failed_census_calls", "poll_officers" add_foreign_key "failed_census_calls", "users" add_foreign_key "flags", "users" diff --git a/spec/factories.rb b/spec/factories.rb index 24c0767d4..0a82fa365 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -368,6 +368,18 @@ FactoryGirl.define do end end + factory :document do + association :user, factory: :user + + trait :proposal_document do + association :documentable, factory: :proposal + end + + trait :budget_investment_document do + association :documentable, factory: :budget_investment + end + end + factory :comment do association :commentable, factory: :debate user diff --git a/spec/models/document_spec.rb b/spec/models/document_spec.rb new file mode 100644 index 000000000..bc5d4e923 --- /dev/null +++ b/spec/models/document_spec.rb @@ -0,0 +1,8 @@ +require 'rails_helper' + +describe Document do + + it_behaves_like "document validations", "budget_investment_document" + it_behaves_like "document validations", "proposal_document" + +end diff --git a/spec/shared/models/document_validatable.rb b/spec/shared/models/document_validatable.rb new file mode 100644 index 000000000..2c6cdb568 --- /dev/null +++ b/spec/shared/models/document_validatable.rb @@ -0,0 +1,27 @@ +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