From d229db4bc1589abd1130d62d580162c2d8cff438 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sat, 15 Dec 2018 09:09:43 +0500 Subject: [PATCH] Add association Person#regional_office --- app/models/person.rb | 2 ++ app/models/regional_office.rb | 2 ++ db/migrate/20181215040559_add_regional_office_to_people.rb | 7 +++++++ db/schema.rb | 5 ++++- factories/people.rb | 1 + spec/models/person_spec.rb | 4 +++- spec/models/regional_office_spec.rb | 2 ++ 7 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20181215040559_add_regional_office_to_people.rb diff --git a/app/models/person.rb b/app/models/person.rb index d9986e4..38698e1 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true class Person < ApplicationRecord + belongs_to :regional_office + has_one :account, dependent: :restrict_with_exception end diff --git a/app/models/regional_office.rb b/app/models/regional_office.rb index ca563b8..27d7093 100644 --- a/app/models/regional_office.rb +++ b/app/models/regional_office.rb @@ -5,5 +5,7 @@ class RegionalOffice < ApplicationRecord has_many :membership_apps, through: :country_state + has_many :people, dependent: :restrict_with_exception + validates :country_state_id, uniqueness: true end diff --git a/db/migrate/20181215040559_add_regional_office_to_people.rb b/db/migrate/20181215040559_add_regional_office_to_people.rb new file mode 100644 index 0000000..36945e8 --- /dev/null +++ b/db/migrate/20181215040559_add_regional_office_to_people.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddRegionalOfficeToPeople < ActiveRecord::Migration[5.2] + def change + add_reference :people, :regional_office, foreign_key: true + end +end diff --git a/db/schema.rb b/db/schema.rb index cc20ad7..65bd8c8 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: 2018_12_13_053336) do +ActiveRecord::Schema.define(version: 2018_12_15_040559) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -126,6 +126,8 @@ ActiveRecord::Schema.define(version: 2018_12_13_053336) do create_table "people", force: :cascade do |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.bigint "regional_office_id" + t.index ["regional_office_id"], name: "index_people_on_regional_office_id" end create_table "regional_offices", force: :cascade do |t| @@ -214,6 +216,7 @@ ActiveRecord::Schema.define(version: 2018_12_13_053336) do add_foreign_key "passport_confirmations", "accounts" add_foreign_key "passport_confirmations", "passports" add_foreign_key "passport_maps", "passports" + add_foreign_key "people", "regional_offices" add_foreign_key "regional_offices", "country_states" add_foreign_key "user_omniauths", "users" add_foreign_key "users", "accounts" diff --git a/factories/people.rb b/factories/people.rb index a3d0841..9be022d 100644 --- a/factories/people.rb +++ b/factories/people.rb @@ -2,5 +2,6 @@ FactoryBot.define do factory :person do + association :regional_office end end diff --git a/spec/models/person_spec.rb b/spec/models/person_spec.rb index 5c88d38..5fefcad 100644 --- a/spec/models/person_spec.rb +++ b/spec/models/person_spec.rb @@ -5,11 +5,13 @@ require 'rails_helper' RSpec.describe Person do subject { create :person } + it { is_expected.to belong_to(:regional_office) } + it do is_expected.to \ have_one(:account) .dependent(:restrict_with_exception) end - pending "add some examples to (or delete) #{__FILE__}" + it { is_expected.not_to validate_presence_of :regional_office } end diff --git a/spec/models/regional_office_spec.rb b/spec/models/regional_office_spec.rb index 0c1e16f..fc65b67 100644 --- a/spec/models/regional_office_spec.rb +++ b/spec/models/regional_office_spec.rb @@ -9,6 +9,8 @@ RSpec.describe RegionalOffice do it { is_expected.to have_many(:membership_apps).through(:country_state) } + it { is_expected.to have_many(:people).dependent(:restrict_with_exception) } + it do is_expected.to \ validate_presence_of(:country_state)