Commit 23f77c83321ba3f58433c28694a1360541e8c78f
1 parent
3cb0a488
Exists in
master
and in
22 other branches
ActionItem261: fixing script
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2073 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
1 changed file
with
50 additions
and
38 deletions
Show diff stats
script/fbes_populate_helper.rb
| 1 | 1 | require File.dirname(__FILE__) + '/../config/environment' |
| 2 | 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'), | |
| 3 | +STATES = {} | |
| 4 | + | |
| 5 | +[ 'Acre', | |
| 6 | + 'Alagoas', | |
| 7 | + 'Amazonas', | |
| 8 | + 'Amapá', | |
| 9 | + 'Bahia', | |
| 10 | + 'Ceará', | |
| 11 | + 'Distrito Federal', | |
| 12 | + 'Espírito Santo', | |
| 13 | + 'Goiás', | |
| 14 | + 'Maranhão', | |
| 15 | + 'Minas Gerais', | |
| 16 | + 'Mato Grosso do Sul', | |
| 17 | + 'Mato Grosso', | |
| 18 | + 'Pará', | |
| 19 | + 'Paraíba', | |
| 20 | + 'Pernambuco', | |
| 21 | + 'Piauí', | |
| 22 | + 'Paraná', | |
| 23 | + 'Rio de Janeiro', | |
| 24 | + 'Rio Grande do Norte', | |
| 25 | + 'Rondônia', | |
| 26 | + 'Roraima', | |
| 27 | + 'Rio Grande do Sul', | |
| 28 | + 'Santa Catarina', | |
| 29 | + 'Sergipe', | |
| 30 | + 'São Paulo', | |
| 31 | + 'Tocantins' | |
| 32 | +].each do |statename| | |
| 33 | + st = Region.find_by_name(statename) | |
| 34 | + STATES[st.id] = st | |
| 35 | +end | |
| 36 | + | |
| 37 | +COUNT = { | |
| 38 | + :enterprises => 0, | |
| 39 | + :regions => 0, | |
| 40 | + :categories => 0, | |
| 31 | 41 | } |
| 32 | 42 | |
| 33 | -def step | |
| 34 | - print '.' | |
| 35 | - $stdout.flush | |
| 43 | +def step(what) | |
| 44 | + COUNT[what] += 1 | |
| 45 | + puts "#{what}: #{COUNT[what]}" | |
| 36 | 46 | end |
| 37 | 47 | |
| 38 | 48 | def new_cat(name, parent = nil) |
| 39 | 49 | path = (parent ? parent.path + '/' : '') + name.to_slug |
| 40 | - pc = ProductCategory.find_by_path(path) || ProductCategory.create!(:name => name, :parent => parent, :environment => Environment.default) | |
| 41 | - step | |
| 50 | + pc = Category.find_by_path(path) | |
| 51 | + pc = ProductCategory.create!(:name => name, :parent => parent, :environment => Environment.default) unless pc | |
| 52 | + step(:categories) | |
| 42 | 53 | pc |
| 43 | 54 | end |
| 44 | 55 | |
| 45 | 56 | def new_region(name, parent, lat, lng) |
| 46 | 57 | path = (parent ? parent.path + '/' : '') + name.to_slug |
| 47 | - region = Region.find_by_path(path) || Region.create!(:name => name, :parent => parent, :lat => lat, :lng => lng, :environment => Environment.default) | |
| 48 | - step | |
| 58 | + region = Region.find_by_path(path) | |
| 59 | + region = Region.create!(:name => name, :parent => parent, :lat => lat, :lng => lng, :environment => Environment.default) unless region | |
| 60 | + step(:regions) | |
| 49 | 61 | region |
| 50 | 62 | end |
| 51 | 63 | |
| ... | ... | @@ -59,10 +71,10 @@ end |
| 59 | 71 | data[:identifier] = ident |
| 60 | 72 | ent = Enterprise.create!({:environment => Environment.default, :enabled => false}.merge(data)) |
| 61 | 73 | products.each do |p| |
| 62 | - ent.products.create!(p) unless ent.products.find(:first, :conditions => p) | |
| 74 | + ent.products.create(p) | |
| 63 | 75 | end |
| 64 | 76 | consumptions.each do |c| |
| 65 | - ent.consumptions.create!(c) unless ent.consumptions.find(:first, :conditions => c) | |
| 77 | + ent.consumptions.create(c) | |
| 66 | 78 | end |
| 67 | - step | |
| 79 | + step(:enterprises) | |
| 68 | 80 | end | ... | ... |