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