1
0
Fork 0

Add column OrgUnitKind#resource_type

This commit is contained in:
Alex Kotov 2019-10-21 11:09:51 +05:00
parent 362b4e0ebb
commit 0c49c40330
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
7 changed files with 64 additions and 8 deletions

View File

@ -33,6 +33,8 @@ class OrgUnitKind < ApplicationRecord
validates :name, good_small_text: true, uniqueness: true
validates :resource_type, allow_nil: true, presence: true
#############
# Callbacks #
#############

View File

@ -1,6 +1,6 @@
lpr | | ЛПР | Либертарианская партия России |
reg_dept | lpr | РО | Региональное отделение |
fed_management | lpr | ФК | Федеральный комитет |
fed_supervision | lpr | ЦКРК | Центральная контрольно-ревизионная комиссия |
reg_management | reg_dept | РК РО | Руководящий комитет регионального отделения |
reg_supervision | reg_dept | РКРК | Региональная контрольно-ревизионная комиссия |
lpr | | ЛПР | Либертарианская партия России | |
reg_dept | lpr | РО | Региональное отделение | FederalSubject |
fed_management | lpr | ФК | Федеральный комитет | |
fed_supervision | lpr | ЦКРК | Центральная контрольно-ревизионная комиссия | |
reg_management | reg_dept | РК РО | Руководящий комитет регионального отделения | |
reg_supervision | reg_dept | РКРК | Региональная контрольно-ревизионная комиссия | |

1 lpr ЛПР Либертарианская партия России
2 reg_dept lpr РО Региональное отделение FederalSubject
3 fed_management lpr ФК Федеральный комитет
4 fed_supervision lpr ЦКРК Центральная контрольно-ревизионная комиссия
5 reg_management reg_dept РК РО Руководящий комитет регионального отделения
6 reg_supervision reg_dept РКРК Региональная контрольно-ревизионная комиссия

View File

@ -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

View File

@ -41,12 +41,13 @@ do |(id, codename, name)|
end
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)
codename.strip!
short_name.strip!
name.strip!
resource_type = resource_type.strip.presence
next if OrgUnitKind.find_by codename: codename
@ -55,6 +56,7 @@ do |(codename, parent, short_name, name)|
short_name: short_name,
name: name,
parent_kind: parent,
resource_type: resource_type,
)
end

View File

@ -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: -
--
@ -677,10 +690,12 @@ CREATE TABLE public.org_unit_kinds (
name character varying NOT NULL,
parent_kind_id bigint,
level integer NOT NULL,
resource_type character varying,
CONSTRAINT codename CHECK (public.is_codename((codename)::text)),
CONSTRAINT level CHECK ((level >= 0)),
CONSTRAINT name CHECK (public.is_good_small_text((name)::text)),
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))
);
@ -1889,6 +1904,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20190930215223'),
('20191001022049'),
('20191002002101'),
('20191002170727');
('20191002170727'),
('20191021060000');

View File

@ -29,6 +29,7 @@ FactoryBot.define do
codename { :reg_dept }
short_name { 'РО' }
name { 'Региональное отделение' }
resource_type { 'FederalSubject' }
association :parent_kind, factory: :lpr_org_unit_kind
end

View File

@ -146,4 +146,17 @@ RSpec.describe OrgUnitKind do
it { is_expected.not_to allow_value "\nFoo" }
it { is_expected.not_to allow_value "Foo\n" }
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