Create new polymorphic model Document.
This commit is contained in:
8
app/models/concerns/documentable.rb
Normal file
8
app/models/concerns/documentable.rb
Normal file
@@ -0,0 +1,8 @@
|
||||
module Documentable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
has_many :documents, as: :documentable, dependent: :destroy
|
||||
end
|
||||
|
||||
end
|
||||
9
app/models/document.rb
Normal file
9
app/models/document.rb
Normal file
@@ -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
|
||||
14
db/migrate/20170720092638_create_documents.rb
Normal file
14
db/migrate/20170720092638_create_documents.rb
Normal file
@@ -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
|
||||
22
db/schema.rb
22
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"
|
||||
|
||||
@@ -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
|
||||
|
||||
8
spec/models/document_spec.rb
Normal file
8
spec/models/document_spec.rb
Normal file
@@ -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
|
||||
27
spec/shared/models/document_validatable.rb
Normal file
27
spec/shared/models/document_validatable.rb
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user