Add model OrgUnit
This commit is contained in:
parent
9d940fcc48
commit
5cfd1147db
|
@ -0,0 +1,33 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class OrgUnit < ApplicationRecord
|
||||
################
|
||||
# Associations #
|
||||
################
|
||||
|
||||
belongs_to :kind,
|
||||
class_name: 'OrgUnitKind',
|
||||
inverse_of: :instances
|
||||
|
||||
belongs_to :parent,
|
||||
class_name: 'OrgUnit',
|
||||
inverse_of: :children,
|
||||
optional: true
|
||||
|
||||
has_many :children,
|
||||
class_name: 'OrgUnit',
|
||||
inverse_of: :parent,
|
||||
foreign_key: :parent_id
|
||||
|
||||
###############
|
||||
# Validations #
|
||||
###############
|
||||
|
||||
validates :name, good_small_text: true, uniqueness: true
|
||||
|
||||
validates :parent,
|
||||
presence: {
|
||||
if: ->(record) { record.kind&.parent_kind },
|
||||
message: :required,
|
||||
}
|
||||
end
|
|
@ -15,6 +15,11 @@ class OrgUnitKind < ApplicationRecord
|
|||
inverse_of: :parent_kind,
|
||||
foreign_key: :parent_kind_id
|
||||
|
||||
has_many :instances,
|
||||
class_name: 'OrgUnit',
|
||||
inverse_of: :kind,
|
||||
foreign_key: :kind_id
|
||||
|
||||
###############
|
||||
# Validations #
|
||||
###############
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class CreateOrgUnits < ActiveRecord::Migration[6.0]
|
||||
include Partynest::Migration
|
||||
|
||||
def change
|
||||
create_table :org_units do |t|
|
||||
t.timestamps null: false
|
||||
|
||||
t.string :name, null: false, index: { unique: true }
|
||||
|
||||
t.references :kind,
|
||||
null: false,
|
||||
index: true,
|
||||
foreign_key: { to_table: :org_unit_kinds }
|
||||
|
||||
t.references :parent,
|
||||
null: true,
|
||||
index: true,
|
||||
foreign_key: { to_table: :org_units }
|
||||
end
|
||||
|
||||
add_constraint :org_units, :name, <<~SQL
|
||||
is_good_small_text(name)
|
||||
SQL
|
||||
end
|
||||
end
|
|
@ -498,6 +498,40 @@ CREATE SEQUENCE public.org_unit_kinds_id_seq
|
|||
ALTER SEQUENCE public.org_unit_kinds_id_seq OWNED BY public.org_unit_kinds.id;
|
||||
|
||||
|
||||
--
|
||||
-- Name: org_units; Type: TABLE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE TABLE public.org_units (
|
||||
id bigint NOT NULL,
|
||||
created_at timestamp(6) without time zone NOT NULL,
|
||||
updated_at timestamp(6) without time zone NOT NULL,
|
||||
name character varying NOT NULL,
|
||||
kind_id bigint NOT NULL,
|
||||
parent_id bigint,
|
||||
CONSTRAINT name CHECK (public.is_good_small_text((name)::text))
|
||||
);
|
||||
|
||||
|
||||
--
|
||||
-- Name: org_units_id_seq; Type: SEQUENCE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE SEQUENCE public.org_units_id_seq
|
||||
START WITH 1
|
||||
INCREMENT BY 1
|
||||
NO MINVALUE
|
||||
NO MAXVALUE
|
||||
CACHE 1;
|
||||
|
||||
|
||||
--
|
||||
-- Name: org_units_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER SEQUENCE public.org_units_id_seq OWNED BY public.org_units.id;
|
||||
|
||||
|
||||
--
|
||||
-- Name: passports; Type: TABLE; Schema: public; Owner: -
|
||||
--
|
||||
|
@ -928,6 +962,13 @@ ALTER TABLE ONLY public.federal_subjects ALTER COLUMN id SET DEFAULT nextval('pu
|
|||
ALTER TABLE ONLY public.org_unit_kinds ALTER COLUMN id SET DEFAULT nextval('public.org_unit_kinds_id_seq'::regclass);
|
||||
|
||||
|
||||
--
|
||||
-- Name: org_units id; Type: DEFAULT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY public.org_units ALTER COLUMN id SET DEFAULT nextval('public.org_units_id_seq'::regclass);
|
||||
|
||||
|
||||
--
|
||||
-- Name: passports id; Type: DEFAULT; Schema: public; Owner: -
|
||||
--
|
||||
|
@ -1063,6 +1104,14 @@ ALTER TABLE ONLY public.org_unit_kinds
|
|||
ADD CONSTRAINT org_unit_kinds_pkey PRIMARY KEY (id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: org_units org_units_pkey; Type: CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY public.org_units
|
||||
ADD CONSTRAINT org_units_pkey PRIMARY KEY (id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: passports passports_pkey; Type: CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
@ -1276,6 +1325,27 @@ CREATE INDEX index_org_unit_kinds_on_parent_kind_id ON public.org_unit_kinds USI
|
|||
CREATE UNIQUE INDEX index_org_unit_kinds_on_short_name ON public.org_unit_kinds USING btree (short_name);
|
||||
|
||||
|
||||
--
|
||||
-- Name: index_org_units_on_kind_id; Type: INDEX; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE INDEX index_org_units_on_kind_id ON public.org_units USING btree (kind_id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: index_org_units_on_name; Type: INDEX; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE UNIQUE INDEX index_org_units_on_name ON public.org_units USING btree (name);
|
||||
|
||||
|
||||
--
|
||||
-- Name: index_org_units_on_parent_id; Type: INDEX; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE INDEX index_org_units_on_parent_id ON public.org_units USING btree (parent_id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: index_passports_on_federal_subject_id; Type: INDEX; Schema: public; Owner: -
|
||||
--
|
||||
|
@ -1459,6 +1529,14 @@ ALTER TABLE ONLY public.people
|
|||
ADD CONSTRAINT fk_rails_4f02f930eb FOREIGN KEY (contact_list_id) REFERENCES public.contact_lists(id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: org_units fk_rails_54c0512b74; Type: FK CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY public.org_units
|
||||
ADD CONSTRAINT fk_rails_54c0512b74 FOREIGN KEY (parent_id) REFERENCES public.org_units(id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: sessions fk_rails_5599381559; Type: FK CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
@ -1547,6 +1625,14 @@ ALTER TABLE ONLY public.relation_transitions
|
|||
ADD CONSTRAINT fk_rails_b61956945e FOREIGN KEY (from_status_id) REFERENCES public.relation_statuses(id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: org_units fk_rails_ccc56f184e; Type: FK CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY public.org_units
|
||||
ADD CONSTRAINT fk_rails_ccc56f184e FOREIGN KEY (kind_id) REFERENCES public.org_unit_kinds(id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: passports fk_rails_cd632a506c; Type: FK CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
@ -1604,6 +1690,7 @@ INSERT INTO "schema_migrations" (version) VALUES
|
|||
('20190929131544'),
|
||||
('20190930154031'),
|
||||
('20190930205337'),
|
||||
('20190930210852');
|
||||
('20190930210852'),
|
||||
('20190930215223');
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
FactoryBot.define do
|
||||
factory :some_root_org_unit, class: OrgUnit do
|
||||
name { Faker::Company.unique.name }
|
||||
|
||||
association :kind, factory: :some_root_org_unit_kind
|
||||
|
||||
trait :with_parent do
|
||||
association :kind, factory: :some_children_org_unit_kind
|
||||
|
||||
parent { create :some_root_org_unit, kind: kind.parent_kind }
|
||||
end
|
||||
end
|
||||
|
||||
factory :some_children_org_unit,
|
||||
parent: :some_root_org_unit,
|
||||
traits: %i[with_parent]
|
||||
end
|
|
@ -34,6 +34,17 @@ RSpec.describe OrgUnitKind do
|
|||
end
|
||||
end
|
||||
|
||||
describe '#instance' do
|
||||
it do
|
||||
is_expected.to \
|
||||
have_many(:instances)
|
||||
.class_name('OrgUnit')
|
||||
.inverse_of(:kind)
|
||||
.with_foreign_key(:kind_id)
|
||||
.dependent(:restrict_with_exception)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#codename' do
|
||||
def allow_value(*)
|
||||
super.for :codename
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe OrgUnit do
|
||||
subject { create :some_children_org_unit }
|
||||
|
||||
describe '#kind' do
|
||||
it do
|
||||
is_expected.to \
|
||||
belong_to(:kind)
|
||||
.class_name('OrgUnitKind')
|
||||
.inverse_of(:instances)
|
||||
.required
|
||||
end
|
||||
|
||||
it { is_expected.to validate_presence_of(:kind).with_message(:required) }
|
||||
end
|
||||
|
||||
describe '#parent' do
|
||||
it do
|
||||
is_expected.to \
|
||||
belong_to(:parent)
|
||||
.class_name('OrgUnit')
|
||||
.inverse_of(:children)
|
||||
end
|
||||
|
||||
context 'when organizational unit type does not require parent' do
|
||||
subject { create :some_root_org_unit }
|
||||
|
||||
it do
|
||||
is_expected.not_to validate_presence_of(:parent).with_message(:required)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when organizational unit type does not require parent' do
|
||||
subject { create :some_children_org_unit }
|
||||
|
||||
it do
|
||||
is_expected.to validate_presence_of(:parent).with_message(:required)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue