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

107 lines
2.9 KiB
Ruby
Raw Normal View History

2018-12-01 18:42:36 +00:00
# frozen_string_literal: true
require 'csv'
2018-12-01 18:42:36 +00:00
2019-09-19 03:21:29 +00:00
seeds_dirname = Rails.root.join 'config', 'seeds'
2019-09-21 16:59:24 +00:00
federal_subjects_filename = seeds_dirname.join 'federal_subjects.csv'
contact_networks_filename = seeds_dirname.join 'contact_networks.csv'
2019-09-30 22:37:59 +00:00
org_unit_kinds_filename = seeds_dirname.join 'org_unit_kinds.csv'
2019-09-21 16:59:24 +00:00
relation_statuses_filename = seeds_dirname.join 'relation_statuses.csv'
relation_transitions_filename = seeds_dirname.join 'relation_transitions.csv'
2019-07-22 09:14:14 +00:00
CSV.foreach(
federal_subjects_filename,
col_sep: '|',
) do |(id, english_name, native_name, number, timezone, centre)|
2019-07-19 21:59:07 +00:00
id = Integer(id.strip)
english_name.strip!
2019-07-19 21:59:07 +00:00
native_name.strip!
2019-07-19 22:52:14 +00:00
number = Integer(number.strip.sub(/\A0*/, ''))
2019-07-20 02:40:56 +00:00
timezone.strip!
2019-07-22 09:14:14 +00:00
centre.strip!
2018-12-01 18:42:36 +00:00
2019-07-19 21:59:07 +00:00
FederalSubject.where(id: id).first_or_create! do |new_federal_subject|
new_federal_subject.english_name = english_name
2019-06-23 15:59:44 +00:00
new_federal_subject.native_name = native_name
2019-07-19 22:52:14 +00:00
new_federal_subject.number = number
2019-07-20 02:40:56 +00:00
new_federal_subject.timezone = timezone
2019-07-22 09:14:14 +00:00
new_federal_subject.centre = centre
2019-04-27 12:40:12 +00:00
end
2018-12-01 18:42:36 +00:00
end
2018-12-04 01:57:16 +00:00
CSV.foreach contact_networks_filename, col_sep: '|' do |(id, codename, name)|
2019-08-04 21:11:44 +00:00
id = Integer(id.strip)
codename.strip!
name.strip!
2019-08-04 21:11:44 +00: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 21:11:44 +00:00
end
end
2019-09-30 22:37:59 +00:00
CSV.foreach(
org_unit_kinds_filename,
col_sep: '|',
) do |(codename, parent, short_name, name)|
codename.strip!
parent = parent.strip.presence
short_name.strip!
name.strip!
next if OrgUnitKind.find_by codename: codename
OrgUnitKind.create!(
codename: codename,
short_name: short_name,
name: name,
parent_kind: parent.nil? ? nil : OrgUnitKind.find_by!(codename: parent),
)
end
2019-09-30 23:06:00 +00:00
CSV.foreach(
relation_statuses_filename,
col_sep: '|',
) do |(org_unit_kind, codename, name)|
org_unit_kind = OrgUnitKind.find_by! codename: org_unit_kind.strip
2019-09-21 14:39:28 +00:00
codename.strip!
name.strip!
RelationStatus.where(codename: codename).first_or_create! \
do |new_relation_status|
new_relation_status.name = name
end
end
2019-09-21 16:59:24 +00:00
CSV.foreach relation_transitions_filename, col_sep: '|' do |(from, to, name)|
from.strip!
to.strip!
name.strip!
from_status = RelationStatus.find_by! codename: from unless from.empty?
to_status = RelationStatus.find_by! codename: to
RelationTransition.where(
from_status: from_status,
to_status: to_status,
).first_or_create! do |new_relation_transition|
new_relation_transition.name = name
end
end
2018-12-09 03:56:06 +00:00
Rails.application.settings(:superuser).tap do |config|
2019-08-11 19:27:06 +00:00
user = User.where(email: config[:email]).first_or_create! do |new_user|
2018-12-09 03:07:39 +00:00
new_user.password = config[:password]
2018-12-09 02:14:33 +00:00
new_user.confirmed_at = Time.zone.now
new_user.account = Account.create!(
2019-04-28 13:34:46 +00:00
nickname: config[:nickname],
public_name: config[:public_name],
2019-04-28 13:34:46 +00:00
biography: config[:biography],
2019-09-04 22:05:00 +00:00
contact_list: ContactList.new,
)
2019-08-11 19:27:06 +00:00
end
user.account.update! superuser: true
2018-12-09 02:14:33 +00:00
end