china_region_fu 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +3 -3
- data/LICENSE +22 -0
- data/README.md +94 -0
- data/Rakefile +1 -0
- data/app/controllers/china_region_fu/fetch_options_controller.rb +22 -0
- data/app/models/city.rb +1 -2
- data/app/models/district.rb +1 -1
- data/app/models/province.rb +2 -3
- data/china_region_fu.gemspec +13 -16
- data/config/routes.rb +3 -0
- data/lib/china_region_fu/helper.rb +79 -0
- data/lib/china_region_fu/version.rb +1 -1
- data/lib/china_region_fu.rb +2 -2
- data/lib/generators/china_region_fu/install/templates/migration.rb +44 -41
- metadata +12 -8
- data/README.rdoc +0 -62
data/Gemfile
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
source
|
1
|
+
source 'https://rubygems.org'
|
2
2
|
|
3
|
-
# Specify your gem's dependencies in
|
4
|
-
gemspec
|
3
|
+
# Specify your gem's dependencies in lean.gemspec
|
4
|
+
gemspec
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Xuhao
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
# ChinaRegionFu
|
2
|
+
|
3
|
+
ChinaRegionFu provide the region data of china.You will got complete chian region data after use this gem.
|
4
|
+
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Put 'gem china_region_fu' to your Gemfile:
|
9
|
+
|
10
|
+
gem china_region_fu
|
11
|
+
|
12
|
+
Run bundler command to install the gem:
|
13
|
+
|
14
|
+
bundle install
|
15
|
+
|
16
|
+
After you install the gem, you need run the generator:
|
17
|
+
|
18
|
+
rails g china_region_fu:install
|
19
|
+
|
20
|
+
It will:
|
21
|
+
* Generate `db/migrate/<timestamp>create_china_region_tables.rb` migrate file to your app, `provinces`, `cities`, `districts` table is used for store the regions.
|
22
|
+
* Copy regions.yml to config/ directory.
|
23
|
+
* Run `rake db:migrate`.
|
24
|
+
* Run `rake region:import`.
|
25
|
+
|
26
|
+
Now you have there ActiveRecord modules: `Province`, `City`, `District`.
|
27
|
+
|
28
|
+
Run with `rails g` for get generator list.
|
29
|
+
|
30
|
+
If you want to customize the region modules you can run the generator:
|
31
|
+
|
32
|
+
rails g china_region_fu:mvc
|
33
|
+
|
34
|
+
This will create:
|
35
|
+
|
36
|
+
create app/models/province.rb
|
37
|
+
create app/models/city.rb
|
38
|
+
create app/models/district.rb
|
39
|
+
|
40
|
+
So you can do what you want to do in this files.
|
41
|
+
|
42
|
+
## Usage
|
43
|
+
|
44
|
+
#### Model
|
45
|
+
|
46
|
+
a = Province.last
|
47
|
+
a.name # => "台湾省"
|
48
|
+
a.cities.map(&:name) # => ["嘉义市", "台南市", "新竹市", "台中市", "基隆市", "台北市"]
|
49
|
+
|
50
|
+
Province.first.districts.map(&:name) # => ["延庆县", "密云县", "平谷区", ...]
|
51
|
+
|
52
|
+
c = City.last
|
53
|
+
c.name # => "酒泉市"
|
54
|
+
c.zip_code # => "735000"
|
55
|
+
c.pinyin # => "jiuquan"
|
56
|
+
c.pinyin_abbr # => "jq"
|
57
|
+
c.districts.map(&:name) # => ["敦煌市", "玉门市", "阿克塞哈萨克族自治县", "肃北蒙古族自治县", "安西县", ...]
|
58
|
+
c.brothers.map(&:name) # => ["甘南藏族自治州", "临夏回族自治州", "陇南市", ...]
|
59
|
+
|
60
|
+
#### View
|
61
|
+
|
62
|
+
<%= form_for(@post) do |f| %>
|
63
|
+
<div class="field">
|
64
|
+
<%= f.label :province, '选择地区:' %><br />
|
65
|
+
|
66
|
+
# FormBuilder
|
67
|
+
# <%= f.region_select :city %>
|
68
|
+
# <%= f.region_select [:province, :city, :district] %>
|
69
|
+
# <%= f.region_select [:province, :city] %>
|
70
|
+
# <%= f.region_select [:city, :district] %>
|
71
|
+
# <%= f.region_select [:province, :district] %>
|
72
|
+
|
73
|
+
# FormHelper
|
74
|
+
# <%= region_select :post, :province %>
|
75
|
+
# <%= region_select :post, [:province, :city, :district] %>
|
76
|
+
# ...
|
77
|
+
|
78
|
+
</div>
|
79
|
+
<% end %>
|
80
|
+
|
81
|
+
Online example: [医院之家](http://www.yihub.com/ "医院").
|
82
|
+
|
83
|
+
## Contributing
|
84
|
+
|
85
|
+
1. Fork it
|
86
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
87
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
88
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
89
|
+
5. Create new Pull Request
|
90
|
+
|
91
|
+
## License
|
92
|
+
|
93
|
+
ChinaRegionFu is released under the MIT license.
|
94
|
+
|
data/Rakefile
CHANGED
@@ -0,0 +1,22 @@
|
|
1
|
+
module ChinaRegionFu
|
2
|
+
class FetchOptionsController < ::ActionController::Metal
|
3
|
+
|
4
|
+
def index
|
5
|
+
if params_valid?(params) and parent_klass = params[:parent_klass].classify.safe_constantize.find(params[:parent_id])
|
6
|
+
table_name = params[:klass].tableize
|
7
|
+
regions = parent_klass.__send__(table_name).select("#{table_name}.id, #{table_name}.name")
|
8
|
+
self.response_body = regions.to_json
|
9
|
+
else
|
10
|
+
self.response_body = [].to_json
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def params_valid?(params)
|
18
|
+
params[:klass].present? and params[:parent_klass] =~ /^province|city$/i and params[:parent_id].present?
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
data/app/models/city.rb
CHANGED
@@ -3,8 +3,7 @@ class City < ActiveRecord::Base
|
|
3
3
|
attr_accessible :name, :province_id, :level, :zip_code, :pinyin, :pinyin_abbr
|
4
4
|
|
5
5
|
belongs_to :province
|
6
|
-
has_many :districts, :
|
7
|
-
has_many :hospitals, :dependent => :nullify
|
6
|
+
has_many :districts, dependent: :destroy
|
8
7
|
|
9
8
|
def short_name
|
10
9
|
@short_name ||= name.gsub(/市|自治州|地区|特别行政区/,'')
|
data/app/models/district.rb
CHANGED
data/app/models/province.rb
CHANGED
@@ -2,7 +2,6 @@
|
|
2
2
|
class Province < ActiveRecord::Base
|
3
3
|
attr_accessible :name, :pinyin, :pinyin_abbr
|
4
4
|
|
5
|
-
has_many :cities, :
|
6
|
-
has_many :districts, :
|
7
|
-
has_many :hospitals, :dependent => :nullify
|
5
|
+
has_many :cities, dependent: :destroy
|
6
|
+
has_many :districts, through: :cities
|
8
7
|
end
|
data/china_region_fu.gemspec
CHANGED
@@ -1,20 +1,17 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
|
3
|
-
require "china_region_fu/version"
|
2
|
+
require File.expand_path('../lib/china_region_fu/version', __FILE__)
|
4
3
|
|
5
|
-
Gem::Specification.new do |
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
s.summary = %q{china region}
|
12
|
-
s.description = %q{china region}
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Xuhao"]
|
6
|
+
gem.email = ["xuhao@rubyfans.com"]
|
7
|
+
gem.description = %q{China region Ruby on rails interface}
|
8
|
+
gem.summary = %q{China region Ruby on rails interface}
|
9
|
+
gem.homepage = "http://rubyfans.com"
|
13
10
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "china_region_fu"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = ChinaRegionFu::VERSION
|
20
17
|
end
|
data/config/routes.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
module ChinaRegionFu
|
3
|
+
module Helper
|
4
|
+
|
5
|
+
module FormHelper
|
6
|
+
def region_select(object, methods, options = {}, html_options = {})
|
7
|
+
html_options[:class] ? (html_options[:class].prepend('region_select ')) : (html_options[:class] = 'region_select')
|
8
|
+
|
9
|
+
output = ''
|
10
|
+
if Array === methods
|
11
|
+
methods.each_with_index do |method, index|
|
12
|
+
if klass = method.to_s.classify.safe_constantize
|
13
|
+
choices = index == 0 ? klass.all.collect {|p| [ p.name, p.id ] } : []
|
14
|
+
next_method = methods.at(index + 1)
|
15
|
+
options[:prompt] = region_prompt(klass)
|
16
|
+
html_options[:data] ? (html_options[:data][:region_klass] = "#{method.to_s}") : (html_options[:data] = { region_klass: "#{method.to_s}" })
|
17
|
+
if next_method
|
18
|
+
html_options[:data].merge!(region_target: "#{object}_#{next_method.to_s}_id", region_target_kalss: next_method.to_s)
|
19
|
+
else
|
20
|
+
html_options[:data].delete(:region_target)
|
21
|
+
html_options[:data].delete(:region_target_kalss)
|
22
|
+
end
|
23
|
+
|
24
|
+
output << select(object, "#{method.to_s}_id", choices, options = options, html_options = html_options)
|
25
|
+
else
|
26
|
+
raise "Method '#{method}' is not a vaild attribute of #{object}"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
else
|
30
|
+
if klass = methods.to_s.classify.safe_constantize
|
31
|
+
options[:prompt] = region_prompt(klass)
|
32
|
+
output << select(object, methods, klass.all.collect {|p| [ p.name, p.id ] }, options = options, html_options = html_options)
|
33
|
+
else
|
34
|
+
raise "Method '#{method}' is not a vaild attribute of #{object}"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
output << javascript_tag(%~
|
38
|
+
$(function(){
|
39
|
+
$('body').on('change', '.region_select', function(event) {
|
40
|
+
var self, target, targetDom;
|
41
|
+
self = $(event.currentTarget);
|
42
|
+
target = self.data('region-target');
|
43
|
+
targetDom = $('#' + target);
|
44
|
+
if (targetDom.size() > 0) {
|
45
|
+
$.getJSON('/china_region_fu/fetch_options', {klass: self.data('region-target-kalss'), parent_klass: self.data('region-klass'), parent_id: self.val()}, function(data) {
|
46
|
+
$('option[value!=""]', targetDom).remove();
|
47
|
+
$.each(data, function(index, value) {
|
48
|
+
targetDom.append("<option value='" + value.id + "'>" + value.name + "</option>");
|
49
|
+
});
|
50
|
+
})
|
51
|
+
}
|
52
|
+
});
|
53
|
+
})
|
54
|
+
~)
|
55
|
+
output.html_safe
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def region_prompt(klass)
|
62
|
+
human_name = klass.model_name.human
|
63
|
+
"请选择#{human_name}"
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
|
69
|
+
module FormBuilder
|
70
|
+
def region_select(methods, options = {}, html_options = {})
|
71
|
+
@template.region_select(@object_name, methods, options = options, html_options = html_options)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
ActionView::Base.send :include, ChinaRegionFu::Helper::FormHelper
|
79
|
+
ActionView::Helpers::FormBuilder.send :include, ChinaRegionFu::Helper::FormBuilder
|
data/lib/china_region_fu.rb
CHANGED
@@ -1,45 +1,48 @@
|
|
1
1
|
class CreateChinaRegionTables < ActiveRecord::Migration
|
2
|
-
def
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
2
|
+
def change
|
3
|
+
unless table_exists? 'provinces'
|
4
|
+
create_table :provinces do |t|
|
5
|
+
t.string :name
|
6
|
+
t.string :pinyin
|
7
|
+
t.string :pinyin_abbr
|
8
|
+
t.timestamps
|
9
|
+
end
|
10
|
+
|
11
|
+
add_index :provinces, :name
|
12
|
+
add_index :provinces, :pinyin
|
13
|
+
add_index :provinces, :pinyin_abbr
|
8
14
|
end
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
15
|
+
|
16
|
+
unless table_exists? 'cities'
|
17
|
+
create_table :cities do |t|
|
18
|
+
t.string :name
|
19
|
+
t.integer :province_id
|
20
|
+
t.integer :level
|
21
|
+
t.string :zip_code
|
22
|
+
t.string :pinyin
|
23
|
+
t.string :pinyin_abbr
|
24
|
+
t.timestamps
|
25
|
+
end
|
26
|
+
add_index :cities, :name
|
27
|
+
add_index :cities, :province_id
|
28
|
+
add_index :cities, :level
|
29
|
+
add_index :cities, :zip_code
|
30
|
+
add_index :cities, :pinyin
|
31
|
+
add_index :cities, :pinyin_abbr
|
32
|
+
end
|
33
|
+
|
34
|
+
unless table_exists? 'districts'
|
35
|
+
create_table :districts do |t|
|
36
|
+
t.string :name
|
37
|
+
t.integer :city_id
|
38
|
+
t.string :pinyin
|
39
|
+
t.string :pinyin_abbr
|
40
|
+
t.timestamps
|
41
|
+
end
|
42
|
+
add_index :districts, :name
|
43
|
+
add_index :districts, :city_id
|
44
|
+
add_index :districts, :pinyin
|
45
|
+
add_index :districts, :pinyin_abbr
|
24
46
|
end
|
25
|
-
add_index :districts, :name
|
26
|
-
add_index :districts, :city_id
|
27
|
-
add_index :districts, :pinyin
|
28
|
-
add_index :districts, :pinyin_abbr
|
29
|
-
add_index :cities, :name
|
30
|
-
add_index :cities, :province_id
|
31
|
-
add_index :cities, :level
|
32
|
-
add_index :cities, :zip_code
|
33
|
-
add_index :cities, :pinyin
|
34
|
-
add_index :cities, :pinyin_abbr
|
35
|
-
add_index :provinces, :name
|
36
|
-
add_index :provinces, :pinyin
|
37
|
-
add_index :provinces, :pinyin_abbr
|
38
|
-
end
|
39
|
-
|
40
|
-
|
41
|
-
def self.down
|
42
|
-
drop_table :provinces, :cities, :districts
|
43
47
|
end
|
44
|
-
|
45
|
-
end
|
48
|
+
end
|
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: china_region_fu
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
|
-
-
|
8
|
+
- Xuhao
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-06-
|
12
|
+
date: 2012-06-13 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
|
-
description:
|
14
|
+
description: China region Ruby on rails interface
|
15
15
|
email:
|
16
16
|
- xuhao@rubyfans.com
|
17
17
|
executables: []
|
@@ -20,14 +20,18 @@ extra_rdoc_files: []
|
|
20
20
|
files:
|
21
21
|
- .gitignore
|
22
22
|
- Gemfile
|
23
|
-
-
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
24
25
|
- Rakefile
|
26
|
+
- app/controllers/china_region_fu/fetch_options_controller.rb
|
25
27
|
- app/models/city.rb
|
26
28
|
- app/models/district.rb
|
27
29
|
- app/models/province.rb
|
28
30
|
- china_region_fu.gemspec
|
31
|
+
- config/routes.rb
|
29
32
|
- lib/china_region_fu.rb
|
30
33
|
- lib/china_region_fu/engine.rb
|
34
|
+
- lib/china_region_fu/helper.rb
|
31
35
|
- lib/china_region_fu/version.rb
|
32
36
|
- lib/generators/china_region_fu/install/USAGE
|
33
37
|
- lib/generators/china_region_fu/install/install_generator.rb
|
@@ -36,7 +40,7 @@ files:
|
|
36
40
|
- lib/generators/china_region_fu/mvc/USAGE
|
37
41
|
- lib/generators/china_region_fu/mvc/mvc_generator.rb
|
38
42
|
- lib/tasks/region.rake
|
39
|
-
homepage:
|
43
|
+
homepage: http://rubyfans.com
|
40
44
|
licenses: []
|
41
45
|
post_install_message:
|
42
46
|
rdoc_options: []
|
@@ -55,9 +59,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
55
59
|
- !ruby/object:Gem::Version
|
56
60
|
version: '0'
|
57
61
|
requirements: []
|
58
|
-
rubyforge_project:
|
62
|
+
rubyforge_project:
|
59
63
|
rubygems_version: 1.8.24
|
60
64
|
signing_key:
|
61
65
|
specification_version: 3
|
62
|
-
summary:
|
66
|
+
summary: China region Ruby on rails interface
|
63
67
|
test_files: []
|
data/README.rdoc
DELETED
@@ -1,62 +0,0 @@
|
|
1
|
-
= ChinaRegionFu
|
2
|
-
|
3
|
-
ChinaRegionFu include the region data of china,and region CURD actions.Got ChinaRegionFu it means you got
|
4
|
-
a powerfull region controller system about china.
|
5
|
-
|
6
|
-
|
7
|
-
== Getting Started
|
8
|
-
|
9
|
-
1. Put 'gem china_region_fu' to your Gemfile:
|
10
|
-
|
11
|
-
gem china_region_fu
|
12
|
-
|
13
|
-
2. Run bundler command to install the gem:
|
14
|
-
|
15
|
-
bundle install
|
16
|
-
|
17
|
-
3. After you install the gem, you need run the generator:
|
18
|
-
|
19
|
-
rails g china_region_fu:install
|
20
|
-
|
21
|
-
It will:
|
22
|
-
* Generate <em>db/migrate/<timestamp>create_china_region_tables.rb</em> migrate file to your app, *_china_regions_* table is used for store the regions.
|
23
|
-
* Copy regions.yml to config/ directory.
|
24
|
-
* Run "rake db:migrate".
|
25
|
-
* Run "rake region:import".
|
26
|
-
|
27
|
-
Now you have there ActiveRecord modules: <b>Province, City, District</b>.
|
28
|
-
|
29
|
-
Run with <em>rails g</em> for get generator list.
|
30
|
-
|
31
|
-
4. If you want to customize the region modules you can run the generator:
|
32
|
-
rails g china_region_fu:mvc
|
33
|
-
|
34
|
-
This will create:
|
35
|
-
|
36
|
-
create app/models/province.rb
|
37
|
-
create app/models/city.rb
|
38
|
-
create app/models/district.rb
|
39
|
-
|
40
|
-
So you can do what you want to do in this files.
|
41
|
-
|
42
|
-
5. Examples
|
43
|
-
|
44
|
-
a = Province.last
|
45
|
-
a.name # => "台湾省"
|
46
|
-
a.cities.map(&:name) # => ["嘉义市", "台南市", "新竹市", "台中市", "基隆市", "台北市"]
|
47
|
-
|
48
|
-
Province.first.districts.map(&:name) # => ["延庆县", "密云县", "平谷区", ...]
|
49
|
-
|
50
|
-
c = City.last
|
51
|
-
c.name # => "酒泉市"
|
52
|
-
c.districts.map(&:name) # => ["敦煌市", "玉门市", "阿克塞哈萨克族自治县", "肃北蒙古族自治县", "安西县", ...]
|
53
|
-
c.brothers.map(&:name) # => ["甘南藏族自治州", "临夏回族自治州", "陇南市", ...]
|
54
|
-
|
55
|
-
== Contributing
|
56
|
-
|
57
|
-
Xuhao <{rubyfans.com}[http://www.rubyfans.com]>.
|
58
|
-
|
59
|
-
== License
|
60
|
-
|
61
|
-
ChinaRegionFu is released under the MIT license.
|
62
|
-
|