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
|
# File 'lib/generators/china/templates/import_china_cities.rb', line 3
def self.up
ChinaCity.delete_all
file = File.new("#{China::Engine.root.to_s}/db/cities.json", 'r')
parser = Yajl::Parser.new
db = parser.parse(file)
db.each do |province_node|
cities_nodes = province_node['cities']
options = province_node.delete_if {|k,v|k == 'cities'}
province = ChinaCity.create! options
puts province.name
if cities_nodes
cities_nodes.each do |city_node|
district_nodes = city_node['cities']
options = city_node.delete_if {|k,v|k == 'cities'}
city = ChinaCity.create! options.merge!({'parent_id' => province.id })
puts city.name
if district_nodes
district_nodes.each do |district_node|
options = district_node.delete_if {|k,v|k == 'cities'}
district = ChinaCity.create! options.merge!({'parent_id' => city.id})
puts district.name
end
end
end
end
end
end
|