1
0
Fork 0
This repository has been archived on 2023-03-27. You can view files and clone it, but cannot push or open issues or pull requests.
lpr-partynest/db/seeds.rb

158 lines
4.7 KiB
Ruby
Raw Normal View History

2018-12-01 13:42:36 -05:00
# frozen_string_literal: true
require 'csv'
2018-12-01 13:42:36 -05:00
2019-09-30 19:24:18 -04:00
def csv_foreach(filename, &block)
CSV.foreach(
2020-01-16 11:14:52 -05:00
Rails.root.join("db/data/#{filename}.csv"),
2019-09-30 19:24:18 -04:00
col_sep: '|',
&block
)
end
csv_foreach :federal_subjects \
do |(id, english_name, native_name, number, timezone, centre)|
2019-07-19 17:59:07 -04:00
id = Integer(id.strip)
english_name.strip!
2019-07-19 17:59:07 -04:00
native_name.strip!
2019-07-19 18:52:14 -04:00
number = Integer(number.strip.sub(/\A0*/, ''))
2019-07-19 22:40:56 -04:00
timezone.strip!
2019-07-22 05:14:14 -04:00
centre.strip!
2018-12-01 13:42:36 -05:00
2019-07-19 17:59:07 -04:00
FederalSubject.where(id: id).first_or_create! do |new_federal_subject|
new_federal_subject.english_name = english_name
2019-06-23 11:59:44 -04:00
new_federal_subject.native_name = native_name
2019-07-19 18:52:14 -04:00
new_federal_subject.number = number
2019-07-19 22:40:56 -04:00
new_federal_subject.timezone = timezone
2019-07-22 05:14:14 -04:00
new_federal_subject.centre = centre
2019-04-27 08:40:12 -04:00
end
2018-12-01 13:42:36 -05:00
end
2018-12-03 20:57:16 -05:00
2019-09-30 19:24:18 -04:00
csv_foreach :contact_networks \
do |(id, codename, name)|
2019-08-04 17:11:44 -04:00
id = Integer(id.strip)
codename.strip!
name.strip!
2019-08-04 17:11:44 -04:00
ContactNetwork.where(id: id).first_or_create! do |new_contact_network|
new_contact_network.codename = codename
new_contact_network.name = name
2019-08-04 17:11:44 -04:00
end
end
2019-09-30 19:24:18 -04:00
csv_foreach :org_unit_kinds \
2019-10-21 02:09:51 -04:00
do |(codename, parent, short_name, name, resource_type)|
2019-09-30 19:12:04 -04:00
parent = parent.blank? ? nil : OrgUnitKind.find_by!(codename: parent.strip)
2019-09-30 18:37:59 -04:00
codename.strip!
short_name.strip!
name.strip!
2019-10-21 02:09:51 -04:00
resource_type = resource_type.strip.presence
2019-09-30 18:37:59 -04:00
next if OrgUnitKind.find_by codename: codename
OrgUnitKind.create!(
codename: codename,
short_name: short_name,
name: name,
2019-09-30 19:12:04 -04:00
parent_kind: parent,
2019-10-21 02:09:51 -04:00
resource_type: resource_type,
2019-09-30 18:37:59 -04:00
)
end
2019-09-30 19:24:18 -04:00
csv_foreach :relation_statuses \
do |(org_unit_kind, codename, name)|
2019-09-30 19:12:04 -04:00
org_unit_kind =
if org_unit_kind.blank?
nil
else
OrgUnitKind.find_by!(codename: org_unit_kind.strip)
end
2019-09-21 10:39:28 -04:00
codename.strip!
name.strip!
RelationStatus.where(codename: codename).first_or_create! \
do |new_relation_status|
new_relation_status.org_unit_kind = org_unit_kind
2019-09-21 10:39:28 -04:00
new_relation_status.name = name
end
end
2018-12-08 22:56:06 -05:00
Rails.application.settings(:superuser).tap do |config|
2020-02-14 16:42:01 -05:00
account = Account.where(
nickname: config[:nickname],
).first_or_create! do |new_account|
new_account.public_name = config[:public_name]
new_account.biography = config[:biography]
new_account.contact_list = ContactList.new
end
2019-08-11 15:27:06 -04:00
user = User.where(email: config[:email]).first_or_create! do |new_user|
2018-12-08 22:07:39 -05:00
new_user.password = config[:password]
2018-12-08 21:14:33 -05:00
new_user.confirmed_at = Time.zone.now
2020-02-14 16:42:01 -05:00
new_user.account = account
2019-08-11 15:27:06 -04:00
end
user.account.update! superuser: true
2018-12-08 21:14:33 -05:00
end
2019-10-21 04:04:26 -04:00
lpr_org_unit_kind = OrgUnitKind.find_by! codename: :lpr
reg_dept_org_unit_kind = OrgUnitKind.find_by! codename: :reg_dept
fed_management_org_unit_kind = OrgUnitKind.find_by! codename: :fed_management
fed_supervision_org_unit_kind = OrgUnitKind.find_by! codename: :fed_supervision
reg_management_org_unit_kind = OrgUnitKind.find_by! codename: :reg_management
reg_supervision_org_unit_kind = OrgUnitKind.find_by! codename: :reg_supervision
lpr_org_unit = OrgUnit.where(kind: lpr_org_unit_kind).first_or_create!(
short_name: 'ЛПР',
name: 'Либертарианская партия России',
)
OrgUnit.where(
kind: fed_management_org_unit_kind,
parent_unit: lpr_org_unit,
).first_or_create!(
short_name: 'ФК ЛПР',
name: 'Федеральный комитет Либертарианской партии России',
)
OrgUnit.where(
kind: fed_supervision_org_unit_kind,
parent_unit: lpr_org_unit,
).first_or_create!(
short_name: 'ЦКРК ЛПР',
name: 'Центральная контрольно-ревизионная комиссия ' \
'Либертарианской партии России',
)
FederalSubject.all.each do |federal_subject|
reg_dept_org_unit = OrgUnit.where(
kind: reg_dept_org_unit_kind,
parent_unit: lpr_org_unit,
resource: federal_subject,
).first_or_create!(
short_name: "РО ЛПР (#{federal_subject.native_name})",
name: 'Региональное отделение Либертарианской партии России ' \
"(#{federal_subject.native_name})",
)
OrgUnit.where(
kind: reg_management_org_unit_kind,
parent_unit: reg_dept_org_unit,
).first_or_create!(
short_name: "РК РО ЛПР (#{federal_subject.native_name})",
name: 'Руководящий комитет регионального отделения ' \
"Либертарианской партии России (#{federal_subject.native_name})",
)
OrgUnit.where(
kind: reg_supervision_org_unit_kind,
parent_unit: reg_dept_org_unit,
).first_or_create!(
short_name: "РКРК ЛПР (#{federal_subject.native_name})",
name: 'Региональная контрольно-ревизионная комиссия' \
"Либертарианской партии России (#{federal_subject.native_name})",
)
end