china_regions 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+
3
+ class CreateChinaRegionsTables < ActiveRecord::Migration
4
+ def change
5
+ unless table_exists? 'provinces'
6
+ create_table :provinces do |t|
7
+ t.string :name
8
+ t.string :name_en
9
+ t.string :name_abbr
10
+ t.timestamps
11
+ end
12
+
13
+ add_index :provinces, :name
14
+ add_index :provinces, :name_en
15
+ add_index :provinces, :name_abbr
16
+ end
17
+
18
+ unless table_exists? 'cities'
19
+ create_table :cities do |t|
20
+ t.string :name
21
+ t.integer :province_id
22
+ t.integer :level
23
+ t.string :zip_code
24
+ t.string :name_en
25
+ t.string :name_abbr
26
+ t.timestamps
27
+ end
28
+ add_index :cities, :name
29
+ add_index :cities, :province_id
30
+ add_index :cities, :level
31
+ add_index :cities, :zip_code
32
+ add_index :cities, :name_en
33
+ add_index :cities, :name_abbr
34
+ end
35
+
36
+ unless table_exists? 'districts'
37
+ create_table :districts do |t|
38
+ t.string :name
39
+ t.integer :city_id
40
+ t.string :name_en
41
+ t.string :name_abbr
42
+ t.timestamps
43
+ end
44
+ add_index :districts, :name
45
+ add_index :districts, :city_id
46
+ add_index :districts, :name_en
47
+ add_index :districts, :name_abbr
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,13 @@
1
+ # encoding: utf-8
2
+
3
+ module ChinaRegions
4
+ class ResgionsGenerator < Rails::Generators::NamedBase
5
+ source_root File.expand_path('../../../../../app', __FILE__)
6
+
7
+ def copy_models_file
8
+ copy_file "models/province.rb", "app/models/province.rb"
9
+ copy_file "models/city.rb", "app/models/city.rb"
10
+ copy_file "models/district.rb", "app/models/district.rb"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,46 @@
1
+ # encoding: utf-8
2
+
3
+ require 'yaml'
4
+
5
+ namespace :china_regions do
6
+ desc "Import regions to database from config/cities.yml"
7
+
8
+ task :import => :environment do
9
+ file_path = File.join(Rails.root, 'config', 'cities.yml')
10
+ data = File.open(file_path) { |file| YAML.load(file) }
11
+ remove_china_regins && load_to_db(data)
12
+ puts "Data import is done."
13
+ end
14
+
15
+ def remove_china_regins
16
+ Province.delete_all && City.delete_all && District.delete_all
17
+ end
18
+
19
+ def load_to_db(data)
20
+ data.each do |province_name, province_hash|
21
+ province = Province.create({
22
+ name: province_name,
23
+ name_en: province_hash['name_en'],
24
+ name_abbr: province_hash['name_abbr']
25
+ })
26
+ province_hash['cities'].each do |city_name, city_hash|
27
+ city = province.cities.create({
28
+ name: city_name,
29
+ name_en: city_hash['name_en'],
30
+ name_abbr: city_hash['name_abbr'],
31
+ zip_code: city_hash['zip_code'],
32
+ level: city_hash['level'] || 4
33
+ })
34
+ districts_hash = city_hash['districts']
35
+
36
+ districts_hash.each do |district_name, district_hash|
37
+ city.districts.create({
38
+ name: district_name,
39
+ name_en: district_hash['name_en'],
40
+ name_abbr: district_hash['name_abbr']
41
+ })
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: china_regions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Encore Shao
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: jquery-rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: China regions Ruby on rails interface
31
+ email:
32
+ - encore.shao@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE
40
+ - README.md
41
+ - Rakefile
42
+ - app/controllers/china_regions/fetch_options_controller.rb
43
+ - app/models/city.rb
44
+ - app/models/district.rb
45
+ - app/models/province.rb
46
+ - china_regions.gemspec
47
+ - config/routes.rb
48
+ - lib/china_regions.rb
49
+ - lib/china_regions/engine.rb
50
+ - lib/china_regions/helper.rb
51
+ - lib/china_regions/version.rb
52
+ - lib/generators/china_regions/install/install_generator.rb
53
+ - lib/generators/china_regions/install/templates/cities.yml
54
+ - lib/generators/china_regions/install/templates/migration.rb
55
+ - lib/generators/china_regions/regions/regions_generator.rb
56
+ - lib/tasks/china_region.rake
57
+ homepage: http://github.com/encoreshao
58
+ licenses: []
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 1.8.23
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: China regions Ruby on rails interface
81
+ test_files: []