diff --git a/app/models/account.rb b/app/models/account.rb index 957f2ec..ad1ca1e 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -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 }, diff --git a/app/models/contacts_list.rb b/app/models/contacts_list.rb new file mode 100644 index 0000000..b1b4b4f --- /dev/null +++ b/app/models/contacts_list.rb @@ -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 diff --git a/app/models/person.rb b/app/models/person.rb index 8e14244..ae837e5 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -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 diff --git a/db/migrate/20190627000456_create_contacts_lists.rb b/db/migrate/20190627000456_create_contacts_lists.rb new file mode 100644 index 0000000..777914b --- /dev/null +++ b/db/migrate/20190627000456_create_contacts_lists.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 9ca7738..b81fbe7 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_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" diff --git a/factories/contacts_lists.rb b/factories/contacts_lists.rb new file mode 100644 index 0000000..c50200f --- /dev/null +++ b/factories/contacts_lists.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :contacts_list +end diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb index 9b8a5fa..a6c80e0 100644 --- a/spec/models/account_spec.rb +++ b/spec/models/account_spec.rb @@ -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) diff --git a/spec/models/contacts_list_spec.rb b/spec/models/contacts_list_spec.rb new file mode 100644 index 0000000..b878c4f --- /dev/null +++ b/spec/models/contacts_list_spec.rb @@ -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 diff --git a/spec/models/person_spec.rb b/spec/models/person_spec.rb index 233c528..32b2e59 100644 --- a/spec/models/person_spec.rb +++ b/spec/models/person_spec.rb @@ -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