Add column OrgUnitKind#resource_type
This commit is contained in:
parent
362b4e0ebb
commit
0c49c40330
7 changed files with 64 additions and 8 deletions
|
@ -33,6 +33,8 @@ class OrgUnitKind < ApplicationRecord
|
||||||
|
|
||||||
validates :name, good_small_text: true, uniqueness: true
|
validates :name, good_small_text: true, uniqueness: true
|
||||||
|
|
||||||
|
validates :resource_type, allow_nil: true, presence: true
|
||||||
|
|
||||||
#############
|
#############
|
||||||
# Callbacks #
|
# Callbacks #
|
||||||
#############
|
#############
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
lpr | | ЛПР | Либертарианская партия России |
|
lpr | | ЛПР | Либертарианская партия России | |
|
||||||
reg_dept | lpr | РО | Региональное отделение |
|
reg_dept | lpr | РО | Региональное отделение | FederalSubject |
|
||||||
fed_management | lpr | ФК | Федеральный комитет |
|
fed_management | lpr | ФК | Федеральный комитет | |
|
||||||
fed_supervision | lpr | ЦКРК | Центральная контрольно-ревизионная комиссия |
|
fed_supervision | lpr | ЦКРК | Центральная контрольно-ревизионная комиссия | |
|
||||||
reg_management | reg_dept | РК РО | Руководящий комитет регионального отделения |
|
reg_management | reg_dept | РК РО | Руководящий комитет регионального отделения | |
|
||||||
reg_supervision | reg_dept | РКРК | Региональная контрольно-ревизионная комиссия |
|
reg_supervision | reg_dept | РКРК | Региональная контрольно-ревизионная комиссия | |
|
||||||
|
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class AddResourceTypeToOrgUnitKinds < ActiveRecord::Migration[6.0]
|
||||||
|
include Partynest::Migration
|
||||||
|
|
||||||
|
def change
|
||||||
|
add_func :is_class_name, <<~SQL
|
||||||
|
(str text) RETURNS boolean IMMUTABLE LANGUAGE plpgsql AS
|
||||||
|
$$
|
||||||
|
BEGIN
|
||||||
|
RETURN str ~ '^[A-Z][a-zA-Z0-9]*(::[A-Z][a-zA-Z0-9])*$';
|
||||||
|
END;
|
||||||
|
$$;
|
||||||
|
SQL
|
||||||
|
|
||||||
|
add_column :org_unit_kinds, :resource_type, :string, null: true
|
||||||
|
|
||||||
|
add_constraint :org_unit_kinds, :resource_type, <<~SQL
|
||||||
|
resource_type IS NULL OR is_class_name(resource_type)
|
||||||
|
SQL
|
||||||
|
end
|
||||||
|
end
|
|
@ -41,12 +41,13 @@ do |(id, codename, name)|
|
||||||
end
|
end
|
||||||
|
|
||||||
csv_foreach :org_unit_kinds \
|
csv_foreach :org_unit_kinds \
|
||||||
do |(codename, parent, short_name, name)|
|
do |(codename, parent, short_name, name, resource_type)|
|
||||||
parent = parent.blank? ? nil : OrgUnitKind.find_by!(codename: parent.strip)
|
parent = parent.blank? ? nil : OrgUnitKind.find_by!(codename: parent.strip)
|
||||||
|
|
||||||
codename.strip!
|
codename.strip!
|
||||||
short_name.strip!
|
short_name.strip!
|
||||||
name.strip!
|
name.strip!
|
||||||
|
resource_type = resource_type.strip.presence
|
||||||
|
|
||||||
next if OrgUnitKind.find_by codename: codename
|
next if OrgUnitKind.find_by codename: codename
|
||||||
|
|
||||||
|
@ -55,6 +56,7 @@ do |(codename, parent, short_name, name)|
|
||||||
short_name: short_name,
|
short_name: short_name,
|
||||||
name: name,
|
name: name,
|
||||||
parent_kind: parent,
|
parent_kind: parent,
|
||||||
|
resource_type: resource_type,
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -120,6 +120,19 @@ END;
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
|
|
||||||
|
--
|
||||||
|
-- Name: is_class_name(text); Type: FUNCTION; Schema: public; Owner: -
|
||||||
|
--
|
||||||
|
|
||||||
|
CREATE FUNCTION public.is_class_name(str text) RETURNS boolean
|
||||||
|
LANGUAGE plpgsql IMMUTABLE
|
||||||
|
AS $_$
|
||||||
|
BEGIN
|
||||||
|
RETURN str ~ '^[A-Z][a-zA-Z0-9]*(::[A-Z][a-zA-Z0-9])*$';
|
||||||
|
END;
|
||||||
|
$_$;
|
||||||
|
|
||||||
|
|
||||||
--
|
--
|
||||||
-- Name: is_codename(text); Type: FUNCTION; Schema: public; Owner: -
|
-- Name: is_codename(text); Type: FUNCTION; Schema: public; Owner: -
|
||||||
--
|
--
|
||||||
|
@ -677,10 +690,12 @@ CREATE TABLE public.org_unit_kinds (
|
||||||
name character varying NOT NULL,
|
name character varying NOT NULL,
|
||||||
parent_kind_id bigint,
|
parent_kind_id bigint,
|
||||||
level integer NOT NULL,
|
level integer NOT NULL,
|
||||||
|
resource_type character varying,
|
||||||
CONSTRAINT codename CHECK (public.is_codename((codename)::text)),
|
CONSTRAINT codename CHECK (public.is_codename((codename)::text)),
|
||||||
CONSTRAINT level CHECK ((level >= 0)),
|
CONSTRAINT level CHECK ((level >= 0)),
|
||||||
CONSTRAINT name CHECK (public.is_good_small_text((name)::text)),
|
CONSTRAINT name CHECK (public.is_good_small_text((name)::text)),
|
||||||
CONSTRAINT parent_kind CHECK ((parent_kind_id <> id)),
|
CONSTRAINT parent_kind CHECK ((parent_kind_id <> id)),
|
||||||
|
CONSTRAINT resource_type CHECK (((resource_type IS NULL) OR public.is_class_name((resource_type)::text))),
|
||||||
CONSTRAINT short_name CHECK (public.is_good_small_text((short_name)::text))
|
CONSTRAINT short_name CHECK (public.is_good_small_text((short_name)::text))
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -1889,6 +1904,7 @@ INSERT INTO "schema_migrations" (version) VALUES
|
||||||
('20190930215223'),
|
('20190930215223'),
|
||||||
('20191001022049'),
|
('20191001022049'),
|
||||||
('20191002002101'),
|
('20191002002101'),
|
||||||
('20191002170727');
|
('20191002170727'),
|
||||||
|
('20191021060000');
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -29,6 +29,7 @@ FactoryBot.define do
|
||||||
codename { :reg_dept }
|
codename { :reg_dept }
|
||||||
short_name { 'РО' }
|
short_name { 'РО' }
|
||||||
name { 'Региональное отделение' }
|
name { 'Региональное отделение' }
|
||||||
|
resource_type { 'FederalSubject' }
|
||||||
|
|
||||||
association :parent_kind, factory: :lpr_org_unit_kind
|
association :parent_kind, factory: :lpr_org_unit_kind
|
||||||
end
|
end
|
||||||
|
|
|
@ -146,4 +146,17 @@ RSpec.describe OrgUnitKind do
|
||||||
it { is_expected.not_to allow_value "\nFoo" }
|
it { is_expected.not_to allow_value "\nFoo" }
|
||||||
it { is_expected.not_to allow_value "Foo\n" }
|
it { is_expected.not_to allow_value "Foo\n" }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#resource_type' do
|
||||||
|
def allow_value(*)
|
||||||
|
super.for :resource_type
|
||||||
|
end
|
||||||
|
|
||||||
|
it { is_expected.to allow_value nil }
|
||||||
|
|
||||||
|
it { is_expected.not_to allow_value '' }
|
||||||
|
it { is_expected.not_to allow_value ' ' * rand(1..3) }
|
||||||
|
|
||||||
|
it { is_expected.to allow_value 'FederalSubject' }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Reference in a new issue