From 0c49c4033035feeca6737a072f1c34bdc03032be Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Mon, 21 Oct 2019 11:09:51 +0500 Subject: [PATCH] Add column OrgUnitKind#resource_type --- app/models/org_unit_kind.rb | 2 ++ db/data/org_unit_kinds.csv | 12 +++++----- ...000_add_resource_type_to_org_unit_kinds.rb | 22 +++++++++++++++++++ db/seeds.rb | 4 +++- db/structure.sql | 18 ++++++++++++++- factories/org_unit_kinds.rb | 1 + spec/models/org_unit_kind_spec.rb | 13 +++++++++++ 7 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 db/migrate/20191021060000_add_resource_type_to_org_unit_kinds.rb diff --git a/app/models/org_unit_kind.rb b/app/models/org_unit_kind.rb index a18b5bd..19900ab 100644 --- a/app/models/org_unit_kind.rb +++ b/app/models/org_unit_kind.rb @@ -33,6 +33,8 @@ class OrgUnitKind < ApplicationRecord validates :name, good_small_text: true, uniqueness: true + validates :resource_type, allow_nil: true, presence: true + ############# # Callbacks # ############# diff --git a/db/data/org_unit_kinds.csv b/db/data/org_unit_kinds.csv index 9dad13c..44d8ee1 100644 --- a/db/data/org_unit_kinds.csv +++ b/db/data/org_unit_kinds.csv @@ -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 | РКРК | Региональная контрольно-ревизионная комиссия | | diff --git a/db/migrate/20191021060000_add_resource_type_to_org_unit_kinds.rb b/db/migrate/20191021060000_add_resource_type_to_org_unit_kinds.rb new file mode 100644 index 0000000..eb1b053 --- /dev/null +++ b/db/migrate/20191021060000_add_resource_type_to_org_unit_kinds.rb @@ -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 diff --git a/db/seeds.rb b/db/seeds.rb index a584c14..7985927 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -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 diff --git a/db/structure.sql b/db/structure.sql index a92558d..5134696 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -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'); diff --git a/factories/org_unit_kinds.rb b/factories/org_unit_kinds.rb index eb851f3..fd5dcee 100644 --- a/factories/org_unit_kinds.rb +++ b/factories/org_unit_kinds.rb @@ -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 diff --git a/spec/models/org_unit_kind_spec.rb b/spec/models/org_unit_kind_spec.rb index 32f6ebe..dda3950 100644 --- a/spec/models/org_unit_kind_spec.rb +++ b/spec/models/org_unit_kind_spec.rb @@ -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