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,6 +37,7 @@ COUNT = {
37 :categories => 0, 37 :categories => 0,
38 } 38 }
39 39
  40 +$default_env = Environment.default_
40 def step(what) 41 def step(what)
41 COUNT[what] += 1 42 COUNT[what] += 1
42 puts "#{what}: #{COUNT[what]}" 43 puts "#{what}: #{COUNT[what]}"
@@ -45,7 +46,7 @@ end @@ -45,7 +46,7 @@ end
45 def new_cat(name, parent = nil) 46 def new_cat(name, parent = nil)
46 path = (parent ? parent.path + '/' : '') + name.to_slug 47 path = (parent ? parent.path + '/' : '') + name.to_slug
47 pc = Category.find_by_path(path) 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 step(:categories) 50 step(:categories)
50 pc 51 pc
51 end 52 end
@@ -53,7 +54,7 @@ end @@ -53,7 +54,7 @@ end
53 def new_region(name, parent, lat, lng) 54 def new_region(name, parent, lat, lng)
54 path = (parent ? parent.path + '/' : '') + name.to_slug 55 path = (parent ? parent.path + '/' : '') + name.to_slug
55 region = Region.find_by_path(path) 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 step(:regions) 58 step(:regions)
58 region 59 region
59 end 60 end
@@ -61,12 +62,13 @@ end @@ -61,12 +62,13 @@ end
61 def new_ent(data, products, consumptions) 62 def new_ent(data, products, consumptions)
62 count = 2 63 count = 2
63 ident = data[:identifier] 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 ident = data[:identifier] + "-#{count}" 67 ident = data[:identifier] + "-#{count}"
66 count += 1 68 count += 1
67 end 69 end
68 data[:identifier] = ident 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 products.each do |p| 72 products.each do |p|
71 ent.products.create(p) 73 ent.products.create(p)
72 end 74 end