1
0
Fork 0

Rename model ContactsList into ContactList

This commit is contained in:
Alex Kotov 2019-07-29 10:36:39 +05:00
parent b36fb8a5e3
commit 58eefa31eb
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
11 changed files with 89 additions and 89 deletions

View file

@ -22,7 +22,7 @@ private
def create_person
context.person = Person.where(id: person_id).lock(true).first_or_create!(
person_attributes.reverse_merge(
contacts_list: ContactsList.new,
contact_list: ContactList.new,
),
)
end

View file

@ -34,7 +34,7 @@ class Account < ApplicationRecord
belongs_to :person, optional: true
belongs_to :contacts_list
belongs_to :contact_list
has_one :user
@ -45,7 +45,7 @@ class Account < ApplicationRecord
after_initialize :generate_nickname
before_validation do
self.contacts_list ||= person ? person.contacts_list : ContactsList.new
self.contact_list ||= person ? person.contact_list : ContactList.new
end
before_validation :turn_blanks_into_nils
@ -59,7 +59,7 @@ class Account < ApplicationRecord
validates :person, allow_nil: true, uniqueness: true
validates :contacts_list, uniqueness: true
validates :contact_list, uniqueness: true
validates :nickname,
presence: true,
@ -73,7 +73,7 @@ class Account < ApplicationRecord
validates :avatar, allow_nil: true, image: true
validate :contacts_list_corresponds_person
validate :contact_list_corresponds_person
###########
# Methods #
@ -134,9 +134,9 @@ private
self.biography = biography&.strip
end
def contacts_list_corresponds_person
def contact_list_corresponds_person
return if person.nil?
errors.add :contacts_list unless contacts_list == person.contacts_list
errors.add :contact_list unless contact_list == person.contact_list
end
end

View file

@ -1,6 +1,6 @@
# frozen_string_literal: true
class ContactsList < ApplicationRecord
class ContactList < ApplicationRecord
################
# Associations #
################

View file

@ -7,7 +7,7 @@ class Person < ApplicationRecord
# Associations #
################
belongs_to :contacts_list
belongs_to :contact_list
has_one :account
@ -34,7 +34,7 @@ class Person < ApplicationRecord
# Validations #
###############
validates :contacts_list, uniqueness: true
validates :contact_list, uniqueness: true
###########
# Methods #

View file

@ -95,12 +95,12 @@ private
$$;
SQL
func :ensure_contacts_list_id_remains_unchanged, <<~SQL
func :ensure_contact_list_id_remains_unchanged, <<~SQL
() RETURNS trigger LANGUAGE plpgsql AS
$$
BEGIN
IF NEW.contacts_list_id IS DISTINCT FROM OLD.contacts_list_id THEN
RAISE EXCEPTION 'can not change column "contacts_list_id"';
IF NEW.contact_list_id IS DISTINCT FROM OLD.contact_list_id THEN
RAISE EXCEPTION 'can not change column "contact_list_id"';
END IF;
RETURN NEW;
@ -108,7 +108,7 @@ private
$$;
SQL
func :ensure_contacts_list_id_matches_related_person, <<~SQL
func :ensure_contact_list_id_matches_related_person, <<~SQL
() RETURNS trigger LANGUAGE plpgsql AS
$$
DECLARE
@ -124,9 +124,9 @@ private
RETURN NEW;
END IF;
IF NEW.contacts_list_id IS DISTINCT FROM person.contacts_list_id THEN
IF NEW.contact_list_id IS DISTINCT FROM person.contact_list_id THEN
RAISE EXCEPTION
'column "contacts_list_id" does not match related person';
'column "contact_list_id" does not match related person';
END IF;
RETURN NEW;
@ -136,7 +136,7 @@ private
end
def change_tables
create_table :contacts_lists do |t|
create_table :contact_lists do |t|
t.timestamps null: false
end
@ -168,7 +168,7 @@ private
t.date :date_of_birth, null: true
t.string :place_of_birth, null: true
t.references :contacts_list,
t.references :contact_list,
null: false, index: { unique: true }, foreign_key: true
end
@ -219,7 +219,7 @@ private
t.references :person, index: { unique: true }, foreign_key: true
t.references :contacts_list,
t.references :contact_list,
null: false, index: { unique: true }, foreign_key: true
end
@ -455,18 +455,18 @@ private
reversible do |dir|
dir.down do
execute <<~SQL
DROP TRIGGER ensure_contacts_list_id_remains_unchanged
DROP TRIGGER ensure_contact_list_id_remains_unchanged
ON people;
SQL
end
dir.up do
execute <<~SQL
CREATE TRIGGER ensure_contacts_list_id_remains_unchanged
BEFORE UPDATE OF contacts_list_id
CREATE TRIGGER ensure_contact_list_id_remains_unchanged
BEFORE UPDATE OF contact_list_id
ON people
FOR EACH ROW
EXECUTE PROCEDURE ensure_contacts_list_id_remains_unchanged();
EXECUTE PROCEDURE ensure_contact_list_id_remains_unchanged();
SQL
end
end
@ -474,18 +474,18 @@ private
reversible do |dir|
dir.down do
execute <<~SQL
DROP TRIGGER ensure_contacts_list_id_matches_related_person
DROP TRIGGER ensure_contact_list_id_matches_related_person
ON accounts;
SQL
end
dir.up do
execute <<~SQL
CREATE TRIGGER ensure_contacts_list_id_matches_related_person
BEFORE INSERT OR UPDATE OF person_id, contacts_list_id
CREATE TRIGGER ensure_contact_list_id_matches_related_person
BEFORE INSERT OR UPDATE OF person_id, contact_list_id
ON accounts
FOR EACH ROW
EXECUTE PROCEDURE ensure_contacts_list_id_matches_related_person();
EXECUTE PROCEDURE ensure_contact_list_id_matches_related_person();
SQL
end
end

