Commit 37fbb82349fbb3cda614c40ba408a13f4d9e6d8d

Authored by MoisesMachado
1 parent 05740740

ActionItem261: added a helper to facilitate the importing of fbes data


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1666 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing 1 changed file with 52 additions and 0 deletions   Show diff stats
script/fbes_populate_helper.rb 0 → 100644
@@ -0,0 +1,52 @@ @@ -0,0 +1,52 @@
  1 +require File.dirname(__FILE__) + '/../config/environment'
  2 +
  3 + STATES = {
  4 + 12 => Region.find_by_name('Acre'),
  5 + 27 => Region.find_by_name('Alagoas'),
  6 + 13 => Region.find_by_name('Amazonas'),
  7 + 16 => Region.find_by_name('Amapá'),
  8 + 29 => Region.find_by_name('Bahia'),
  9 + 23 => Region.find_by_name('Ceará'),
  10 + 53 => Region.find_by_name('Distrito Federal'),
  11 + 32 => Region.find_by_name('Espírito Santo'),
  12 + 52 => Region.find_by_name('Goiás'),
  13 + 21 => Region.find_by_name('Maranhão'),
  14 + 31 => Region.find_by_name('Minas Gerais'),
  15 + 50 => Region.find_by_name('Mato Grosso do Sul'),
  16 + 51 => Region.find_by_name('Mato Grosso'),
  17 + 15 => Region.find_by_name('Pará'),
  18 + 25 => Region.find_by_name('Paraíba'),
  19 + 26 => Region.find_by_name('Pernambuco'),
  20 + 22 => Region.find_by_name('Piauí'),
  21 + 41 => Region.find_by_name('Paraná'),
  22 + 33 => Region.find_by_name('Rio de Janeiro'),
  23 + 24 => Region.find_by_name('Rio Grande do Norte'),
  24 + 11 => Region.find_by_name('Rondônia'),
  25 + 14 => Region.find_by_name('Roraima'),
  26 + 43 => Region.find_by_name('Rio Grande do Sul'),
  27 + 42 => Region.find_by_name('Santa Catarina'),
  28 + 28 => Region.find_by_name('Sergipe'),
  29 + 35 => Region.find_by_name('São Paulo'),
  30 + 17 => Region.find_by_name('Tocantins'),
  31 +}
  32 +
  33 + def new_cat(name, parent = nil)
  34 + path = (parent ? parent.path + '/' : '') + name.to_slug
  35 + ProductCategory.find_by_path(path) || ProductCategory.create!(:name => name, :parent => parent, :environment => Environment.default)
  36 + end
  37 +
  38 + def new_ent(data, products, consumptions)
  39 + posfix = ''
  40 + count = 1
  41 + while Enterprise.find_by_identifier(data[:identifier] = (data[:identifier] + posfix)) do
  42 + count += 1
  43 + posfix = "-#{count}"
  44 + end
  45 + ent = Enterprise.create!({:environment => Environment.default}.merge(data))
  46 + products.each do |p|
  47 + ent.products.create!(p)
  48 + end
  49 + consumptions.each do |c|
  50 + ent.consumptions.create!(c) unless ent.consumptions.find(:first, :conditions => c)
  51 + end
  52 + end