1
0
Fork 0

Add column Relationship#role

This commit is contained in:
Alex Kotov 2019-07-20 14:12:58 +05:00
parent c0a5e2a4ec
commit ce51091dbd
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
5 changed files with 64 additions and 11 deletions

View file

@ -1,7 +1,9 @@
# frozen_string_literal: true
class Relationship < ApplicationRecord
enum status: %i[supporter member excluded]
enum status: %i[supporter excluded member]
enum role: %i[manager supervisor]
################
# Associations #
@ -17,4 +19,6 @@ class Relationship < ApplicationRecord
validates :from_date, presence: true, uniqueness: { scope: :person_id }
validates :status, presence: true
validates :role, absence: { unless: :member? }
end

View file

@ -11,10 +11,10 @@ class CreateRelationships < ActiveRecord::Migration[6.0]
t.references :regional_office,
null: false, index: true, foreign_key: true
t.date :from_date, null: false, index: true
t.integer :status, null: false, index: true
t.date :until_date
t.date :from_date, null: false, index: true
t.date :until_date, null: true, index: false
t.integer :status, null: false, index: true
t.integer :role, null: true, index: true
t.index %i[person_id from_date], unique: true
end
@ -25,6 +25,10 @@ class CreateRelationships < ActiveRecord::Migration[6.0]
ALTER TABLE relationships ADD CONSTRAINT dates CHECK (
until_date IS NULL OR from_date < until_date
);
ALTER TABLE relationships ADD CONSTRAINT role CHECK (
status = 2 /* member */ OR role IS NULL
);
SQL
end
end

View file

@ -400,9 +400,11 @@ CREATE TABLE public.relationships (
person_id bigint NOT NULL,
regional_office_id bigint NOT NULL,
from_date date NOT NULL,
status integer NOT NULL,
until_date date,
CONSTRAINT dates CHECK (((until_date IS NULL) OR (from_date < until_date)))
status integer NOT NULL,
role integer,
CONSTRAINT dates CHECK (((until_date IS NULL) OR (from_date < until_date))),
CONSTRAINT role CHECK (((status = 2) OR (role IS NULL)))
);
@ -915,6 +917,13 @@ CREATE UNIQUE INDEX index_relationships_on_person_id_and_from_date ON public.rel
CREATE INDEX index_relationships_on_regional_office_id ON public.relationships USING btree (regional_office_id);
--
-- Name: index_relationships_on_role; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX index_relationships_on_role ON public.relationships USING btree (role);
--
-- Name: index_relationships_on_status; Type: INDEX; Schema: public; Owner: -
--

View file

@ -10,10 +10,7 @@ FactoryBot.define do
end
status { :supporter }
end
factory :member_relationship, parent: :supporter_relationship do
status { :member }
role { nil }
end
factory :excluded_supporter_relationship, parent: :supporter_relationship do
@ -23,4 +20,16 @@ FactoryBot.define do
factory :excluded_member_relationship, parent: :member_relationship do
status { :excluded }
end
factory :member_relationship, parent: :supporter_relationship do
status { :member }
end
factory :manager_relationship, parent: :member_relationship do
role { :manager }
end
factory :supervisor_relationship, parent: :member_relationship do
role { :supervisor }
end
end

View file

@ -21,4 +21,31 @@ RSpec.describe Relationship do
describe '#status' do
it { is_expected.to validate_presence_of :status }
end
describe '#role' do
context 'for supporter relationship' do
subject { create :supporter_relationship }
it { is_expected.to validate_absence_of :role }
end
context 'for excluded supporter relationship' do
subject { create :excluded_supporter_relationship }
it { is_expected.to validate_absence_of :role }
end
context 'for excluded member relationship' do
subject { create :excluded_member_relationship }
it { is_expected.to validate_absence_of :role }
end
context 'for member relationship' do
subject { create :member_relationship }
it { is_expected.not_to validate_absence_of :role }
it { is_expected.not_to validate_presence_of :role }
end
end
end