china_regions 0.4.5 → 1.0.0
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.
- checksums.yaml +4 -4
- data/README.md +19 -17
- data/README.zh.md +20 -18
- data/bin/console +8 -0
- data/bin/setup +3 -0
- data/china_regions.gemspec +5 -2
- data/config/locales/en.yml +0 -1
- data/config/locales/zh.yml +0 -1
- data/config/routes.rb +2 -1
- data/lib/china_regions.rb +7 -3
- data/lib/china_regions/version.rb +1 -1
- data/lib/generators/china_regions/install_generator.rb +4 -10
- data/lib/generators/china_regions/templates/migration.rb +6 -1
- data/lib/tasks/china_regions.rake +143 -0
- metadata +53 -9
- data/lib/custom_tasks/china_regions.rake +0 -88
- data/lib/generators/china_regions/templates/cities.yml +0 -10829
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5aec74f02a9856df9798317291e9fc587c07a6473f8c13639b878e8b6a10bf67
|
4
|
+
data.tar.gz: 4aaea0730f59300c09c9ad5685f38cfa5e061df00f0d5c81e6aa4c386c04fa6d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
-
|
25
|
+
bundle install
|
26
26
|
|
27
27
|
Run the generator:
|
28
28
|
|
29
|
-
|
29
|
+
rails g china_regions:install
|
30
30
|
|
31
31
|
Then you can see the changes that happened to the console:
|
32
|
-
*
|
33
|
-
*
|
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
|
-
|
37
|
+
rake db:migrate
|
39
38
|
|
40
|
-
|
39
|
+
Copy Models [Province`, `City`, `District] into your project:
|
41
40
|
|
42
|
-
|
41
|
+
please run `rails g` see the generator list.
|
43
42
|
|
43
|
+
rails g china_regions:regions
|
44
44
|
|
45
|
-
|
45
|
+
Newly added models:
|
46
46
|
|
47
|
-
|
47
|
+
- create app/models/province.rb
|
48
|
+
- create app/models/city.rb
|
49
|
+
- create app/models/district.rb
|
48
50
|
|
49
|
-
|
51
|
+
Download and import the latest regions to your project:
|
50
52
|
|
51
|
-
|
53
|
+
rake china_regions:all
|
52
54
|
|
53
|
-
|
54
|
-
|
55
|
-
|
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
|
107
|
+
[Chinese](https://github.com/encoreshao/china_regions/blob/master/README.zh.md)
|
106
108
|
|
107
109
|
### Contributing
|
108
110
|
|
data/README.zh.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
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
|
-
|
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
|
-
|
23
|
+
bundle install
|
24
24
|
|
25
25
|
#### 开始构建城市数据
|
26
26
|
|
27
27
|
复制所需文件到你的项目中:
|
28
28
|
|
29
|
-
|
29
|
+
rails g china_regions:install
|
30
30
|
|
31
31
|
随后你可以看到控制台发生的变化:
|
32
|
-
* 复制 db/migrate/xxxxxxxxxxx_create_china_regions_tables.rb 文件到db/migrate
|
33
|
-
* 复制
|
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
|
-
|
37
|
+
rake db:migrate
|
39
38
|
|
40
|
-
|
39
|
+
将所需的模型(Models) [`Province`, `City`, `District`] 到您的应用程式中:
|
41
40
|
|
42
|
-
|
41
|
+
你可以执行 `rails g` 查看到 `generator` 列表.
|
43
42
|
|
43
|
+
rails g china_regions:regions
|
44
44
|
|
45
|
-
|
45
|
+
查看 app/models:
|
46
46
|
|
47
|
-
|
47
|
+
- create app/models/province.rb
|
48
|
+
- create app/models/city.rb
|
49
|
+
- create app/models/district.rb
|
48
50
|
|
49
|
-
|
51
|
+
下载并导入最新数据到你的项目中:
|
50
52
|
|
51
|
-
|
53
|
+
rake china_regions:all
|
52
54
|
|
53
|
-
|
54
|
-
|
55
|
-
|
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
|
107
|
+
[English](https://github.com/encoreshao/china_regions/blob/master/README.md)
|
106
108
|
|
107
109
|
### 贡献者
|
108
110
|
|
data/bin/console
ADDED
data/bin/setup
ADDED
data/china_regions.gemspec
CHANGED
@@ -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
|
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
|
data/config/locales/en.yml
CHANGED
data/config/locales/zh.yml
CHANGED
data/config/routes.rb
CHANGED
@@ -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' =>
|
4
|
+
match '/china_regions/fetch_options' =>
|
5
|
+
ChinaRegions::FetchOptionsController.action(:index), via: [:get]
|
5
6
|
end
|
data/lib/china_regions.rb
CHANGED
@@ -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
|
-
|
5
|
+
if defined? Rails
|
6
|
+
require 'china_regions/engine'
|
7
|
+
require 'china_regions/helpers/form_helper'
|
8
|
+
end
|
9
|
+
|
10
|
+
module ChinaRegions
|
11
|
+
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
|
-
|
20
|
-
|
21
|
-
|
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
|