china_regions 0.4.5 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a4de2d9eced27245300d96f23af7e9e5499374f789c975631acbcc286b33c2fb
4
- data.tar.gz: eae0d2259e6e32d3557a3f78441ff040c9b271244573f792cfe4f764de55ef72
3
+ metadata.gz: 5aec74f02a9856df9798317291e9fc587c07a6473f8c13639b878e8b6a10bf67
4
+ data.tar.gz: 4aaea0730f59300c09c9ad5685f38cfa5e061df00f0d5c81e6aa4c386c04fa6d
5
5
  SHA512:
6
- metadata.gz: 48da9ff3cb7646afb56e391cde3de6d8f721c324f0a4e508b57c6fdeb64dd44445cc466767c76287fce7a898757b75e8da421f94d2858ae11c302935d8a23d96
7
- data.tar.gz: 70ef8dd05114aad397f1612c06f6c7df60ba57caafcf4f7a97e9a6f7d63769fc26f11a93b7aecb87dafc3d84dece439a1679f519c21d7cdc1800667d00784d59
6
+ metadata.gz: 36d42b1e5e2f74b5492e3cf6d32bf79e13ab131de92e2c9f683a44a8f975a3424bd2851d58947909665684d2c3772ab18a6330371c159351fd9f67cdf454eaaa
7
+ data.tar.gz: aaffe9a0e03be688218b84aaa8a9005b3300f643b81d6e7cb5a19690ee8b539e7f32ee233e16e92874588f3fbe400da24c23ac97ea33b9483e9839f149c08d93
data/README.md CHANGED
@@ -10,7 +10,7 @@ Chinese provinces, cities, regions [prefecture-level cities] Ruby on Rails code,
10
10
 
11
11
  If you are using ChinaRegions version 0.1.x be sure to run:
12
12
 
13
- >> rails g china_regions:regions
13
+ rails g china_regions:regions
14
14
 
15
15
  to have the javascript file copied over into your project.
16
16
 
@@ -22,37 +22,39 @@ Add it to your Gemfile:
22
22
 
23
23
  Run the following command to install it:
24
24
 
25
- >> bundle install
25
+ bundle install
26
26
 
27
27
  Run the generator:
28
28
 
29
- >> rails g china_regions:install
29
+ rails g china_regions:install
30
30
 
31
31
  Then you can see the changes that happened to the console:
32
- * copy `db/migrate/xxxxxxxxxxx_create_china_regions_tables.rb` to your project `db/migrate`
33
- * copy datasource cities.yml config 目录. config/cities.yml
34
- * copy regions.en.yml 和 regions.zh.yml 文件到 config/locales 目录
32
+ * Copy `db/migrate/xxxxxxxxxxx_create_china_regions_tables.rb` to `db/migrate` folder.
33
+ * Copy `regions.en.yml` and `regions.zh.yml` files to `config/locales` folders
35
34
 
36
35
  Create tables (provinces, cities, districts):
37
36
 
38
- >> rake db:migrate
37
+ rake db:migrate
39
38
 
40
- Import data:
39
+ Copy Models [Province`, `City`, `District] into your project:
41
40
 
42
- >> rake china_regions:import
41
+ please run `rails g` see the generator list.
43
42
 
43
+ rails g china_regions:regions
44
44
 
45
- Copy Models [`Province`, `City`, `District`] into your app:
45
+ Newly added models:
46
46
 
47
- 你可以执行 `rails g` 查看到 generator LIST.
47
+ - create app/models/province.rb
48
+ - create app/models/city.rb
49
+ - create app/models/district.rb
48
50
 
49
- >> rails g china_regions:regions
51
+ Download and import the latest regions to your project:
50
52
 
51
- Newly added models:
53
+ rake china_regions:all
52
54
 
53
- create app/models/province.rb
54
- create app/models/city.rb
55
- create app/models/district.rb
55
+ * 1. Downloading regions from `Administrative-divisions-of-China` to `db/regions` folder.
56
+ - db/regions/pca-code.json
57
+ * 2. Import the regions into provinces and cities, districts
56
58
 
57
59
  ### Usage
58
60
 
@@ -102,7 +104,7 @@ Prior choice:
102
104
 
103
105
  ### Other languages
104
106
 
