chinese_regions_rails 0.1.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +72 -5
- data/lib/chinese_regions_rails.rb +1 -1
- data/lib/chinese_regions_rails/engine.rb +17 -0
- data/lib/chinese_regions_rails/helper.rb +79 -0
- data/lib/chinese_regions_rails/version.rb +1 -1
- data/lib/generators/chinese_regions/install_generator.rb +3 -5
- data/lib/regions.db +0 -0
- data/lib/regions.json +5 -5
- data/lib/tasks/seed.rake +4 -7
- metadata +6 -6
- data/lib/chinese_regions_rails/railtie.rb +0 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 188854b0a371acfad97c2d8df7fbe21d36b76bfd
|
4
|
+
data.tar.gz: 2dd67aa17a6a79e74967448b0721953b7b9a0a35
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5f376ad65d7bc3d4ce3c176270523a96db196a8f921e554167cc489f51787dbea963d32f91c158235cd1666957eafa797c364ebc41c1b42788663a7394636f6f
|
7
|
+
data.tar.gz: 78c0c16f824babd5c5de679b7f51e27e3abdc975aa670bf9b95e840ac7e550ba91c200fe39f8e7710cf46cdf9e3b45d24c2daa4c43f8acd03e6a43b8416a9941
|
data/README.md
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
数据来源 https://github.com/xixilive/chinese_regions_db 。
|
5
5
|
支持 active_record 和 mongoid 。
|
6
6
|
|
7
|
-
##
|
7
|
+
## 安装
|
8
8
|
|
9
9
|
Gemfile
|
10
10
|
|
@@ -12,7 +12,7 @@ Gemfile
|
|
12
12
|
gem 'chinese_regions_rails'
|
13
13
|
```
|
14
14
|
|
15
|
-
|
15
|
+
Copy migrations
|
16
16
|
|
17
17
|
```
|
18
18
|
rails g chinese_regions:install
|
@@ -25,13 +25,80 @@ rake db:migrate
|
|
25
25
|
rake db:seed:regions
|
26
26
|
```
|
27
27
|
|
28
|
-
##
|
28
|
+
## 使用
|
29
|
+
|
30
|
+
直接使用 Region model
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
class User < ActiveRecord::Base
|
34
|
+
belongs_to :city, class_name: "ChineseRegions::Region"
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
#### 使用 [city-picker](https://github.com/tshi0912/city-picker) 作为表单输入
|
39
|
+
|
40
|
+
application.css 添加下面代码
|
41
|
+
```
|
42
|
+
*= require city-picker
|
43
|
+
```
|
44
|
+
|
45
|
+
application.js 中在 jQuery 后面引入文件
|
46
|
+
```
|
47
|
+
//= require city-picker.data
|
48
|
+
//= require city-picker
|
49
|
+
//= require chinese-regions
|
50
|
+
```
|
51
|
+
|
52
|
+
表单里可以使用 `city_picker_tag(method, value, options)` 和 `city_picker(method, options)`, 这两个标签只是简单的封装,options 可以传入任何东西, 比如 city-picer 的 level 和 simple 选项可以通过 data 传入 `data: { level: :city, simple: true }`。
|
53
|
+
```erb
|
54
|
+
<%= form_tag do %>
|
55
|
+
<div style="position: relative;">
|
56
|
+
# Region model
|
57
|
+
<%= city_picker_tag :city_id, @user.city %>
|
58
|
+
# Regoin id
|
59
|
+
<%= city_picker_tag :city_id, "610101", data: { level: :city } %>
|
60
|
+
</div>
|
61
|
+
<% end %>
|
29
62
|
|
30
|
-
|
31
|
-
|
63
|
+
<% form_for @user do |f| %>
|
64
|
+
<div style="position: relative;">
|
65
|
+
<%= f.city_pciker :city_id %>
|
66
|
+
</div>
|
67
|
+
<% end %>
|
68
|
+
```
|
69
|
+
|
70
|
+
## Region model
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
class ChineseRegions::Region < ActiveRecord
|
74
|
+
belogns_to :parent # 上级 region
|
75
|
+
has_many :children # 下级 region
|
76
|
+
end
|
77
|
+
```
|
78
|
+
|
79
|
+
model 包含的属性
|
80
|
+
* id
|
81
|
+
* parent_id
|
32
82
|
* level
|
33
83
|
* name
|
34
84
|
* alias
|
35
85
|
* pinyin
|
36
86
|
* abbr
|
37
87
|
* zip
|
88
|
+
|
89
|
+
### Change log
|
90
|
+
|
91
|
+
* v1.0.0
|
92
|
+
|
93
|
+
**Break Change**
|
94
|
+
|
95
|
+
* 直接使用 engine 里的 ChineseRegions::Region model,而不是添加 model 到用户项目
|
96
|
+
* 引入 city-picker
|
97
|
+
|
98
|
+
* v0.1.*
|
99
|
+
|
100
|
+
修改 Sqlite3 数据源到 json
|
101
|
+
|
102
|
+
* v0.0.*
|
103
|
+
|
104
|
+
生成 Region model 到用户项目
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "rails"
|
2
|
+
require "chinese_regions_rails/helper"
|
3
|
+
|
4
|
+
module ChineseRegions
|
5
|
+
|
6
|
+
class Engine < Rails::Engine
|
7
|
+
isolate_namespace ChineseRegions
|
8
|
+
|
9
|
+
initializer "chinese_regions_rails.helper_and_builder" do
|
10
|
+
ActiveSupport.on_load :action_view do
|
11
|
+
include ChineseRegionsRails::Helper
|
12
|
+
ActionView::Helpers::FormBuilder.send(:include, ChineseRegionsRails::Builder)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
|
3
|
+
module ChineseRegionsRails
|
4
|
+
module Helper
|
5
|
+
def city_picker_tag(name, value = nil, options = {})
|
6
|
+
HelperUtil.setup_options(options)
|
7
|
+
|
8
|
+
_raw_label = HelperUtil.raw_label(value)
|
9
|
+
_raw_value = HelperUtil.raw_value(value)
|
10
|
+
|
11
|
+
|
12
|
+
output = ActiveSupport::SafeBuffer.new
|
13
|
+
output << text_field_tag(nil, _raw_label, options)
|
14
|
+
output << hidden_field_tag(name, _raw_value, {
|
15
|
+
data: { "city-picker-value-for" => options[:data][:id] }
|
16
|
+
})
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module Builder
|
21
|
+
def city_picker(method, options = {})
|
22
|
+
HelperUtil.setup_options(options)
|
23
|
+
|
24
|
+
options[:name] = nil
|
25
|
+
|
26
|
+
value = retrive_value(method)
|
27
|
+
_raw_label = HelperUtil.raw_label(value)
|
28
|
+
_raw_value = HelperUtil.raw_value(value)
|
29
|
+
|
30
|
+
options[:value] = _raw_label
|
31
|
+
|
32
|
+
output = ActiveSupport::SafeBuffer.new
|
33
|
+
output << text_field(nil, options)
|
34
|
+
output << hidden_field(method, {
|
35
|
+
value: _raw_value,
|
36
|
+
data: { "city-picker-value-for" => options[:data][:id] }
|
37
|
+
})
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
def retrive_value(method)
|
42
|
+
object = self.instance_variable_get("@object")
|
43
|
+
object.public_send(method) if object
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
module HelperUtil
|
48
|
+
def self.setup_options(options)
|
49
|
+
input_identifer = SecureRandom.hex(8)
|
50
|
+
if data = options[:data]
|
51
|
+
data.merge!(toggle: "city-picker", id: input_identifer)
|
52
|
+
else
|
53
|
+
data = {toggle: "city-picker", id: input_identifer}
|
54
|
+
options[:data] = data
|
55
|
+
end
|
56
|
+
options.merge!(readonly: true)
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.raw_label(value)
|
60
|
+
return unless value
|
61
|
+
|
62
|
+
if value.is_a?(ChineseRegions::Region)
|
63
|
+
value.full_name("/")
|
64
|
+
else
|
65
|
+
ChineseRegions::Region.find_by(id: value).full_name("/")
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.raw_value(value)
|
70
|
+
return unless value
|
71
|
+
|
72
|
+
if value.is_a?(ChineseRegions::Region)
|
73
|
+
value.id
|
74
|
+
else
|
75
|
+
value
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -3,12 +3,10 @@ require "rails/generators"
|
|
3
3
|
module ChineseRegions
|
4
4
|
class InstallGenerator < Rails::Generators::Base
|
5
5
|
|
6
|
-
desc "Generate region model.
|
7
|
-
|
6
|
+
desc "Generate region model migration."
|
8
7
|
def install
|
9
|
-
|
10
|
-
generate "model", "#{class_name} code:string parent_code:string name:string alias:string pinyin:string abbr:string zip:string level:integer"
|
8
|
+
rake 'railties:install:migrations'
|
11
9
|
end
|
12
10
|
|
13
11
|
end
|
14
|
-
end
|
12
|
+
end
|
data/lib/regions.db
CHANGED
Binary file
|
data/lib/regions.json
CHANGED
@@ -20813,7 +20813,7 @@
|
|
20813
20813
|
"code": "450000",
|
20814
20814
|
"parent_code": null,
|
20815
20815
|
"name": "广西壮族自治区",
|
20816
|
-
"alias": "
|
20816
|
+
"alias": "广西",
|
20817
20817
|
"pinyin": "GuangXiZhuangZu",
|
20818
20818
|
"abbr": "GXZZ",
|
20819
20819
|
"zip": "530000",
|
@@ -27924,7 +27924,7 @@
|
|
27924
27924
|
"parent_code": null,
|
27925
27925
|
"name": "陕西省",
|
27926
27926
|
"alias": "陕西",
|
27927
|
-
"pinyin": "
|
27927
|
+
"pinyin": "ShaanXi",
|
27928
27928
|
"abbr": "SX",
|
27929
27929
|
"zip": "710000",
|
27930
27930
|
"level": 1
|
@@ -30553,7 +30553,7 @@
|
|
30553
30553
|
"code": "640000",
|
30554
30554
|
"parent_code": null,
|
30555
30555
|
"name": "宁夏回族自治区",
|
30556
|
-
"alias": "
|
30556
|
+
"alias": "宁夏",
|
30557
30557
|
"pinyin": "NingXiaHuiZu",
|
30558
30558
|
"abbr": "NXHZ",
|
30559
30559
|
"zip": "750000",
|
@@ -30833,7 +30833,7 @@
|
|
30833
30833
|
"code": "650000",
|
30834
30834
|
"parent_code": null,
|
30835
30835
|
"name": "新疆维吾尔自治区",
|
30836
|
-
"alias": "
|
30836
|
+
"alias": "新疆",
|
30837
30837
|
"pinyin": "XinJiangWeiWuEr",
|
30838
30838
|
"abbr": "XJWWE",
|
30839
30839
|
"zip": "830000",
|
@@ -33189,4 +33189,4 @@
|
|
33189
33189
|
"zip": "833418",
|
33190
33190
|
"level": 3
|
33191
33191
|
}
|
33192
|
-
]
|
33192
|
+
]
|
data/lib/tasks/seed.rake
CHANGED
@@ -1,8 +1,5 @@
|
|
1
|
-
# default model regions
|
2
1
|
# rake db:seed:regions
|
3
2
|
|
4
|
-
# TODO: rake db:seed:regions xxx_regions
|
5
|
-
# custom model to xxx_regions
|
6
3
|
require 'active_support/core_ext/string/inflections'
|
7
4
|
require "chinese_regions_rails"
|
8
5
|
|
@@ -13,12 +10,12 @@ namespace :db do
|
|
13
10
|
|
14
11
|
desc "seed regions data to db"
|
15
12
|
task regions: :environment do
|
16
|
-
|
17
|
-
args[:model_class] = 'region'
|
18
|
-
region_class = args[:model_class].classify.constantize
|
13
|
+
region_class = ChineseRegions::Region
|
19
14
|
|
20
15
|
ChineseRegionsRails::JsonDataSource.query_regions do |region_param|
|
21
|
-
|
16
|
+
region_param["id"] = region_param.delete("code")
|
17
|
+
region_param["parent_id"] = region_param.delete("parent_code")
|
18
|
+
region_class.create!(region_param)
|
22
19
|
end
|
23
20
|
|
24
21
|
puts "insert regions"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chinese_regions_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Basten Gao
|
@@ -24,7 +24,7 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '3.2'
|
27
|
-
description:
|
27
|
+
description: 中国省市区县数据库,包含 行政编码,邮政编码,地区拼音和简拼。 数据来源 https://github.com/xixilive/chinese_regions_db
|
28
28
|
email: bastengao@gmail.com
|
29
29
|
executables: []
|
30
30
|
extensions: []
|
@@ -35,8 +35,9 @@ files:
|
|
35
35
|
- Rakefile
|
36
36
|
- lib/chinese_regions_rails.rb
|
37
37
|
- lib/chinese_regions_rails/db.rb
|
38
|
+
- lib/chinese_regions_rails/engine.rb
|
39
|
+
- lib/chinese_regions_rails/helper.rb
|
38
40
|
- lib/chinese_regions_rails/json_data_source.rb
|
39
|
-
- lib/chinese_regions_rails/railtie.rb
|
40
41
|
- lib/chinese_regions_rails/version.rb
|
41
42
|
- lib/dump_json.rb
|
42
43
|
- lib/generators/chinese_regions/install_generator.rb
|
@@ -66,12 +67,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
66
67
|
version: 1.8.11
|
67
68
|
requirements: []
|
68
69
|
rubyforge_project:
|
69
|
-
rubygems_version: 2.
|
70
|
+
rubygems_version: 2.6.11
|
70
71
|
signing_key:
|
71
72
|
specification_version: 4
|
72
|
-
summary:
|
73
|
+
summary: 中国省市区县数据库
|
73
74
|
test_files:
|
74
75
|
- spec/chinese_regions_rails/db_spec.rb
|
75
76
|
- spec/chinese_regions_rails/json_data_source_spec.rb
|
76
77
|
- spec/helper.rb
|
77
|
-
has_rdoc:
|