fbes_populate_helper.rb
1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
require File.dirname(__FILE__) + '/../config/environment'
STATES = {}
[ 'Acre',
'Alagoas',
'Amazonas',
'Amapá',
'Bahia',
'Ceará',
'Distrito Federal',
'Espírito Santo',
'Goiás',
'Maranhão',
'Minas Gerais',
'Mato Grosso do Sul',
'Mato Grosso',
'Pará',
'Paraíba',
'Pernambuco',
'Piauí',
'Paraná',
'Rio de Janeiro',
'Rio Grande do Norte',
'Rondônia',
'Roraima',
'Rio Grande do Sul',
'Santa Catarina',
'Sergipe',
'São Paulo',
'Tocantins'
].each do |statename|
st = Region.find_by_name(statename)
STATES[st.id] = st
end
COUNT = {
:enterprises => 0,
:regions => 0,
:categories => 0,
}
def step(what)
COUNT[what] += 1
puts "#{what}: #{COUNT[what]}"
end
def new_cat(name, parent = nil)
path = (parent ? parent.path + '/' : '') + name.to_slug
pc = Category.find_by_path(path)
pc = ProductCategory.create!(:name => name, :parent => parent, :environment => Environment.default) unless pc
step(:categories)
pc
end
def new_region(name, parent, lat, lng)
path = (parent ? parent.path + '/' : '') + name.to_slug
region = Region.find_by_path(path)
region = Region.create!(:name => name, :parent => parent, :lat => lat, :lng => lng, :environment => Environment.default) unless region
step(:regions)
region
end
def new_ent(data, products, consumptions)
count = 2
ident = data[:identifier]
while Enterprise.find_by_identifier(ident)
ident = data[:identifier] + "-#{count}"
count += 1
end
data[:identifier] = ident
ent = Enterprise.create!({:environment => Environment.default, :enabled => false}.merge(data))
products.each do |p|
ent.products.create(p)
end
consumptions.each do |c|
ent.consumptions.create(c)
end
step(:enterprises)
end