diff --git a/app/models/person.rb b/app/models/person.rb index ba42250..660b3f4 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -23,6 +23,8 @@ class Person < ApplicationRecord class_name: 'Relationship', inverse_of: :person + has_many :person_comments, dependent: :restrict_with_exception + has_many :passports, dependent: :restrict_with_exception has_many :resident_registrations, dependent: :restrict_with_exception diff --git a/app/models/person_comment.rb b/app/models/person_comment.rb new file mode 100644 index 0000000..b65a84e --- /dev/null +++ b/app/models/person_comment.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class PersonComment < ApplicationRecord + ################ + # Associations # + ################ + + belongs_to :person + + belongs_to :account, optional: true + + ############### + # Validations # + ############### + + validates :text, presence: true, length: { in: 1..10_000 } +end diff --git a/db/migrate/20190715210610_create_person_comments.rb b/db/migrate/20190715210610_create_person_comments.rb new file mode 100644 index 0000000..6a29a55 --- /dev/null +++ b/db/migrate/20190715210610_create_person_comments.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +class CreatePersonComments < ActiveRecord::Migration[6.0] + def change + create_table :person_comments do |t| + t.timestamps null: false + + t.references :person, null: false, index: true, foreign_key: true + t.references :account, null: true, index: true, foreign_key: true + + t.text :text, null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 6d48a32..99269c2 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_07_08_130309) do +ActiveRecord::Schema.define(version: 2019_07_15_210610) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -125,6 +125,16 @@ ActiveRecord::Schema.define(version: 2019_07_08_130309) do t.index ["regional_office_id"], name: "index_people_on_regional_office_id" end + create_table "person_comments", force: :cascade do |t| + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.bigint "person_id", null: false + t.bigint "account_id" + t.text "text", null: false + t.index ["account_id"], name: "index_person_comments_on_account_id" + t.index ["person_id"], name: "index_person_comments_on_person_id" + end + create_table "regional_offices", force: :cascade do |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false @@ -212,6 +222,8 @@ ActiveRecord::Schema.define(version: 2019_07_08_130309) do add_foreign_key "passports", "people" add_foreign_key "people", "contacts_lists" add_foreign_key "people", "regional_offices" + add_foreign_key "person_comments", "accounts" + add_foreign_key "person_comments", "people" add_foreign_key "regional_offices", "federal_subjects" add_foreign_key "relationships", "people" add_foreign_key "relationships", "regional_offices" diff --git a/factories/person_comments.rb b/factories/person_comments.rb new file mode 100644 index 0000000..e10c1fa --- /dev/null +++ b/factories/person_comments.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :person_comment do + association :person, factory: :initial_person + association :account, factory: :superuser_account + + text { Faker::Lorem.paragraph 5, false, 5 } + end +end diff --git a/spec/models/person_comment_spec.rb b/spec/models/person_comment_spec.rb new file mode 100644 index 0000000..a56cc4f --- /dev/null +++ b/spec/models/person_comment_spec.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe PersonComment do + subject { create :person_comment } + + it { is_expected.to belong_to(:person).required } + + it { is_expected.to belong_to(:account).optional } + + describe '#text' do + it { is_expected.to validate_presence_of :text } + + it do + is_expected.to \ + validate_length_of(:text) + .is_at_least(1) + .is_at_most(10_000) + end + end +end diff --git a/spec/models/person_spec.rb b/spec/models/person_spec.rb index 32b2e59..77fcbcb 100644 --- a/spec/models/person_spec.rb +++ b/spec/models/person_spec.rb @@ -29,6 +29,12 @@ RSpec.describe Person do .order(number: :desc) end + it do + is_expected.to \ + have_many(:person_comments) + .dependent(:restrict_with_exception) + end + it do is_expected.to \ have_many(:passports)