View file

@ -90,10 +90,10 @@ CREATE TYPE public.sex AS ENUM (
--
-- Name: ensure_contacts_list_id_matches_related_person(); Type: FUNCTION; Schema: public; Owner: -
-- Name: ensure_contact_list_id_matches_related_person(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.ensure_contacts_list_id_matches_related_person() RETURNS trigger
CREATE FUNCTION public.ensure_contact_list_id_matches_related_person() RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
@ -109,9 +109,9 @@ BEGIN
RETURN NEW;
END IF;
IF NEW.contacts_list_id IS DISTINCT FROM person.contacts_list_id THEN
IF NEW.contact_list_id IS DISTINCT FROM person.contact_list_id THEN
RAISE EXCEPTION
'column "contacts_list_id" does not match related person';
'column "contact_list_id" does not match related person';
END IF;
RETURN NEW;
@ -120,15 +120,15 @@ $$;
--
-- Name: ensure_contacts_list_id_remains_unchanged(); Type: FUNCTION; Schema: public; Owner: -
-- Name: ensure_contact_list_id_remains_unchanged(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.ensure_contacts_list_id_remains_unchanged() RETURNS trigger
CREATE FUNCTION public.ensure_contact_list_id_remains_unchanged() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
IF NEW.contacts_list_id IS DISTINCT FROM OLD.contacts_list_id THEN
RAISE EXCEPTION 'can not change column "contacts_list_id"';
IF NEW.contact_list_id IS DISTINCT FROM OLD.contact_list_id THEN
RAISE EXCEPTION 'can not change column "contact_list_id"';
END IF;
RETURN NEW;
@ -266,7 +266,7 @@ CREATE TABLE public.accounts (
public_name character varying,
biography text,
person_id bigint,
contacts_list_id bigint NOT NULL,
contact_list_id bigint NOT NULL,
CONSTRAINT biography CHECK (((biography IS NULL) OR public.is_good_big_text(biography))),
CONSTRAINT guest_token CHECK (public.is_guest_token((guest_token)::text)),
CONSTRAINT nickname CHECK (public.is_nickname((nickname)::text)),
@ -374,10 +374,10 @@ CREATE TABLE public.ar_internal_metadata (
--
-- Name: contacts_lists; Type: TABLE; Schema: public; Owner: -
-- Name: contact_lists; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.contacts_lists (
CREATE TABLE public.contact_lists (
id bigint NOT NULL,
created_at timestamp(6) without time zone NOT NULL,
updated_at timestamp(6) without time zone NOT NULL
@ -385,10 +385,10 @@ CREATE TABLE public.contacts_lists (
--
-- Name: contacts_lists_id_seq; Type: SEQUENCE; Schema: public; Owner: -
-- Name: contact_lists_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE public.contacts_lists_id_seq
CREATE SEQUENCE public.contact_lists_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
@ -397,10 +397,10 @@ CREATE SEQUENCE public.contacts_lists_id_seq
--
-- Name: contacts_lists_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
-- Name: contact_lists_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE public.contacts_lists_id_seq OWNED BY public.contacts_lists.id;
ALTER SEQUENCE public.contact_lists_id_seq OWNED BY public.contact_lists.id;
--
@ -529,7 +529,7 @@ CREATE TABLE public.people (
sex public.sex,
date_of_birth date,
place_of_birth character varying,
contacts_list_id bigint NOT NULL
contact_list_id bigint NOT NULL
);
@ -812,10 +812,10 @@ ALTER TABLE ONLY public.active_storage_blobs ALTER COLUMN id SET DEFAULT nextval
--
-- Name: contacts_lists id; Type: DEFAULT; Schema: public; Owner: -
-- Name: contact_lists id; Type: DEFAULT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.contacts_lists ALTER COLUMN id SET DEFAULT nextval('public.contacts_lists_id_seq'::regclass);
ALTER TABLE ONLY public.contact_lists ALTER COLUMN id SET DEFAULT nextval('public.contact_lists_id_seq'::regclass);
--
@ -922,11 +922,11 @@ ALTER TABLE ONLY public.ar_internal_metadata
--
-- Name: contacts_lists contacts_lists_pkey; Type: CONSTRAINT; Schema: public; Owner: -
-- Name: contact_lists contact_lists_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.contacts_lists
ADD CONSTRAINT contacts_lists_pkey PRIMARY KEY (id);
ALTER TABLE ONLY public.contact_lists
ADD CONSTRAINT contact_lists_pkey PRIMARY KEY (id);
--
@ -1024,10 +1024,10 @@ CREATE INDEX index_account_roles_on_role_id ON public.account_roles USING btree
--
-- Name: index_accounts_on_contacts_list_id; Type: INDEX; Schema: public; Owner: -
-- Name: index_accounts_on_contact_list_id; Type: INDEX; Schema: public; Owner: -
--
CREATE UNIQUE INDEX index_accounts_on_contacts_list_id ON public.accounts USING btree (contacts_list_id);
CREATE UNIQUE INDEX index_accounts_on_contact_list_id ON public.accounts USING btree (contact_list_id);
--
@ -1108,10 +1108,10 @@ CREATE INDEX index_passports_on_person_id ON public.passports USING btree (perso
--
-- Name: index_people_on_contacts_list_id; Type: INDEX; Schema: public; Owner: -
-- Name: index_people_on_contact_list_id; Type: INDEX; Schema: public; Owner: -
--
CREATE UNIQUE INDEX index_people_on_contacts_list_id ON public.people USING btree (contacts_list_id);
CREATE UNIQUE INDEX index_people_on_contact_list_id ON public.people USING btree (contact_list_id);
--
@ -1255,25 +1255,17 @@ CREATE UNIQUE INDEX index_users_on_unlock_token ON public.users USING btree (unl
--
-- Name: accounts ensure_contacts_list_id_matches_related_person; Type: TRIGGER; Schema: public; Owner: -
-- Name: accounts ensure_contact_list_id_matches_related_person; Type: TRIGGER; Schema: public; Owner: -
--
CREATE TRIGGER ensure_contacts_list_id_matches_related_person BEFORE INSERT OR UPDATE OF person_id, contacts_list_id ON public.accounts FOR EACH ROW EXECUTE PROCEDURE public.ensure_contacts_list_id_matches_related_person();
CREATE TRIGGER ensure_contact_list_id_matches_related_person BEFORE INSERT OR UPDATE OF person_id, contact_list_id ON public.accounts FOR EACH ROW EXECUTE PROCEDURE public.ensure_contact_list_id_matches_related_person();
--
-- Name: people ensure_contacts_list_id_remains_unchanged; Type: TRIGGER; Schema: public; Owner: -
-- Name: people ensure_contact_list_id_remains_unchanged; Type: TRIGGER; Schema: public; Owner: -
--
CREATE TRIGGER ensure_contacts_list_id_remains_unchanged BEFORE UPDATE OF contacts_list_id ON public.people FOR EACH ROW EXECUTE PROCEDURE public.ensure_contacts_list_id_remains_unchanged();
--
-- Name: accounts fk_rails_0fa1840045; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.accounts
ADD CONSTRAINT fk_rails_0fa1840045 FOREIGN KEY (contacts_list_id) REFERENCES public.contacts_lists(id);
CREATE TRIGGER ensure_contact_list_id_remains_unchanged BEFORE UPDATE OF contact_list_id ON public.people FOR EACH ROW EXECUTE PROCEDURE public.ensure_contact_list_id_remains_unchanged();
--
@ -1284,6 +1276,14 @@ ALTER TABLE ONLY public.relationships
ADD CONSTRAINT fk_rails_100235139c FOREIGN KEY (regional_office_id) REFERENCES public.regional_offices(id);
--
-- Name: people fk_rails_4f02f930eb; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.people
ADD CONSTRAINT fk_rails_4f02f930eb FOREIGN KEY (contact_list_id) REFERENCES public.contact_lists(id);
--
-- Name: passports fk_rails_5cdfa39dea; Type: FK CONSTRAINT; Schema: public; Owner: -
--
@ -1308,6 +1308,14 @@ ALTER TABLE ONLY public.accounts
ADD CONSTRAINT fk_rails_777d10a224 FOREIGN KEY (person_id) REFERENCES public.people(id);
--
-- Name: accounts fk_rails_77a360a20e; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.accounts
ADD CONSTRAINT fk_rails_77a360a20e FOREIGN KEY (contact_list_id) REFERENCES public.contact_lists(id);
--
-- Name: regional_offices fk_rails_7a6d5fdd9a; Type: FK CONSTRAINT; Schema: public; Owner: -
--
@ -1340,14 +1348,6 @@ ALTER TABLE ONLY public.person_comments
ADD CONSTRAINT fk_rails_a9c7b4ae11 FOREIGN KEY (account_id) REFERENCES public.accounts(id);
--
-- Name: people fk_rails_cb9b5a21ec; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.people
ADD CONSTRAINT fk_rails_cb9b5a21ec FOREIGN KEY (contacts_list_id) REFERENCES public.contacts_lists(id);
--
-- Name: passports fk_rails_cd632a506c; Type: FK CONSTRAINT; Schema: public; Owner: -
--

View file

@ -1,5 +1,5 @@
# frozen_string_literal: true
FactoryBot.define do
factory :empty_contacts_list, class: ContactsList
factory :empty_contact_list, class: ContactList
end

View file

@ -6,7 +6,7 @@ FactoryBot.define do
regional_office { create :regional_office }
end
association :contacts_list, factory: :empty_contacts_list
association :contact_list, factory: :empty_contact_list
first_name { Faker::Name.first_name }
middle_name { Faker::Name.first_name }

View file

@ -48,40 +48,40 @@ RSpec.describe Account do
it { is_expected.not_to validate_presence_of :person }
end
describe '#contacts_list' do
describe '#contact_list' do
def allow_value(*)
super.for :contacts_list
super.for :contact_list
end
xit { is_expected.to belong_to(:contacts_list).required }
xit { is_expected.to belong_to(:contact_list).required }
context 'for usual account' do
subject { create :usual_account }
it { is_expected.to allow_value create :empty_contacts_list }
it { is_expected.to allow_value create :empty_contact_list }
end
context 'for personal account' do
subject { create :personal_account }
it { is_expected.not_to allow_value create :empty_contacts_list }
it { is_expected.not_to allow_value create :empty_contact_list }
it { is_expected.to allow_value subject.person.contacts_list }
it { is_expected.to allow_value subject.person.contact_list }
specify do
expect(subject.contacts_list).to eq subject.person.contacts_list
expect(subject.contact_list).to eq subject.person.contact_list
end
context 'when it was changed' do
before do
subject.contacts_list = ContactsList.new
subject.contact_list = ContactList.new
end
specify do
expect { subject.save validate: false }.to raise_error(
ActiveRecord::StatementInvalid,
/\APG::RaiseException:\sERROR:\s\s
column\s"contacts_list_id"\sdoes\snot\smatch\srelated\sperson$/x,
column\s"contact_list_id"\sdoes\snot\smatch\srelated\sperson$/x,
)
end
end

View file

@ -2,8 +2,8 @@
require 'rails_helper'
RSpec.describe ContactsList do
subject { create :empty_contacts_list }
RSpec.describe ContactList do
subject { create :empty_contact_list }
describe '#account' do
it do

View file

@ -11,19 +11,19 @@ RSpec.describe Person do
it { is_expected.to have_one(:account).dependent(:restrict_with_exception) }
end
describe '#contacts_list' do
xit { is_expected.to belong_to(:contacts_list).required }
describe '#contact_list' do
xit { is_expected.to belong_to(:contact_list).required }
context 'when it was changed' do
before do
subject.contacts_list = ContactsList.new
subject.contact_list = ContactList.new
end
specify do
expect { subject.save }.to raise_error(
ActiveRecord::StatementInvalid,
/\APG::RaiseException:\s
ERROR:\s\scan\snot\schange\scolumn\s"contacts_list_id"$/x,
ERROR:\s\scan\snot\schange\scolumn\s"contact_list_id"$/x,
)
end
end