china_regions 0.0.5 → 0.1.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 +7 -0
- data/Gemfile +2 -2
- data/README.md +7 -0
- data/app/controllers/china_regions/fetch_options_controller.rb +5 -7
- data/app/models/city.rb +1 -4
- data/app/models/district.rb +13 -16
- data/app/models/province.rb +1 -5
- data/china_regions.gemspec +4 -4
- data/config/routes.rb +2 -2
- data/lib/china_regions/engine.rb +2 -2
- data/lib/china_regions/helpers/form_helper.rb +33 -27
- data/lib/china_regions/version.rb +1 -1
- data/lib/generators/china_regions/install_generator.rb +10 -4
- data/lib/generators/china_regions/templates/migration.rb +4 -4
- data/lib/tasks/china_regions.rake +40 -17
- metadata +14 -15
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 42ebe2926674e82aeebf73c5111a62a20e182eda
|
4
|
+
data.tar.gz: d7ef3319973190e74f2e17d70a656db38d22a133
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 389e838506bd0821046b6879a11fe63fa8d996afb8f32c03fec9347d8aba079e48d2f3e30f4aeb4e99366e18751e717c5d82cc2cd8207c3a822c83b6dfb2cdcb
|
7
|
+
data.tar.gz: a2924b8c593fbb8a9f6044d29dc84b8b8b5d091daf3e77a1e33fbce5f490633d4dd7dc500d8a29a0146ffb573a190ed3260a1581347a79f5a87fa9e210daf420
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -59,6 +59,13 @@ bundle install
|
|
59
59
|
= region_select :article, :district
|
60
60
|
|
61
61
|
= f.submit class: 'btn'
|
62
|
+
|
63
|
+
添加前缀名:
|
64
|
+
|
65
|
+
= form_for @article do |f|
|
66
|
+
|
67
|
+
= f.region_select [:province, :city, :district], :prefix => "home"
|
68
|
+
= f.region_select [:province, :city, :district], :prefix => "work"
|
62
69
|
|
63
70
|
|
64
71
|
## Contributing
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module ChinaRegions
|
2
2
|
class FetchOptionsController < ::ActionController::Metal
|
3
|
-
|
3
|
+
|
4
4
|
def index
|
5
5
|
if params_valid?(params) and parent_klass = params[:parent_klass].classify.safe_constantize.find(params[:parent_id])
|
6
6
|
table_name = params[:klass].tableize
|
@@ -15,16 +15,14 @@ module ChinaRegions
|
|
15
15
|
self.response_body = [].to_json
|
16
16
|
end
|
17
17
|
end
|
18
|
-
|
19
|
-
|
20
|
-
protected
|
18
|
+
|
19
|
+
protected
|
21
20
|
def has_level_column?(klass_name)
|
22
21
|
klass_name.classify.safe_constantize.try(:column_names).to_a.include?('level')
|
23
22
|
end
|
24
|
-
|
23
|
+
|
25
24
|
def params_valid?(params)
|
26
25
|
params[:klass].present? and params[:parent_klass] =~ /^province|city$/i and params[:parent_id].present?
|
27
26
|
end
|
28
|
-
|
29
27
|
end
|
30
|
-
end
|
28
|
+
end
|
data/app/models/city.rb
CHANGED
@@ -1,9 +1,6 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
class City < ActiveRecord::Base
|
4
|
-
|
5
|
-
attr_accessible :name, :province_id, :level, :zip_code, :name_en, :name_abbr
|
6
|
-
|
7
4
|
belongs_to :province
|
8
5
|
has_many :districts, dependent: :destroy
|
9
6
|
|
@@ -14,7 +11,7 @@ class City < ActiveRecord::Base
|
|
14
11
|
end
|
15
12
|
|
16
13
|
def siblings
|
17
|
-
@siblings ||=
|
14
|
+
@siblings ||= where(nil).with_province(self.province_id)
|
18
15
|
end
|
19
16
|
|
20
17
|
end
|
data/app/models/district.rb
CHANGED
@@ -1,23 +1,20 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
class District < ActiveRecord::Base
|
4
|
-
|
5
|
-
attr_accessible :name, :city_id, :name_en, :name_abbr
|
6
|
-
|
7
|
-
belongs_to :city
|
4
|
+
belongs_to :city
|
8
5
|
|
9
|
-
|
6
|
+
scope :with_city, ->(city) { where(city_id: city) }
|
10
7
|
|
11
|
-
|
12
|
-
|
13
|
-
|
8
|
+
def province
|
9
|
+
city.province
|
10
|
+
end
|
11
|
+
|
12
|
+
def short_name
|
13
|
+
@short_name ||= name.gsub(/区|县|市|自治县/, '')
|
14
|
+
end
|
15
|
+
|
16
|
+
def siblings
|
17
|
+
@siblings ||= where(nil).with_city(self.city_id)
|
18
|
+
end
|
14
19
|
|
15
|
-
def short_name
|
16
|
-
@short_name ||= name.gsub(/区|县|市|自治县/, '')
|
17
|
-
end
|
18
|
-
|
19
|
-
def siblings
|
20
|
-
@siblings ||= scoped.with_city(self.city_id)
|
21
|
-
end
|
22
|
-
|
23
20
|
end
|
data/app/models/province.rb
CHANGED
data/china_regions.gemspec
CHANGED
@@ -2,10 +2,10 @@
|
|
2
2
|
require File.expand_path('../lib/china_regions/version', __FILE__)
|
3
3
|
|
4
4
|
Gem::Specification.new do |gem|
|
5
|
-
gem.authors = ["Encore Shao"]
|
6
|
-
gem.email = ["encore.shao@gmail.com"]
|
5
|
+
gem.authors = ["Encore Shao","Trey Springer"]
|
6
|
+
gem.email = ["encore.shao@gmail.com","dspringer@enova.com"]
|
7
7
|
gem.description = %q{China regions Ruby on rails interface}
|
8
|
-
gem.summary = %q{
|
8
|
+
gem.summary = %q{Rails 4 version of dropdowns for all provinces, cities, and districts in China.}
|
9
9
|
gem.homepage = "http://github.com/encoreshao"
|
10
10
|
|
11
11
|
gem.files = `git ls-files`.split($\)
|
@@ -14,6 +14,6 @@ Gem::Specification.new do |gem|
|
|
14
14
|
gem.name = "china_regions"
|
15
15
|
gem.require_paths = ["lib"]
|
16
16
|
gem.version = ChinaRegions::VERSION
|
17
|
-
|
17
|
+
|
18
18
|
gem.add_dependency 'jquery-rails'
|
19
19
|
end
|
data/config/routes.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
Rails.application.routes.draw do
|
2
|
-
match '/china_regions/fetch_options'
|
3
|
-
end
|
2
|
+
match '/china_regions/fetch_options' => ChinaRegions::FetchOptionsController.action(:index), via: [:get]
|
3
|
+
end
|
data/lib/china_regions/engine.rb
CHANGED
@@ -3,24 +3,25 @@
|
|
3
3
|
module ChinaRegions
|
4
4
|
module Helpers
|
5
5
|
module FormHelper
|
6
|
-
|
7
6
|
def region_select(object, methods, options = {}, html_options = {})
|
8
7
|
output = ''
|
9
8
|
|
10
9
|
html_options[:class] ?
|
11
10
|
(html_options[:class].prepend('region_select ')) :
|
12
11
|
(html_options[:class] = 'region_select')
|
13
|
-
|
12
|
+
|
13
|
+
dropdown_prefix = options[:prefix].to_s + "_" || ""
|
14
|
+
|
14
15
|
if Array === methods
|
15
16
|
methods.each_with_index do |method, index|
|
16
17
|
if region_klass = method.to_s.classify.safe_constantize
|
17
|
-
choices = (index == 0 ? region_klass.
|
18
|
+
choices = (index == 0 ? region_klass.where(nil).collect {|p| [ p.name, p.id ] } : [])
|
18
19
|
next_method = methods.at(index + 1)
|
19
|
-
|
20
|
+
|
20
21
|
set_options(method, options, region_klass)
|
21
|
-
set_html_options(object, method, html_options, next_method)
|
22
|
-
|
23
|
-
output << select(object, "#{method.to_s}_id", choices, options = options, html_options = html_options)
|
22
|
+
set_html_options(object, method, html_options, next_method, dropdown_prefix)
|
23
|
+
|
24
|
+
output << select(object, "#{dropdown_prefix}#{method.to_s}_id", choices, options = options, html_options = html_options)
|
24
25
|
else
|
25
26
|
raise "Method '#{method}' is not a vaild attribute of #{object}"
|
26
27
|
end
|
@@ -37,20 +38,20 @@ module ChinaRegions
|
|
37
38
|
|
38
39
|
if region_klass = methods.to_s.classify.safe_constantize
|
39
40
|
options[:prompt] = region_prompt(region_klass)
|
40
|
-
|
41
|
-
output << select(object, _methods, region_klass.
|
41
|
+
|
42
|
+
output << select(object, _methods, region_klass.where(nil).collect {|p| [ p.name, p.id ] }, options = options, html_options = html_options)
|
42
43
|
else
|
43
44
|
raise "Method '#{method}' is not a vaild attribute of #{object}"
|
44
45
|
end
|
45
46
|
end
|
46
|
-
|
47
|
+
|
47
48
|
output << javascript_tag(js_output)
|
48
49
|
output.html_safe
|
49
50
|
end
|
50
|
-
|
51
|
-
|
51
|
+
|
52
|
+
|
52
53
|
private
|
53
|
-
|
54
|
+
|
54
55
|
def set_options(method, options, region_klass)
|
55
56
|
if respond_to?("#{method}_select_prompt")
|
56
57
|
options[:prompt] = __send__("#{method}_select_prompt")
|
@@ -58,44 +59,49 @@ module ChinaRegions
|
|
58
59
|
options[:prompt] = region_prompt(region_klass)
|
59
60
|
end
|
60
61
|
end
|
61
|
-
|
62
|
-
def set_html_options(object, method, html_options, next_region)
|
62
|
+
|
63
|
+
def set_html_options(object, method, html_options, next_region, prefix)
|
63
64
|
html_options[:data] ? (html_options[:data][:region_klass] = "#{method.to_s}") : (html_options[:data] = { region_klass: "#{method.to_s}" })
|
64
65
|
if next_region
|
65
|
-
html_options[:data].merge!(region_target: "#{object}_#{next_region.to_s}_id",
|
66
|
+
html_options[:data].merge!(region_target: "#{object}_#{prefix}#{next_region.to_s}_id", region_target_klass: next_region.to_s)
|
66
67
|
else
|
67
68
|
html_options[:data].delete(:region_target)
|
68
|
-
html_options[:data].delete(:
|
69
|
+
html_options[:data].delete(:region_target_klass)
|
69
70
|
end
|
70
71
|
end
|
71
|
-
|
72
|
+
|
72
73
|
def region_prompt(region_klass)
|
73
74
|
t('views.select', model: region_klass.model_name.human)
|
74
75
|
end
|
75
|
-
|
76
|
+
|
76
77
|
def js_output
|
77
78
|
%~
|
78
79
|
$(function(){
|
79
80
|
$('body').on('change', '.region_select', function(event) {
|
80
|
-
var
|
81
|
-
|
82
|
-
targetDom = $('#' +
|
81
|
+
var changedObj, targetDom;
|
82
|
+
changedObj = $(event.currentTarget);
|
83
|
+
targetDom = $('#' + changedObj.data('region-target'));
|
83
84
|
if (targetDom.size() > 0) {
|
84
|
-
$.getJSON('/china_regions/fetch_options', {klass:
|
85
|
+
$.getJSON('/china_regions/fetch_options', {klass: changedObj.data('region-target-klass'), parent_klass: changedObj.data('region-klass'), parent_id: changedObj.val()}, function(data) {
|
85
86
|
$('option[value!=""]', targetDom).remove();
|
86
87
|
$.each(data, function(index, value) {
|
87
88
|
targetDom.append("<option value='" + value.id + "'>" + value.name + "</option>");
|
88
89
|
});
|
89
90
|
})
|
91
|
+
// just clear out the second dropdown if it exists, they should start from the beginning
|
92
|
+
secondTargetDom = $('#' + targetDom.data('region-target'));
|
93
|
+
if (secondTargetDom.size() > 0) {
|
94
|
+
$('option[value!=""]', secondTargetDom).remove();
|
95
|
+
}
|
90
96
|
}
|
91
97
|
});
|
92
98
|
});
|
93
99
|
~
|
94
100
|
end
|
95
|
-
|
101
|
+
|
96
102
|
end
|
97
|
-
|
98
|
-
|
103
|
+
|
104
|
+
|
99
105
|
module FormBuilder
|
100
106
|
def region_select(methods, options = {}, html_options = {})
|
101
107
|
@template.region_select(@object_name, methods, options = options, html_options = html_options)
|
@@ -106,4 +112,4 @@ module ChinaRegions
|
|
106
112
|
end
|
107
113
|
|
108
114
|
ActionView::Base.send :include, ChinaRegions::Helpers::FormHelper
|
109
|
-
ActionView::Helpers::FormBuilder.send :include, ChinaRegions::Helpers::FormBuilder
|
115
|
+
ActionView::Helpers::FormBuilder.send :include, ChinaRegions::Helpers::FormBuilder
|
@@ -6,14 +6,14 @@ module ChinaRegions
|
|
6
6
|
source_root File.expand_path('../templates', __FILE__)
|
7
7
|
|
8
8
|
def copy_migration
|
9
|
-
|
9
|
+
Dir["db/migrate/*_china_regions_tables.rb"].each{ |file| File.delete(file) }
|
10
10
|
migration_template "migration.rb", "db/migrate/create_china_regions_tables.rb"
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
def copy_cities
|
14
14
|
copy_file('cities.yml', 'config/cities.yml') unless File::exists?("config/cities.yml")
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
def copy_locales
|
18
18
|
unless File::exists?("config/locales/regions.en.yml")
|
19
19
|
copy_file "../../../../config/locales/en.yml", "config/locales/regions.en.yml"
|
@@ -23,10 +23,16 @@ module ChinaRegions
|
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
|
+
def copy_rake_tasks
|
27
|
+
unless File::exists?("lib/tasks/china_regions.rake")
|
28
|
+
copy_file "../../../../lib/tasks/china_regions.rake", "lib/tasks/china_regions.rake"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
26
32
|
def execute_migrate
|
27
33
|
rake("db:migrate")
|
28
34
|
end
|
29
|
-
|
35
|
+
|
30
36
|
def import_cities_to_database
|
31
37
|
rake('china_regions:import')
|
32
38
|
end
|
@@ -9,12 +9,12 @@ class CreateChinaRegionsTables < ActiveRecord::Migration
|
|
9
9
|
t.string :name_abbr
|
10
10
|
t.timestamps
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
add_index :provinces, :name
|
14
14
|
add_index :provinces, :name_en
|
15
15
|
add_index :provinces, :name_abbr
|
16
16
|
end
|
17
|
-
|
17
|
+
|
18
18
|
unless table_exists? 'cities'
|
19
19
|
create_table :cities do |t|
|
20
20
|
t.string :name
|
@@ -32,7 +32,7 @@ class CreateChinaRegionsTables < ActiveRecord::Migration
|
|
32
32
|
add_index :cities, :name_en
|
33
33
|
add_index :cities, :name_abbr
|
34
34
|
end
|
35
|
-
|
35
|
+
|
36
36
|
unless table_exists? 'districts'
|
37
37
|
create_table :districts do |t|
|
38
38
|
t.string :name
|
@@ -47,4 +47,4 @@ class CreateChinaRegionsTables < ActiveRecord::Migration
|
|
47
47
|
add_index :districts, :name_abbr
|
48
48
|
end
|
49
49
|
end
|
50
|
-
end
|
50
|
+
end
|
@@ -8,39 +8,62 @@ namespace :china_regions do
|
|
8
8
|
task :import => :environment do
|
9
9
|
file_path = File.join(Rails.root, 'config', 'cities.yml')
|
10
10
|
data = File.open(file_path) { |file| YAML.load(file) }
|
11
|
-
|
12
|
-
puts "
|
11
|
+
remove_china_regions && load_to_db(data)
|
12
|
+
puts "\n China's provinces, city, region data import is complete."
|
13
13
|
end
|
14
|
-
|
15
|
-
def
|
14
|
+
|
15
|
+
def remove_china_regions
|
16
16
|
Province.delete_all && City.delete_all && District.delete_all
|
17
17
|
end
|
18
18
|
|
19
19
|
def load_to_db(data)
|
20
20
|
data.each do |province_name, province_hash|
|
21
|
-
|
21
|
+
province_parameters = province_params({
|
22
22
|
name: province_name,
|
23
23
|
name_en: province_hash['name_en'],
|
24
24
|
name_abbr: province_hash['name_abbr']
|
25
25
|
})
|
26
|
+
province = Province.create(province_parameters)
|
27
|
+
|
26
28
|
province_hash['cities'].each do |city_name, city_hash|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
29
|
+
city_parameters = city_params({
|
30
|
+
province_id: province.id,
|
31
|
+
name: city_name,
|
32
|
+
name_en: city_hash['name_en'],
|
33
|
+
name_abbr: city_hash['name_abbr'],
|
34
|
+
zip_code: city_hash['zip_code'],
|
35
|
+
level: city_hash['level'] || 4
|
33
36
|
})
|
34
|
-
|
37
|
+
city = City.create(city_parameters)
|
35
38
|
|
39
|
+
districts_hash = city_hash['districts']
|
36
40
|
districts_hash.each do |district_name, district_hash|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
+
district_parameters = district_params({
|
42
|
+
city_id: city.id,
|
43
|
+
name: district_name,
|
44
|
+
name_en: district_hash['name_en'],
|
45
|
+
name_abbr: district_hash['name_abbr']
|
41
46
|
})
|
47
|
+
District.create(district_parameters)
|
42
48
|
end
|
43
49
|
end
|
50
|
+
print "."
|
44
51
|
end
|
45
52
|
end
|
46
|
-
|
53
|
+
|
54
|
+
private
|
55
|
+
def province_params(raw_parameters)
|
56
|
+
parameters = ActionController::Parameters.new(raw_parameters)
|
57
|
+
parameters.permit(:name, :name_en, :name_abbr)
|
58
|
+
end
|
59
|
+
|
60
|
+
def city_params(raw_parameters)
|
61
|
+
parameters = ActionController::Parameters.new(raw_parameters)
|
62
|
+
parameters.permit(:province_id, :name, :name_en, :name_abbr, :zip_code, :level)
|
63
|
+
end
|
64
|
+
|
65
|
+
def district_params(raw_parameters)
|
66
|
+
parameters = ActionController::Parameters.new(raw_parameters)
|
67
|
+
parameters.permit(:city_id, :name, :name_en, :name_abbr)
|
68
|
+
end
|
69
|
+
end
|
metadata
CHANGED
@@ -1,40 +1,39 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: china_regions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Encore Shao
|
8
|
+
- Trey Springer
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2014-04-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: jquery-rails
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
17
|
requirements:
|
19
|
-
- -
|
18
|
+
- - ">="
|
20
19
|
- !ruby/object:Gem::Version
|
21
20
|
version: '0'
|
22
21
|
type: :runtime
|
23
22
|
prerelease: false
|
24
23
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
24
|
requirements:
|
27
|
-
- -
|
25
|
+
- - ">="
|
28
26
|
- !ruby/object:Gem::Version
|
29
27
|
version: '0'
|
30
28
|
description: China regions Ruby on rails interface
|
31
29
|
email:
|
32
30
|
- encore.shao@gmail.com
|
31
|
+
- dspringer@enova.com
|
33
32
|
executables: []
|
34
33
|
extensions: []
|
35
34
|
extra_rdoc_files: []
|
36
35
|
files:
|
37
|
-
- .gitignore
|
36
|
+
- ".gitignore"
|
38
37
|
- Gemfile
|
39
38
|
- LICENSE
|
40
39
|
- README.md
|
@@ -58,26 +57,26 @@ files:
|
|
58
57
|
- lib/tasks/china_regions.rake
|
59
58
|
homepage: http://github.com/encoreshao
|
60
59
|
licenses: []
|
60
|
+
metadata: {}
|
61
61
|
post_install_message:
|
62
62
|
rdoc_options: []
|
63
63
|
require_paths:
|
64
64
|
- lib
|
65
65
|
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
-
none: false
|
67
66
|
requirements:
|
68
|
-
- -
|
67
|
+
- - ">="
|
69
68
|
- !ruby/object:Gem::Version
|
70
69
|
version: '0'
|
71
70
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
-
none: false
|
73
71
|
requirements:
|
74
|
-
- -
|
72
|
+
- - ">="
|
75
73
|
- !ruby/object:Gem::Version
|
76
74
|
version: '0'
|
77
75
|
requirements: []
|
78
76
|
rubyforge_project:
|
79
|
-
rubygems_version:
|
77
|
+
rubygems_version: 2.2.0.rc.1
|
80
78
|
signing_key:
|
81
|
-
specification_version:
|
82
|
-
summary:
|
79
|
+
specification_version: 4
|
80
|
+
summary: Rails 4 version of dropdowns for all provinces, cities, and districts in
|
81
|
+
China.
|
83
82
|
test_files: []
|