1
0
Fork 0

Add column Relationship#federal_secretary_flag

This commit is contained in:
Alex Kotov 2019-07-26 08:22:12 +05:00
parent 8160514840
commit 9a8eb2651c
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
5 changed files with 134 additions and 0 deletions

View file

@ -10,6 +10,8 @@ class Relationship < ApplicationRecord
regional_supervisor
]
pg_enum :federal_secretary_flag, %i[federal_secretary]
################
# Associations #
################
@ -26,6 +28,9 @@ class Relationship < ApplicationRecord
scope :federal_supervisors, -> { where(role: :federal_supervisor) }
scope :federal_secretaries,
-> { where(federal_secretary_flag: :federal_secretary) }
###############
# Validations #
###############
@ -35,4 +40,9 @@ class Relationship < ApplicationRecord
validates :status, presence: true
validates :role, absence: { unless: :member? }
validates :federal_secretary_flag,
allow_nil: true,
absence: { unless: :federal_manager? },
uniqueness: true
end

View file

@ -22,6 +22,8 @@ private
regional_supervisor
]
enum :relationship_federal_secretary_flag, %i[federal_secretary]
enum :person_comment_origin, %i[
general_comments
first_contact_date
@ -277,6 +279,11 @@ private
t.column :status, :relationship_status, null: false, index: true
t.column :role, :relationship_role, null: true, index: true
t.column :federal_secretary_flag,
:relationship_federal_secretary_flag,
null: true,
index: { unique: true }
t.index %i[person_id from_date], unique: true
end
end
@ -290,6 +297,10 @@ private
status = 'member' OR role IS NULL
SQL
constraint :relationships, :federal_secretary_flag, <<~SQL
federal_secretary_flag IS NULL OR role = 'federal_manager'
SQL
constraint :accounts, :guest_token, <<~SQL
is_guest_token(guest_token)
SQL

View file

@ -38,6 +38,15 @@ CREATE TYPE public.person_comment_origin AS ENUM (
);
--
-- Name: relationship_federal_secretary_flag; Type: TYPE; Schema: public; Owner: -
--
CREATE TYPE public.relationship_federal_secretary_flag AS ENUM (
'federal_secretary'
);
--
-- Name: relationship_role; Type: TYPE; Schema: public; Owner: -
--
@ -566,7 +575,9 @@ CREATE TABLE public.relationships (
until_date date,
status public.relationship_status NOT NULL,
role public.relationship_role,
federal_secretary_flag public.relationship_federal_secretary_flag,
CONSTRAINT dates CHECK (((until_date IS NULL) OR (from_date < until_date))),
CONSTRAINT federal_secretary_flag CHECK (((federal_secretary_flag IS NULL) OR (role = 'federal_manager'::public.relationship_role))),
CONSTRAINT role CHECK (((status = 'member'::public.relationship_status) OR (role IS NULL)))
);
@ -1066,6 +1077,13 @@ CREATE INDEX index_person_comments_on_person_id ON public.person_comments USING
CREATE UNIQUE INDEX index_regional_offices_on_federal_subject_id ON public.regional_offices USING btree (federal_subject_id);
--
-- Name: index_relationships_on_federal_secretary_flag; Type: INDEX; Schema: public; Owner: -
--
CREATE UNIQUE INDEX index_relationships_on_federal_secretary_flag ON public.relationships USING btree (federal_secretary_flag);
--
-- Name: index_relationships_on_from_date; Type: INDEX; Schema: public; Owner: -
--

View file

@ -40,4 +40,9 @@ FactoryBot.define do
factory :regional_supervisor_relationship, parent: :member_relationship do
role { :regional_supervisor }
end
factory :federal_secretary_relationship,
parent: :federal_manager_relationship do
federal_secretary_flag { :federal_secretary }
end
end

View file

@ -66,6 +66,79 @@ RSpec.describe Relationship do
end
end
describe '#federal_secretary_flag' do
def allow_value(*)
super.for :federal_secretary_flag
end
context 'for supporter relationship' do
subject { create :supporter_relationship }
it { is_expected.to allow_value nil }
it { is_expected.not_to allow_value :federal_secretary }
end
context 'for excluded supporter relationship' do
subject { create :excluded_supporter_relationship }
it { is_expected.to allow_value nil }
it { is_expected.not_to allow_value :federal_secretary }
end
context 'for member relationship' do
subject { create :member_relationship }
it { is_expected.to allow_value nil }
it { is_expected.not_to allow_value :federal_secretary }
end
context 'for excluded member relationship' do
subject { create :excluded_member_relationship }
it { is_expected.to allow_value nil }
it { is_expected.not_to allow_value :federal_secretary }
end
context 'for federal manager relationship' do
subject { create :federal_manager_relationship }
it { is_expected.to allow_value nil }
it { is_expected.to allow_value :federal_secretary }
context 'when federal secretary already exists' do
subject { create :federal_secretary_relationship }
it do
is_expected.to \
validate_uniqueness_of(:federal_secretary_flag)
.allow_nil
.ignoring_case_sensitivity
end
end
end
context 'for federal supervisor relationship' do
subject { create :federal_supervisor_relationship }
it { is_expected.to allow_value nil }
it { is_expected.not_to allow_value :federal_secretary }
end
context 'for regional manager relationship' do
subject { create :regional_manager_relationship }
it { is_expected.to allow_value nil }
it { is_expected.not_to allow_value :federal_secretary }
end
context 'for regional supervisor relationship' do
subject { create :regional_supervisor_relationship }
it { is_expected.to allow_value nil }
it { is_expected.not_to allow_value :federal_secretary }
end
end
describe '.federal_managers' do
let!(:relationship1) { create :supporter_relationship }
let!(:relationship2) { create :member_relationship }
@ -99,4 +172,21 @@ RSpec.describe Relationship do
eq [relationship5, relationship8]
end
end
describe '.federal_secretaries' do
let!(:relationship1) { create :supporter_relationship }
let!(:relationship2) { create :member_relationship }
let!(:relationship3) { create :excluded_supporter_relationship }
let!(:relationship3) { create :excluded_member_relationship }
let!(:relationship4) { create :federal_manager_relationship }
let!(:relationship5) { create :federal_supervisor_relationship }
let!(:relationship6) { create :regional_manager_relationship }
let!(:relationship7) { create :regional_supervisor_relationship }
let!(:relationship8) { create :federal_secretary_relationship }
let!(:relationship9) { create :federal_manager_relationship }
specify do
expect(described_class.federal_secretaries).to eq [relationship8]
end
end
end