Commit a0b4c60ebf1d3e181646aa36bebf727edf6a68e1

Authored by MoisesMachado
1 parent 8b957e61

ActionItem501: improving fbes script peformance


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2103 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing 1 changed file with 6 additions and 4 deletions   Show diff stats
script/fbes_populate_helper.rb
... ... @@ -37,6 +37,7 @@ COUNT = {
37 37 :categories => 0,
38 38 }
39 39  
  40 +$default_env = Environment.default_
40 41 def step(what)
41 42 COUNT[what] += 1
42 43 puts "#{what}: #{COUNT[what]}"
... ... @@ -45,7 +46,7 @@ end
45 46 def new_cat(name, parent = nil)
46 47 path = (parent ? parent.path + '/' : '') + name.to_slug
47 48 pc = Category.find_by_path(path)
48   - pc = ProductCategory.create!(:name => name, :parent => parent, :environment => Environment.default) unless pc
  49 + pc = ProductCategory.create!(:name => name, :parent => parent, :environment => $default_env) unless pc
49 50 step(:categories)
50 51 pc
51 52 end
... ... @@ -53,7 +54,7 @@ end
53 54 def new_region(name, parent, lat, lng)
54 55 path = (parent ? parent.path + '/' : '') + name.to_slug
55 56 region = Region.find_by_path(path)
56   - region = Region.create!(:name => name, :parent => parent, :lat => lat, :lng => lng, :environment => Environment.default) unless region
  57 + region = Region.create!(:name => name, :parent => parent, :lat => lat, :lng => lng, :environment => $default_env) unless region
57 58 step(:regions)
58 59 region
59 60 end
... ... @@ -61,12 +62,13 @@ end
61 62 def new_ent(data, products, consumptions)
62 63 count = 2
63 64 ident = data[:identifier]
64   - while Enterprise.find_by_identifier(ident)
  65 + idents = Enterprise.find(:all, :conditons => ['identifier like ?', ident + '%']).map(&:identifier)
  66 + while idents.includes?(ident)
65 67 ident = data[:identifier] + "-#{count}"
66 68 count += 1
67 69 end
68 70 data[:identifier] = ident
69   - ent = Enterprise.create!({:environment => Environment.default, :enabled => false}.merge(data))
  71 + ent = Enterprise.create!({:environment => $default_env, :enabled => false}.merge(data))
70 72 products.each do |p|
71 73 ent.products.create(p)
72 74 end
... ...