1
0
Fork 0

Fix associations RegionalOffice#people; Person#regional_office

This commit is contained in:
Alex Kotov 2019-07-20 14:53:51 +05:00
parent ce51091dbd
commit 86bfba4b16
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
8 changed files with 61 additions and 36 deletions

View file

@ -24,6 +24,12 @@ class Person < ApplicationRecord
inverse_of: :person,
dependent: :restrict_with_exception
has_one :regional_office,
inverse_of: :people,
through: :current_relationship,
source: :regional_office,
dependent: :restrict_with_exception
has_many :person_comments, dependent: :restrict_with_exception
has_many :passports, dependent: :restrict_with_exception

View file

@ -7,10 +7,14 @@ class RegionalOffice < ApplicationRecord
belongs_to :federal_subject
has_many :people, dependent: :restrict_with_exception
has_many :relationships, dependent: :restrict_with_exception
has_many :people,
inverse_of: :regional_office,
through: :relationships,
source: :person,
dependent: :restrict_with_exception
###############
# Validations #
###############

View file

@ -1,7 +0,0 @@
# frozen_string_literal: true
class AddRegionalOfficeToPeople < ActiveRecord::Migration[5.2]
def change
add_reference :people, :regional_office, foreign_key: true
end
end

View file

@ -295,7 +295,6 @@ CREATE TABLE public.people (
id bigint NOT NULL,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL,
regional_office_id bigint,
first_name character varying NOT NULL,
middle_name character varying,
last_name character varying NOT NULL,
@ -868,13 +867,6 @@ CREATE INDEX index_passports_on_person_id ON public.passports USING btree (perso
CREATE UNIQUE INDEX index_people_on_contacts_list_id ON public.people USING btree (contacts_list_id);
--
-- Name: index_people_on_regional_office_id; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX index_people_on_regional_office_id ON public.people USING btree (regional_office_id);
--
-- Name: index_person_comments_on_account_id; Type: INDEX; Schema: public; Owner: -
--
@ -1074,14 +1066,6 @@ ALTER TABLE ONLY public.people
ADD CONSTRAINT fk_rails_cb9b5a21ec FOREIGN KEY (contacts_list_id) REFERENCES public.contacts_lists(id);
--
-- Name: people fk_rails_cc7540413a; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.people
ADD CONSTRAINT fk_rails_cc7540413a FOREIGN KEY (regional_office_id) REFERENCES public.regional_offices(id);
--
-- Name: person_comments fk_rails_d3ef7dc526; Type: FK CONSTRAINT; Schema: public; Owner: -
--
@ -1115,7 +1099,6 @@ SET search_path TO "$user", public;
INSERT INTO "schema_migrations" (version) VALUES
('20181129203927'),
('20181130024918'),
('20181215040559'),
('20181215053720'),
('20190129013754'),
('20190129015721'),

View file

@ -6,6 +6,9 @@ FactoryBot.define do
FederalSubject.find_or_initialize_by english_name: english_name
end
sequence(:id) { |n| n + 100 }
sequence(:number) { |n| n + 100 }
english_name do
I18n.with_locale :en do
Faker::Address.unique.state
@ -14,8 +17,6 @@ FactoryBot.define do
native_name { english_name }
sequence :number
timezone { "#{[nil, :-].sample}#{rand(0..11).to_s.rjust(2, '0')}:00:00" }
end
end

View file

@ -2,6 +2,10 @@
FactoryBot.define do
factory :initial_person, class: Person do
transient do
regional_office { create :regional_office }
end
association :contacts_list, factory: :empty_contacts_list
first_name { Faker::Name.first_name }
@ -12,7 +16,27 @@ FactoryBot.define do
place_of_birth { Faker::Address.city }
end
factory :supporter_person, parent: :initial_person
factory :member_person, parent: :supporter_person
factory :excluded_person, parent: :member_person
factory :supporter_person, parent: :initial_person do
after :create do |person, evaluator|
create :supporter_relationship,
person: person,
regional_office: evaluator.regional_office
end
end
factory :member_person, parent: :supporter_person do
after :create do |person, evaluator|
create :member_relationship,
person: person,
regional_office: evaluator.regional_office
end
end
factory :excluded_person, parent: :member_person do
after :create do |person, evaluator|
create :excluded_member_relationship,
person: person,
regional_office: evaluator.regional_office
end
end
end

View file

@ -3,12 +3,10 @@
require 'rails_helper'
RSpec.describe Person do
subject { create :member_person }
subject { create :initial_person }
it_behaves_like 'nameable'
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) }
@ -30,6 +28,15 @@ RSpec.describe Person do
.order(from_date: :desc)
end
it do
is_expected.to \
have_one(:regional_office)
.inverse_of(:people)
.through(:current_relationship)
.source(:regional_office)
.dependent(:restrict_with_exception)
end
it do
is_expected.to \
have_many(:person_comments)

View file

@ -7,14 +7,21 @@ RSpec.describe RegionalOffice do
it { is_expected.to belong_to :federal_subject }
it { is_expected.to have_many(:people).dependent(:restrict_with_exception) }
it do
is_expected.to \
have_many(:relationships)
.dependent(:restrict_with_exception)
end
it do
is_expected.to \
have_many(:people)
.inverse_of(:regional_office)
.through(:relationships)
.source(:person)
.dependent(:restrict_with_exception)
end
it do
is_expected.to \
validate_presence_of(:federal_subject)