20151002180659_create_siorg_institutions.rb
2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#encoding: utf-8
require "i18n"
class CreateSiorgInstitutions < ActiveRecord::Migration
def up
governmental_power = GovernmentalPower.where("name ILIKE ?", "Executivo").first
governmental_sphere = GovernmentalSphere.where("name ILIKE ?", "Federal").first
env = Environment.default
if env && governmental_power && governmental_sphere
CSV.foreach("plugins/spb_migrations/files/orgaos_siorg.csv", :headers => true) do |row|
template = Community["institution"]
community = Community.where("identifier ILIKE ?", row["Nome"].to_slug).first
unless community
institution = Institution.where("acronym ILIKE ?", row["Sigla"]).first
community = institution.community if institution
end
community = Community.new unless community
community.environment = env if community.environment.blank?
community.name = row["Nome"].rstrip
community.country = row["Pais"]
community.state = row["Estado"]
community.city = row["Cidade"]
community.template = template if template
unless community.save
print "F"
next
end
juridical_nature = JuridicalNature.where("name ILIKE ? OR name ILIKE ?", "#{I18n.transliterate(row['Natureza Jurídica'].rstrip)}", "#{row['Natureza Jurídica'].rstrip}").first
juridical_nature = JuridicalNature.create!(name: row['Natureza Jurídica'].rstrip) unless juridical_nature
institution = Hash.new
institution[:name] = row["Nome"]
institution[:siorg_code] = row["Código do SIORG"]
institution[:acronym] = row["Sigla"]
institution[:governmental_sphere] = governmental_sphere
institution[:governmental_power] = governmental_power
institution[:juridical_nature] = juridical_nature
institution[:sisp] = (row["SISP"] == "Sim")
institution[:cnpj] = row["CNPJ"]
institution[:community] = community
if community.institution
community.institution.update_attributes(institution)
else
institution[:community] = community
community.institution = PublicInstitution.create!(institution)
end
if community.save
print "."
else
print "F"
end
end
end
puts ""
end
def down
say "This can't be reverted"
end
end