Add model PersonComment
This commit is contained in:
parent
31a3679bd7
commit
96de699ec3
7 changed files with 84 additions and 1 deletions
|
@ -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
|
||||
|
|
17
app/models/person_comment.rb
Normal file
17
app/models/person_comment.rb
Normal file
|
@ -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
|
14
db/migrate/20190715210610_create_person_comments.rb
Normal file
14
db/migrate/20190715210610_create_person_comments.rb
Normal file
|
@ -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
|
14
db/schema.rb
14
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"
|
||||
|
|
10
factories/person_comments.rb
Normal file
10
factories/person_comments.rb
Normal file
|
@ -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
|
22
spec/models/person_comment_spec.rb
Normal file
22
spec/models/person_comment_spec.rb
Normal file
|
@ -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
|
|
@ -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)
|
||||
|
|
Reference in a new issue