105
- [Chinese Readme](https://github.com/encoreshao/china_regions/blob/master/README.zh.md)
107
+ [Chinese](https://github.com/encoreshao/china_regions/blob/master/README.zh.md)
106
108
 
107
109
  ### Contributing
108
110
 
@@ -1,4 +1,4 @@
1
- # ChinaRegions
1
+ # 中国地区
2
2
 
3
3
  中国省份,城市,地区[地级市]Ruby on Rails 程式代码, Ruby (> 1.9.x) And Rails (> 4.0)
4
4
 
@@ -10,7 +10,7 @@
10
10
 
11
11
  复制所需文件到你的项目中:
12
12
 
13
- >> rails g china_regions:regions
13
+ rails g china_regions:regions
14
14
 
15
15
  ### 如何引入china_regions到你的项目
16
16
 
@@ -20,39 +20,41 @@
20
20
 
21
21
  安装:
22
22
 
23
- >> bundle install
23
+ bundle install
24
24
 
25
25
  #### 开始构建城市数据
26
26
 
27
27
  复制所需文件到你的项目中:
28
28
 
29
- >> rails g china_regions:install
29
+ rails g china_regions:install
30
30
 
31
31
  随后你可以看到控制台发生的变化:
32
- * 复制 db/migrate/xxxxxxxxxxx_create_china_regions_tables.rb 文件到db/migrate 目录
33
- * 复制 数据源 cities.yml config 目录. config/cities.yml
34
- * 复制 regions.en.yml 和 regions.zh.yml 文件到 config/locales 目录
32
+ * 复制 `db/migrate/xxxxxxxxxxx_create_china_regions_tables.rb` 文件到 `db/migrate` 目录中
33
+ * 复制 `regions.en.yml` 和 `regions.zh.yml` 配置文件到 `config/locales` 目录中
35
34
 
36
35
  创建所需的表 (provinces, cities, districts):
37
36
 
38
- >> rake db:migrate
37
+ rake db:migrate
39
38
 
40
- 导入数据到对应表中:
39
+ 将所需的模型(Models) [`Province`, `City`, `District`] 到您的应用程式中:
41
40
 
42
- >> rake china_regions:import
41
+ 你可以执行 `rails g` 查看到 `generator` 列表.
43
42
 
43
+ rails g china_regions:regions
44
44
 
45
- 将所需的模型(Models) [`Province`, `City`, `District`] 到您的应用程式中:
45
+ 查看 app/models:
46
46
 
47
- 你可以执行 `rails g` 查看到 generator LIST.
47
+ - create app/models/province.rb
48
+ - create app/models/city.rb
49
+ - create app/models/district.rb
48
50
 
49
- >> rails g china_regions:regions
51
+ 下载并导入最新数据到你的项目中:
50
52
 
51
- 查看 app/models:
53
+ rake china_regions:all
52
54
 
53
- create app/models/province.rb
54
- create app/models/city.rb
55
- create app/models/district.rb
55
+ * 1. 从 `Administrative-divisions-of-China` 下载最新的地区信息到 `db/regions` 目录中.
56
+ - db/regions/pca-code.json
57
+ * 2. 将下载后的地区信息导入数据库中
56
58
 
57
59
  ### 如何在View中使用
58
60
 
@@ -102,7 +104,7 @@
102
104
 
103
105
  ### 其它语言
104
106
 
105
- [English Readme](https://github.com/encoreshao/china_regions/blob/master/README.md)
107
+ [English](https://github.com/encoreshao/china_regions/blob/master/README.md)
106
108
 
107
109
  ### 贡献者
108
110
 
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'china_regions'
5
+
6
+ # If you use this, don't forget to add pry to your Gemfile!
7
+ require 'pry'
8
+ Pry.start
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env bash
2
+
3
+ bundle install
@@ -6,8 +6,8 @@ require File.expand_path('lib/china_regions/version', __dir__)
6
6
  Gem::Specification.new do |gem|
7
7
  gem.authors = ['Encore Shao']
8
8
  gem.email = ['encore.shao@gmail.com']
9
- gem.description = 'China regions Ruby on rails interface'
10
- gem.summary = 'Rails 4 version of dropdowns for all provinces, cities,
9
+ gem.description = 'China Regions is Ruby on rails interface'
10
+ gem.summary = 'Rails 4+ version of dropdowns for all provinces, cities,
11
11
  and districts in China.'
12
12
  gem.homepage = 'http://github.com/encoreshao'
13
13
 
@@ -18,6 +18,9 @@ Gem::Specification.new do |gem|
18
18
  gem.require_paths = ['lib']
19
19
  gem.version = ChinaRegions::VERSION
20
20
 
21
+ gem.add_dependency 'down'
21
22
  gem.add_dependency 'jquery-rails'
23
+ gem.add_dependency 'ruby-pinyin'
24
+ gem.add_development_dependency 'pry'
22
25
  gem.add_development_dependency 'rubocop'
23
26
  end
@@ -18,7 +18,6 @@ en:
18
18
  province: Province
19
19
  name_en: English Name
20
20
  name_abbr: Short Name
21
- zip_code: Zip
22
21
  level: Level
23
22
  district:
24
23
  name: Name
@@ -18,7 +18,6 @@ zh:
18
18
  province: 省份
19
19
  name_en: 拼音
20
20
  name_abbr: 简称
21
- zip_code: 邮编
22
21
  level: 等级
23
22
  district:
24
23
  name: 名称
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  Rails.application.routes.draw do
4
- match '/china_regions/fetch_options' => ChinaRegions::FetchOptionsController.action(:index), via: [:get]
4
+ match '/china_regions/fetch_options' =>
5
+ ChinaRegions::FetchOptionsController.action(:index), via: [:get]
5
6
  end
@@ -1,7 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'china_regions/version'
4
- require 'china_regions/engine' if defined? Rails
5
- require 'china_regions/helpers/form_helper'
6
4
 
7
- module ChinaRegions; end
5
+ if defined? Rails
6
+ require 'china_regions/engine'
7
+ require 'china_regions/helpers/form_helper'
8
+ end
9
+
10
+ module ChinaRegions
11
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ChinaRegions
4
- VERSION = '0.4.5'
4
+ VERSION = '1.0.0'
5
5
  end
@@ -11,17 +11,11 @@ module ChinaRegions
11
11
  migration_template 'migration.rb', 'db/migrate/create_china_regions_tables.rb'
12
12
  end
13
13
 
14
- def copy_cities
15
- copy_file('cities.yml', 'config/cities.yml') unless File.exist?('config/cities.yml')
16
- end
17
-
18
14
  def copy_locales
19
- copy_file '../../../../config/locales/en.yml', 'config/locales/regions.en.yml' unless File.exist?('config/locales/regions.en.yml')
20
- copy_file '../../../../config/locales/zh.yml', 'config/locales/regions.zh.yml' unless File.exist?('config/locales/regions.zh.yml')
21
- end
22
-
23
- def copy_rake_tasks
24
- copy_file '../../../../lib/custom_tasks/china_regions.rake', 'lib/tasks/china_regions.rake' unless File.exist?('lib/tasks/china_regions.rake')
15
+ %w[en zh].each do |locale|
16
+ config_file = "config/locales/regions.#{locale}.yml"
17
+ copy_file "../../../../config/locales/#{locale}.yml", config_file unless File.exist?(config_file)
18
+ end
25
19
  end
26
20
 
27
21
  def self.next_migration_number(dirname)
@@ -10,6 +10,7 @@ class CreateChinaRegionsTables < ActiveRecord::Migration
10
10
  def setup_provinces
11
11
  create_table :provinces do |t|
12
12
  t.string :name
13
+ t.integer :code
13
14
  t.string :name_en
14
15
  t.string :name_abbr
15
16
 
@@ -22,9 +23,9 @@ class CreateChinaRegionsTables < ActiveRecord::Migration
22
23
  def setup_citites
23
24
  create_table :cities do |t|
24
25
  t.string :name
26
+ t.integer :code
25
27
  t.integer :province_id
26
28
  t.integer :level
27
- t.string :zip_code
28
29
  t.string :name_en
29
30
  t.string :name_abbr
30
31
 
@@ -32,17 +33,21 @@ class CreateChinaRegionsTables < ActiveRecord::Migration
32
33
  end
33
34
 
34
35
  add_index :cities, :name
36
+ add_index :cities, :province_id
35
37
  end
36
38
 
37
39
  def setup_districts
38
40
  create_table :districts do |t|
39
41
  t.string :name
40
42
  t.integer :city_id
43
+ t.integer :code
41
44
  t.string :name_en
42
45
  t.string :name_abbr
43
46
 
44
47
  t.timestamps
45
48
  end
49
+
46
50
  add_index :districts, :name
51
+ add_index :districts, :city_id
47
52
  end
48
53
  end
@@ -0,0 +1,143 @@
1
+ # frozen_string_literal: true
2
+
3
+ namespace :china_regions do
4
+ desc 'Download and import regions into tables'
5
+ task all: :environment do
6
+ Rake::Task['china_regions:download'].invoke
7
+ Rake::Task['china_regions:import'].invoke
8
+ end
9
+
10
+ desc 'Download regions from `Administrative-divisions-of-China`'
11
+ task download: :environment do
12
+ ChinaRegions::Download.all
13
+ end
14
+
15
+ desc 'Import provinces and cities and areas to database'
16
+ task import: :environment do
17
+ ChinaRegions::Import.all
18
+ end
19
+ end
20
+
21
+ module ChinaRegions
22
+ module Download
23
+ require "down"
24
+
25
+ module_function
26
+
27
+ def all(filename = 'pca-code.json')
28
+ detect_folder
29
+
30
+ downloading(filename)
31
+ end
32
+
33
+ def downloading(filename)
34
+ down(filename)
35
+
36
+ move_to(filename)
37
+ end
38
+
39
+ def down(filename)
40
+ Down.download(
41
+ github_url(filename),
42
+ destination: File.join(Rails.root, 'db', 'regions')
43
+ )
44
+ end
45
+
46
+ def detect_folder
47
+ FileUtils.mkdir_p File.join(Rails.root, 'db', 'regions')
48
+ end
49
+
50
+ def github_url(filename)
51
+ [
52
+ 'https://raw.githubusercontent.com',
53
+ 'encoreshao',
54
+ 'Administrative-divisions-of-China',
55
+ 'master',
56
+ 'dist',
57
+ filename
58
+ ].join('/')
59
+ end
60
+
61
+ def move_to(filename)
62
+ src_file = Dir.glob(File.join(Rails.root, 'db', 'regions', "*.json")).max_by { |f| File.mtime(f) }
63
+
64
+ FileUtils.mv src_file, File.join(Rails.root, 'db', 'regions', filename)
65
+ end
66
+ end
67
+
68
+ module Import
69
+ require 'json'
70
+ require 'ruby-pinyin'
71
+
72
+ module_function
73
+
74
+ def all(filename = 'pca-code.json')
75
+ data_hash(filename).each { |province_hash| creating_province(province_hash) }
76
+
77
+ puts "Imported done!"
78
+ puts ''
79
+ puts " Total of #{Province.count} provinces."
80
+ puts " Total of #{City.count} cities."
81
+ puts " Total of #{District.count} districts."
82
+ end
83
+
84
+ def creating_province(prov_hash)
85
+ province = Province.find_or_create_by(name: prov_hash['name'])
86
+ province.update(build_params(prov_hash['name'], prov_hash['code']))
87
+
88
+ prov_hash["children"].each { |city_hash| creating_city(province, city_hash) }
89
+ end
90
+
91
+ def creating_city(province, city_hash)
92
+ city = City.find_or_create_by(province: province, name: city_hash['name'])
93
+ city_params = build_params(city_hash['name'], city_hash['code'])
94
+ .merge(city_level(city_hash['name']))
95
+ city.update(city_params)
96
+
97
+ city_hash['children'].each { |district| creating_district(city, district) }
98
+ end
99
+
100
+ def city_level(city_name)
101
+ {
102
+ level: municipalities.include?(city_name) ? 1 : 4
103
+ }
104
+ end
105
+
106
+ def creating_district(city, district_hash)
107
+ district = District.find_or_create_by(city: city, name: district_hash['name'])
108
+ district.update(build_params(district_hash['name'], district_hash['code']))
109
+ end
110
+
111
+ def build_params(full_name, code)
112
+ new_name = convert_pinyin(to_decorate(full_name))
113
+ name_en = new_name.join
114
+ name_abbr = new_name.map { |e| e[0] }.join
115
+
116
+ {
117
+ code: code,
118
+ name_en: name_en,
119
+ name_abbr: name_abbr
120
+ }
121
+ end
122
+
123
+ def convert_pinyin(text)
124
+ PinYin.of_string(text)
125
+ end
126
+
127
+ def to_decorate(text)
128
+ text.gsub(/市|自治州|地区|特别行政区|区|县|自治县/, '')
129
+ end
130
+
131
+ def municipalities
132
+ %w[北京市 天津市 重庆市 上海市]
133
+ end
134
+
135
+ def data_hash(filename)
136
+ @data_hash ||= JSON.parse File.read(latest_file_path(filename))
137
+ end
138
+
139
+ def latest_file_path(filename)
140
+ File.join(Rails.root, 'db', 'regions', filename)
141
+ end
142
+ end
143
+ end