1
0
Fork 0

Validate uniqueness of Contact#value

This commit is contained in:
Alex Kotov 2019-09-04 22:01:14 +05:00
parent 5fc557f741
commit 2d6a930bb8
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
4 changed files with 20 additions and 1 deletions

View file

@ -23,7 +23,9 @@ class Contact < ApplicationRecord
validates :contact_network, presence: true
validates :value, presence: true
validates :value,
presence: true,
uniqueness: { scope: %i[contact_list_id contact_network_id] }
private

View file

@ -178,6 +178,10 @@ private
t.references :contact_network, null: false, index: true, foreign_key: true
t.string :value, null: false
t.index %i[contact_list_id contact_network_id value],
name: :index_contacts_on_list_id_and_network_id_and_value,
unique: true
end
create_table :federal_subjects do |t|

View file

@ -1156,6 +1156,13 @@ CREATE INDEX index_contacts_on_contact_list_id ON public.contacts USING btree (c
CREATE INDEX index_contacts_on_contact_network_id ON public.contacts USING btree (contact_network_id);
--
-- Name: index_contacts_on_list_id_and_network_id_and_value; Type: INDEX; Schema: public; Owner: -
--
CREATE UNIQUE INDEX index_contacts_on_list_id_and_network_id_and_value ON public.contacts USING btree (contact_list_id, contact_network_id, value);
--
-- Name: index_federal_subjects_on_english_name; Type: INDEX; Schema: public; Owner: -
--

View file

@ -24,6 +24,12 @@ RSpec.describe Contact do
it { is_expected.to validate_presence_of :value }
it do
is_expected.to \
validate_uniqueness_of(:value)
.scoped_to(:contact_list_id, :contact_network_id)
end
it { is_expected.not_to allow_value nil }
it { is_expected.not_to allow_value '' }
it { is_expected.not_to allow_value ' ' * rand(1..3) }