1
0
Fork 0

Add model ContactsList

This commit is contained in:
Alex Kotov 2019-06-27 05:21:37 +05:00
parent 0347926d99
commit 6ac0847fed
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
9 changed files with 94 additions and 1 deletions

View file

@ -33,6 +33,8 @@ class Account < ApplicationRecord
belongs_to :person, optional: true
belongs_to :contacts_list
has_one :user, dependent: :restrict_with_exception
has_many :passport_confirmations, dependent: :restrict_with_exception
@ -43,6 +45,10 @@ class Account < ApplicationRecord
after_initialize :generate_nickname
before_validation do
self.contacts_list ||= ContactsList.new
end
before_validation :turn_blanks_into_nils
before_validation :strip_extra_spaces
@ -54,6 +60,8 @@ class Account < ApplicationRecord
validates :person, allow_nil: true, uniqueness: true
validates :contacts_list, uniqueness: true
validates :nickname,
presence: true,
length: { in: 3..36 },

View file

@ -0,0 +1,11 @@
# frozen_string_literal: true
class ContactsList < ApplicationRecord
################
# Associations #
################
has_one :account, dependent: :restrict_with_exception
has_one :person, dependent: :restrict_with_exception
end

View file

@ -9,6 +9,8 @@ class Person < ApplicationRecord
belongs_to :regional_office, optional: true
belongs_to :contacts_list
has_one :account, dependent: :restrict_with_exception
has_many :relationships,
@ -24,4 +26,18 @@ class Person < ApplicationRecord
has_many :passports, dependent: :restrict_with_exception
has_many :resident_registrations, dependent: :restrict_with_exception
###############
# Validations #
###############
validates :contacts_list, uniqueness: true
#############
# Callbacks #
#############
before_validation do
self.contacts_list ||= ContactsList.new
end
end

View file

@ -0,0 +1,19 @@
# frozen_string_literal: true
class CreateContactsLists < ActiveRecord::Migration[6.0]
def change
create_table :contacts_lists do |t|
t.timestamps null: false
end
# rubocop:disable Rails/NotNullColumn
add_reference :accounts, :contacts_list,
null: false, index: { unique: true }, foreign_key: true
add_reference :people, :contacts_list,
null: false, index: { unique: true }, foreign_key: true
# rubocop:enable Rails/NotNullColumn
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2019_06_23_160130) do
ActiveRecord::Schema.define(version: 2019_06_27_000456) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -35,6 +35,8 @@ ActiveRecord::Schema.define(version: 2019_06_23_160130) do
t.string "nickname", null: false
t.text "biography"
t.string "public_name"
t.bigint "contacts_list_id", null: false
t.index ["contacts_list_id"], name: "index_accounts_on_contacts_list_id", unique: true
t.index ["person_id"], name: "index_accounts_on_person_id", unique: true
end
@ -59,6 +61,11 @@ ActiveRecord::Schema.define(version: 2019_06_23_160130) do
t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
end
create_table "contacts_lists", force: :cascade do |t|
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "federal_subjects", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
@ -114,6 +121,8 @@ ActiveRecord::Schema.define(version: 2019_06_23_160130) do
t.integer "sex", null: false
t.date "date_of_birth", null: false
t.string "place_of_birth", null: false
t.bigint "contacts_list_id", null: false
t.index ["contacts_list_id"], name: "index_people_on_contacts_list_id", unique: true
t.index ["regional_office_id"], name: "index_people_on_regional_office_id"
end
@ -196,11 +205,13 @@ ActiveRecord::Schema.define(version: 2019_06_23_160130) do
add_foreign_key "account_roles", "accounts"
add_foreign_key "account_roles", "roles"
add_foreign_key "accounts", "contacts_lists"
add_foreign_key "accounts", "people"
add_foreign_key "passport_confirmations", "accounts"
add_foreign_key "passport_confirmations", "passports"
add_foreign_key "passport_maps", "passports"
add_foreign_key "passports", "people"
add_foreign_key "people", "contacts_lists"
add_foreign_key "people", "regional_offices"
add_foreign_key "regional_offices", "federal_subjects"
add_foreign_key "relationships", "people"

View file

@ -0,0 +1,5 @@
# frozen_string_literal: true
FactoryBot.define do
factory :contacts_list
end

View file

@ -7,6 +7,8 @@ RSpec.describe Account do
it { is_expected.to belong_to(:person).optional }
xit { is_expected.to belong_to(:contacts_list).required }
it do
is_expected.to \
have_one(:user)

View file

@ -0,0 +1,19 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe ContactsList do
subject { create :contacts_list }
it do
is_expected.to \
have_one(:account)
.dependent(:restrict_with_exception)
end
it do
is_expected.to \
have_one(:person)
.dependent(:restrict_with_exception)
end
end

View file

@ -9,6 +9,8 @@ RSpec.describe Person do
it { is_expected.to belong_to(:regional_office).optional }
xit { is_expected.to belong_to(:contacts_list).required }
it { is_expected.to have_one(:account).dependent(:restrict_with_exception) }
it do