diff --git a/app/models/relationship.rb b/app/models/relationship.rb index 08a2d69..1412172 100644 --- a/app/models/relationship.rb +++ b/app/models/relationship.rb @@ -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 diff --git a/db/migrate/20190427141639_create_relationships.rb b/db/migrate/20190427141639_create_relationships.rb index c9dac9a..96b3ffa 100644 --- a/db/migrate/20190427141639_create_relationships.rb +++ b/db/migrate/20190427141639_create_relationships.rb @@ -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 diff --git a/db/structure.sql b/db/structure.sql index 1a3d285..96c1ba9 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -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: - -- diff --git a/factories/relationships.rb b/factories/relationships.rb index 03d6276..a73eab7 100644 --- a/factories/relationships.rb +++ b/factories/relationships.rb @@ -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 diff --git a/spec/models/relationship_spec.rb b/spec/models/relationship_spec.rb index 2d7b2d1..0e3efb6 100644 --- a/spec/models/relationship_spec.rb +++ b/spec/models/relationship_spec.rb @@ -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