Improve code
This commit is contained in:
parent
39acdd74c9
commit
30c5bf90d1
38
db/seeds.rb
38
db/seeds.rb
|
@ -2,18 +2,16 @@
|
||||||
|
|
||||||
require 'csv'
|
require 'csv'
|
||||||
|
|
||||||
seeds_dirname = Rails.root.join 'config', 'seeds'
|
def csv_foreach(filename, &block)
|
||||||
|
CSV.foreach(
|
||||||
federal_subjects_filename = seeds_dirname.join 'federal_subjects.csv'
|
Rails.root.join('config', 'seeds', "#{filename}.csv"),
|
||||||
contact_networks_filename = seeds_dirname.join 'contact_networks.csv'
|
|
||||||
org_unit_kinds_filename = seeds_dirname.join 'org_unit_kinds.csv'
|
|
||||||
relation_statuses_filename = seeds_dirname.join 'relation_statuses.csv'
|
|
||||||
relation_transitions_filename = seeds_dirname.join 'relation_transitions.csv'
|
|
||||||
|
|
||||||
CSV.foreach(
|
|
||||||
federal_subjects_filename,
|
|
||||||
col_sep: '|',
|
col_sep: '|',
|
||||||
) do |(id, english_name, native_name, number, timezone, centre)|
|
&block
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
csv_foreach :federal_subjects \
|
||||||
|
do |(id, english_name, native_name, number, timezone, centre)|
|
||||||
id = Integer(id.strip)
|
id = Integer(id.strip)
|
||||||
english_name.strip!
|
english_name.strip!
|
||||||
native_name.strip!
|
native_name.strip!
|
||||||
|
@ -30,7 +28,8 @@ CSV.foreach(
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
CSV.foreach contact_networks_filename, col_sep: '|' do |(id, codename, name)|
|
csv_foreach :contact_networks \
|
||||||
|
do |(id, codename, name)|
|
||||||
id = Integer(id.strip)
|
id = Integer(id.strip)
|
||||||
codename.strip!
|
codename.strip!
|
||||||
name.strip!
|
name.strip!
|
||||||
|
@ -41,10 +40,8 @@ CSV.foreach contact_networks_filename, col_sep: '|' do |(id, codename, name)|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
CSV.foreach(
|
csv_foreach :org_unit_kinds \
|
||||||
org_unit_kinds_filename,
|
do |(codename, parent, short_name, name)|
|
||||||
col_sep: '|',
|
|
||||||
) do |(codename, parent, short_name, name)|
|
|
||||||
parent = parent.blank? ? nil : OrgUnitKind.find_by!(codename: parent.strip)
|
parent = parent.blank? ? nil : OrgUnitKind.find_by!(codename: parent.strip)
|
||||||
|
|
||||||
codename.strip!
|
codename.strip!
|
||||||
|
@ -61,10 +58,8 @@ CSV.foreach(
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
CSV.foreach(
|
csv_foreach :relation_statuses \
|
||||||
relation_statuses_filename,
|
do |(org_unit_kind, codename, name)|
|
||||||
col_sep: '|',
|
|
||||||
) do |(org_unit_kind, codename, name)|
|
|
||||||
org_unit_kind =
|
org_unit_kind =
|
||||||
if org_unit_kind.blank?
|
if org_unit_kind.blank?
|
||||||
nil
|
nil
|
||||||
|
@ -81,7 +76,8 @@ CSV.foreach(
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
CSV.foreach relation_transitions_filename, col_sep: '|' do |(from, to, name)|
|
csv_foreach :relation_transitions \
|
||||||
|
do |(from, to, name)|
|
||||||
from.strip!
|
from.strip!
|
||||||
to.strip!
|
to.strip!
|
||||||
name.strip!
|
name.strip!
|
||||||
|
|
Reference in New Issue