china_region_fu 0.0.4 → 0.0.5
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 +7 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +2 -1
- data/README.md +121 -53
- data/Rakefile +6 -2
- data/app/controllers/china_region_fu/fetch_options_controller.rb +26 -20
- data/app/models/city.rb +5 -3
- data/app/models/district.rb +15 -13
- data/app/models/province.rb +6 -5
- data/china_region_fu.gemspec +24 -17
- data/config/routes.rb +2 -2
- data/lib/china_region_fu.rb +0 -2
- data/lib/china_region_fu/engine.rb +14 -1
- data/lib/china_region_fu/exceptions.rb +7 -0
- data/lib/china_region_fu/helpers/formtastic.rb +16 -0
- data/lib/china_region_fu/helpers/helpers.rb +70 -0
- data/lib/china_region_fu/helpers/simple_form.rb +23 -0
- data/lib/china_region_fu/helpers/utilities.rb +54 -0
- data/lib/china_region_fu/version.rb +1 -1
- data/lib/generators/china_region_fu/install/install_generator.rb +6 -5
- data/lib/generators/china_region_fu/models/USAGE +10 -0
- data/lib/generators/china_region_fu/{mvc/mvc_generator.rb → models/models_generator.rb} +2 -2
- data/lib/tasks/region.rake +4 -4
- data/spec/china_region_fu_spec.rb +9 -0
- data/spec/spec_helper.rb +2 -0
- metadata +83 -21
- data/lib/china_region_fu/helper.rb +0 -97
- data/lib/generators/china_region_fu/install/templates/regions.yml +0 -10829
- data/lib/generators/china_region_fu/mvc/USAGE +0 -17
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fb7a4ba9afffa06e9951668c3b118fff38af7384
|
4
|
+
data.tar.gz: 6d1d2d68a4f9cfefebcf6b9a80f0554d6fec9b8a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 931fa75b8e33bf6692cd59a6cb1a074341bef82be9052a71a1570f38208153bea10a0f20ff788f9a7132db51b18ef49bd4fc9e066542cd34faca29908c46ef45
|
7
|
+
data.tar.gz: 9cc9a236a8cf053e9aea0b55549c616fa673ffb56dd3d991e6e26bf0c2db58756ca02039a9522f0353f416f83f1ddc4ba34ade89506eec3e658f00a2f67de777
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
# ChinaRegionFu
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
[](http://badge.fury.io/rb/china_region_fu)
|
4
|
+
[](https://travis-ci.org/Xuhao/china_region_fu)
|
5
|
+
|
6
|
+
ChinaRegionFu provide the region data of china.You will got complete China region data after use this gem.
|
5
7
|
|
6
8
|
## Installation
|
7
9
|
|
@@ -16,20 +18,20 @@ Run bundler command to install the gem:
|
|
16
18
|
After you install the gem, you need run the generator:
|
17
19
|
|
18
20
|
rails g china_region_fu:install
|
19
|
-
|
21
|
+
|
20
22
|
It will:
|
21
|
-
* Generate `db/migrate/<timestamp>create_china_region_tables.rb` migrate file to your app
|
22
|
-
*
|
23
|
+
* Generate `db/migrate/<timestamp>create_china_region_tables.rb` migrate file to your app.
|
24
|
+
* Download [https://github.com/Xuhao/china_region_data/raw/master/regions.yml](https://github.com/Xuhao/china_region_data/raw/master/regions.yml) to config/regions.yml.
|
23
25
|
* Run `rake db:migrate`.
|
24
26
|
* Run `rake region:import`.
|
25
27
|
|
26
28
|
Now you have there ActiveRecord modules: `Province`, `City`, `District`.
|
27
|
-
|
28
|
-
|
29
|
+
|
30
|
+
Region data if from [ChinaRegionData](https://github.com/Xuhao/china_region_data), check it out to see what kind of data you have now.
|
29
31
|
|
30
32
|
If you want to customize the region modules you can run the generator:
|
31
33
|
|
32
|
-
rails g china_region_fu:
|
34
|
+
rails g china_region_fu:models
|
33
35
|
|
34
36
|
This will create:
|
35
37
|
|
@@ -38,57 +40,123 @@ If you want to customize the region modules you can run the generator:
|
|
38
40
|
create app/models/district.rb
|
39
41
|
|
40
42
|
So you can do what you want to do in this files.
|
41
|
-
|
43
|
+
|
42
44
|
## Usage
|
43
45
|
|
44
46
|
#### Model
|
45
47
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
48
|
+
```ruby
|
49
|
+
a = Province.last
|
50
|
+
a.name # => "台湾省"
|
51
|
+
a.cities.pluck(:name) # => ["嘉义市", "台南市", "新竹市", "台中市", "基隆市", "台北市"]
|
52
|
+
|
53
|
+
Province.first.districts.pluck(:name) # => ["延庆县", "密云县", "平谷区", ...]
|
54
|
+
|
55
|
+
c = City.last
|
56
|
+
c.name # => "酒泉市"
|
57
|
+
c.short_name # => "酒泉"
|
58
|
+
c.zip_code # => "735000"
|
59
|
+
c.pinyin # => "jiuquan"
|
60
|
+
c.pinyin_abbr # => "jq"
|
61
|
+
c.districts.pluck(:name) # => ["敦煌市", "玉门市", "阿克塞哈萨克族自治县", "肃北蒙古族自治县", "安西县", ...]
|
62
|
+
c.brothers.pluck(:name) # => ["甘南藏族自治州", "临夏回族自治州", "陇南市", ...]
|
63
|
+
|
64
|
+
d = District.last
|
65
|
+
d.name # => "吉木乃县"
|
66
|
+
d.short_name # => "吉木乃"
|
67
|
+
d.pinyin # => "jimunai"
|
68
|
+
d.pinyin_abbr # => "jmn"
|
69
|
+
d.city.name # => "阿勒泰地区"
|
70
|
+
d.province.name # => "新疆维吾尔自治区"
|
71
|
+
```
|
72
|
+
|
60
73
|
#### View
|
61
74
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
75
|
+
##### Form helpers
|
76
|
+
|
77
|
+
```erb
|
78
|
+
<%= form_for(@post) do |f| %>
|
79
|
+
<div class="field">
|
80
|
+
<%= f.label :province, '选择地区:' %><br />
|
81
|
+
|
82
|
+
# FormBuilder
|
83
|
+
<%= f.region_select :city %>
|
84
|
+
<%= f.region_select [:province, :city, :district], province_prompt: 'Do', city_prompt: 'it', district_prompt: 'like this' %>
|
85
|
+
<%= f.region_select [:province, :city], include_blank: true %>
|
86
|
+
<%= f.region_select [:city, :district] %>
|
87
|
+
<%= f.region_select [:province, :district] %>
|
88
|
+
|
89
|
+
# FormHelper
|
90
|
+
<%= region_select :post, :province %>
|
91
|
+
<%= region_select :post, [:province, :city, :district] %>
|
92
|
+
...
|
93
|
+
|
94
|
+
# FormTagHelper
|
95
|
+
<%= region_select_tag :province, class: 'my', include_blank: true %>
|
96
|
+
<%= region_select_tag [:province, :city, :district], province_prompt: 'Do', city_prompt: 'it', district_prompt: 'like this', class: 'my' %>
|
97
|
+
...
|
98
|
+
</div>
|
99
|
+
<% end %>
|
100
|
+
```
|
101
|
+
|
102
|
+
##### SimpleForm
|
103
|
+
|
104
|
+
```erb
|
105
|
+
<%= simple_form_for(@post) do |f| %>
|
106
|
+
<%= f.input :province, as: :region, collection: Province.select('id, name'), sub_region: :city %>
|
107
|
+
<%= f.input :city, as: :region, sub_region: :district %>
|
108
|
+
<%= f.input :district, as: :region %>
|
109
|
+
<%= js_for_region_ajax %>
|
110
|
+
<% end %>
|
111
|
+
```
|
112
|
+
|
113
|
+
##### Formtastic
|
114
|
+
|
115
|
+
```erb
|
116
|
+
<%= semantic_form_for(@post) do |f| %>
|
117
|
+
<%= f.input :province, as: :region, collection: Province.select('id, name'), sub_region: :city %>
|
118
|
+
<%= f.input :city, as: :region, sub_region: :district %>
|
119
|
+
<%= f.input :district, as: :region %>
|
120
|
+
<%= js_for_region_ajax %>
|
121
|
+
<% end %>
|
122
|
+
```
|
123
|
+
|
124
|
+
##### Fetch sub regions by Ajax
|
125
|
+
|
126
|
+
Once select one province, we want fetch cities of the selected province and fill the city select box automatically. If you use `:region_select_tag` and FormBuilder/FormHelper method aka `:region_select`, you need do nothing for this. If you use simple_form or normal form helper like `:select_tag` or `:select`, to implement this, what you need to do is add below helper in your form:
|
127
|
+
|
128
|
+
```erb
|
129
|
+
<%= js_for_region_ajax %>
|
130
|
+
```
|
131
|
+
|
132
|
+
it will render:
|
133
|
+
|
134
|
+
```html
|
135
|
+
<script type="text/javascript">
|
136
|
+
//<![CDATA[
|
137
|
+
$(function(){
|
138
|
+
$('body').on('change', '.region_select', function(event) {
|
139
|
+
var self, $targetDom;
|
140
|
+
self = $(event.currentTarget);
|
141
|
+
$targetDom = $('#' + self.data('region-target'));
|
142
|
+
if ($targetDom.size() > 0) {
|
143
|
+
$.getJSON('/china_region_fu/fetch_options', {klass: self.data('region-target-kalss'), parent_klass: self.data('region-klass'), parent_id: self.val()}, function(data) {
|
144
|
+
var options = [];
|
145
|
+
$('option[value!=""]', $targetDom).remove();
|
146
|
+
$.each(data, function(index, value) {
|
147
|
+
options.push("<option value='" + value.id + "'>" + value.name + "</option>");
|
148
|
+
});
|
149
|
+
$targetDom.append(options.join(''));
|
150
|
+
});
|
151
|
+
}
|
152
|
+
});
|
153
|
+
});
|
154
|
+
//]]>
|
155
|
+
</script>
|
156
|
+
```
|
157
|
+
|
158
|
+
## Online example
|
159
|
+
[医院之家](http://www.yihub.com/ "医院").
|
92
160
|
|
93
161
|
## Contributing
|
94
162
|
|
data/Rakefile
CHANGED
@@ -1,31 +1,37 @@
|
|
1
1
|
module ChinaRegionFu
|
2
2
|
class FetchOptionsController < ::ActionController::Metal
|
3
|
-
|
3
|
+
|
4
4
|
def index
|
5
|
-
if params_valid?
|
6
|
-
|
7
|
-
regions
|
8
|
-
if has_level_column?(params[:klass])
|
9
|
-
regions = regions.order('level ASC')
|
10
|
-
else
|
11
|
-
regions = regions.order('name ASC')
|
12
|
-
end
|
5
|
+
if params_valid? and klass and parent_klass and (parent_object = parent_klass.find(params[:parent_id]))
|
6
|
+
regions = parent_object.__send__(klass.model_name.plural).select("#{klass.table_name}.id, #{klass.table_name}.name").order("#{klass.table_name}.name ASC")
|
7
|
+
regions.reorder("#{klass.table_name}.level ASC") if has_level_column?
|
13
8
|
self.response_body = regions.to_json
|
14
9
|
else
|
15
10
|
self.response_body = [].to_json
|
16
11
|
end
|
17
12
|
end
|
18
|
-
|
19
|
-
|
13
|
+
|
20
14
|
private
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
15
|
+
|
16
|
+
def has_level_column?
|
17
|
+
klass.column_names.to_a.include?('level')
|
18
|
+
end
|
19
|
+
|
20
|
+
def klass
|
21
|
+
params[:klass].sub(/_id\Z/, '').classify.safe_constantize
|
22
|
+
end
|
23
|
+
|
24
|
+
def parent_klass
|
25
|
+
parent_klass_name.classify.safe_constantize
|
26
|
+
end
|
27
|
+
|
28
|
+
def parent_klass_name
|
29
|
+
params[:parent_klass].sub(/_id\Z/, '')
|
30
|
+
end
|
31
|
+
|
32
|
+
def params_valid?
|
33
|
+
params[:klass].present? and parent_klass_name=~ /\Aprovince\Z|\Acity\Z/i and params[:parent_id].present?
|
34
|
+
end
|
35
|
+
|
30
36
|
end
|
31
37
|
end
|
data/app/models/city.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
-
#
|
1
|
+
# coding: utf-8
|
2
2
|
class City < ActiveRecord::Base
|
3
|
-
|
4
|
-
|
3
|
+
if Rails.version < '4.0'
|
4
|
+
attr_accessible :name, :province_id, :level, :zip_code, :pinyin, :pinyin_abbr
|
5
|
+
end
|
6
|
+
|
5
7
|
belongs_to :province
|
6
8
|
has_many :districts, dependent: :destroy
|
7
9
|
|
data/app/models/district.rb
CHANGED
@@ -1,16 +1,18 @@
|
|
1
|
-
#
|
1
|
+
# coding: utf-8
|
2
2
|
class District < ActiveRecord::Base
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
if Rails.version < '4.0'
|
4
|
+
attr_accessible :name, :city_id, :pinyin, :pinyin_abbr
|
5
|
+
end
|
6
|
+
|
7
|
+
belongs_to :city
|
8
|
+
has_one :province, through: :city
|
9
|
+
|
10
|
+
def short_name
|
11
|
+
@short_name ||= name.gsub(/区|县|市|自治县/,'')
|
12
|
+
end
|
13
|
+
|
14
|
+
def brothers
|
15
|
+
@brothers ||= District.where("city_id = #{city_id}")
|
16
|
+
end
|
7
17
|
|
8
|
-
def short_name
|
9
|
-
@short_name ||= name.gsub(/区|县|市|自治县/,'')
|
10
|
-
end
|
11
|
-
|
12
|
-
def brothers
|
13
|
-
@brothers ||= District.where("city_id = #{city_id}")
|
14
|
-
end
|
15
|
-
|
16
18
|
end
|
data/app/models/province.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
1
|
class Province < ActiveRecord::Base
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
2
|
+
if Rails.version < '4.0'
|
3
|
+
attr_accessible :name, :pinyin, :pinyin_abbr
|
4
|
+
end
|
5
|
+
|
6
|
+
has_many :cities, dependent: :destroy
|
7
|
+
has_many :districts, through: :cities
|
7
8
|
end
|
data/china_region_fu.gemspec
CHANGED
@@ -1,19 +1,26 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'china_region_fu/version'
|
3
5
|
|
4
|
-
Gem::Specification.new do |
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "china_region_fu"
|
8
|
+
spec.version = ChinaRegionFu::VERSION
|
9
|
+
spec.authors = ["Xuhao"]
|
10
|
+
spec.email = ["xuhao@rubyfans.com"]
|
11
|
+
spec.description = %q{China region Ruby on rails interface}
|
12
|
+
spec.summary = %q{China region Ruby on rails interface}
|
13
|
+
spec.homepage = "https://github.com/Xuhao/china_region_fu"
|
14
|
+
spec.license = "MIT"
|
10
15
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_dependency 'activesupport'
|
25
|
+
spec.add_dependency 'actionpack'
|
26
|
+
end
|
data/config/routes.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
Rails.application.routes.draw do
|
2
|
-
|
3
|
-
end
|
2
|
+
get '/china_region_fu/fetch_options', to: ChinaRegionFu::FetchOptionsController.action(:index)
|
3
|
+
end
|
data/lib/china_region_fu.rb
CHANGED
@@ -1,5 +1,18 @@
|
|
1
1
|
require "rails"
|
2
|
+
require 'china_region_fu/helpers/helpers'
|
2
3
|
|
3
4
|
module ChinaRegionFu
|
4
|
-
class Engine < Rails::Engine
|
5
|
+
class Engine < Rails::Engine
|
6
|
+
initializer "form helper extensions" do
|
7
|
+
ActiveSupport.on_load :action_view do
|
8
|
+
ActionView::Base.send :include, ChinaRegionFu::Helpers
|
9
|
+
ActionView::Helpers::FormBuilder.send :include, ChinaRegionFu::FormBuilder
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
initializer "simple extension" do
|
14
|
+
require "china_region_fu/helpers/simple_form" if Object.const_defined?("SimpleForm")
|
15
|
+
require "china_region_fu/helpers/formtastic" if Object.const_defined?("Formtastic")
|
16
|
+
end
|
17
|
+
end
|
5
18
|
end
|