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.
@@ -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
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
data/Gemfile CHANGED
@@ -1,4 +1,5 @@
1
- source 'https://rubygems.org'
1
+ # source 'https://rubygems.org'
2
+ source 'http://ruby.taobao.org'
2
3
 
3
4
  # Specify your gem's dependencies in lean.gemspec
4
5
  gemspec
data/README.md CHANGED
@@ -1,7 +1,9 @@
1
1
  # ChinaRegionFu
2
2
 
3
- ChinaRegionFu provide the region data of china.You will got complete chian region data after use this gem.
4
-
3
+ [![Gem Version](https://badge.fury.io/rb/china_region_fu.png)](http://badge.fury.io/rb/china_region_fu)
4
+ [![Build Status](https://travis-ci.org/Xuhao/china_region_fu.png?branch=master)](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, `provinces`, `cities`, `districts` table is used for store the regions.
22
- * Copy regions.yml to config/ directory.
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
- Run with `rails g` for get generator list.
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:mvc
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
- 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
-
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
- <%= 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
- ##### prompt
82
-
83
- You need define `province_select_prompt`, `city_select_prompt`, `district_select_prompt` helpers for each select prompt.
84
- If you have not define these helpers, it will use the default one like:
85
-
86
- def region_prompt(region_klass)
87
- human_name = region_klass.model_name.human
88
- "请选择#{human_name}"
89
- end
90
-
91
- Online example: [医院之家](http://www.yihub.com/ "医院").
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,2 +1,6 @@
1
- #!/usr/bin/env rake
2
- require 'bundler/gem_tasks'
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -1,31 +1,37 @@
1
1
  module ChinaRegionFu
2
2
  class FetchOptionsController < ::ActionController::Metal
3
-
3
+
4
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
- 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
- def has_level_column?(klass_name)
23
- klass_name.classify.safe_constantize.try(:column_names).to_a.include?('level')
24
- end
25
-
26
- def params_valid?(params)
27
- params[:klass].present? and params[:parent_klass] =~ /^province|city$/i and params[:parent_id].present?
28
- end
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
@@ -1,7 +1,9 @@
1
- # -*- encoding: utf-8 -*-
1
+ # coding: utf-8
2
2
  class City < ActiveRecord::Base
3
- attr_accessible :name, :province_id, :level, :zip_code, :pinyin, :pinyin_abbr
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
 
@@ -1,16 +1,18 @@
1
- # -*- encoding: utf-8 -*-
1
+ # coding: utf-8
2
2
  class District < ActiveRecord::Base
3
- attr_accessible :name, :city_id, :pinyin, :pinyin_abbr
4
-
5
- belongs_to :city
6
- belongs_to :province
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
@@ -1,7 +1,8 @@
1
- # -*- encoding: utf-8 -*-
2
1
  class Province < ActiveRecord::Base
3
- attr_accessible :name, :pinyin, :pinyin_abbr
4
-
5
- has_many :cities, dependent: :destroy
6
- has_many :districts, through: :cities
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
@@ -1,19 +1,26 @@
1
- # -*- encoding: utf-8 -*-
2
- require File.expand_path('../lib/china_region_fu/version', __FILE__)
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 |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"
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
- 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
17
-
18
- gem.add_dependency 'jquery-rails'
19
- end
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
@@ -1,3 +1,3 @@
1
1
  Rails.application.routes.draw do
2
- match '/china_region_fu/fetch_options', to: ChinaRegionFu::FetchOptionsController.action(:index)
3
- end
2
+ get '/china_region_fu/fetch_options', to: ChinaRegionFu::FetchOptionsController.action(:index)
3
+ end
@@ -2,5 +2,3 @@ require "china_region_fu/version"
2
2
  require 'china_region_fu/engine' if defined? Rails
3
3
 
4
4
  module ChinaRegionFu;end
5
-
6
- require 'china_region_fu/helper'
@@ -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;end
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
@@ -0,0 +1,7 @@
1
+ module ChinaRegionFu
2
+ class InvalidAttributeError < StandardError
3
+ def initialize
4
+ super 'Region attribute is not valid.'
5
+ end
6
+ end
7
+ end