china_region_fu 0.0.6 → 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.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +12 -5
  3. data/.rspec +2 -1
  4. data/.rubocop.yml +101 -0
  5. data/.travis.yml +4 -2
  6. data/CODE_OF_CONDUCT.md +74 -0
  7. data/Gemfile +10 -4
  8. data/LICENSE.txt +21 -0
  9. data/README.en.md +212 -0
  10. data/README.md +103 -68
  11. data/Rakefile +1 -1
  12. data/app/controllers/china_region_fu/fetch_options_controller.rb +64 -28
  13. data/app/models/city.rb +1 -11
  14. data/app/models/district.rb +1 -11
  15. data/app/models/province.rb +4 -4
  16. data/bin/console +14 -0
  17. data/bin/rspec +17 -0
  18. data/bin/setup +8 -0
  19. data/china_region_fu.gemspec +20 -19
  20. data/db/migrate/20111111111111_create_china_region_tables.rb +19 -17
  21. data/lib/china_region_fu.rb +4 -3
  22. data/lib/china_region_fu/engine.rb +11 -9
  23. data/lib/china_region_fu/errors.rb +5 -0
  24. data/lib/china_region_fu/helpers/formtastic.rb +4 -6
  25. data/lib/china_region_fu/helpers/helpers.rb +44 -53
  26. data/lib/china_region_fu/helpers/simple_form.rb +15 -14
  27. data/lib/china_region_fu/helpers/utils.rb +82 -0
  28. data/lib/china_region_fu/version.rb +1 -1
  29. data/lib/generators/china_region_fu/models/USAGE +1 -1
  30. data/lib/generators/china_region_fu/models/models_generator.rb +3 -3
  31. data/lib/tasks/{region.rake → china_region_fu_tasks.rake} +8 -14
  32. data/spec/china_region_fu_spec.rb +3 -7
  33. data/spec/helpers/helpers_spec.rb +113 -0
  34. data/spec/helpers/utils_spec.rb +87 -0
  35. data/spec/spec_helper.rb +6 -1
  36. data/spec/support/models.rb +39 -0
  37. metadata +50 -61
  38. data/LICENSE +0 -22
  39. data/lib/china_region_fu/exceptions.rb +0 -7
  40. data/lib/china_region_fu/helpers/utilities.rb +0 -54
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9e28ca0436e1c02e7842fda2168254af0c1a095e
4
- data.tar.gz: a1b649576d71c22856263875df33c28c9e190df8
3
+ metadata.gz: 65a269673a48e7be55e4115d225c7b9b9b83206e
4
+ data.tar.gz: dbfb89b52ffed28f37f5fb6a234fc5f36e437f70
5
5
  SHA512:
6
- metadata.gz: ed31c16f389c36bbda4ecf00b40e9629f0b9962773c1849beba67677b48f94f16bfc8f7d16a3e9ff215354cc45c6cc75f7f968ad23dde0639f4db8ad105f0ea5
7
- data.tar.gz: 77c751addd30b359f6bea919f6cd278d6f80d888e8e472ca9a96a5d4fbc2eec655ef6ab8096d6fe24a8f2b7190ff7c674c9fa08345b78d40d4d7474f5d442f66
6
+ metadata.gz: 2a99edd958c4560bb477bd561d2aa830b339ea62b3cb7f761837ef95116ef7e2dd51d06b34c2b840709ccc3dfcb6b82f588531d3c3dc6ace88e1f2781743d49b
7
+ data.tar.gz: dbfcea798f01da81c46b5130390dbf7be3f70f9e8c9ce339ce5740dc7f571076b3aa1c10b8c59091e753f6f3e78645ea802cb7d9d8396860aee4c641df1673c6
data/.gitignore CHANGED
@@ -1,5 +1,12 @@
1
- *.gem
2
- .bundle
3
- Gemfile.lock
4
- pkg/*
5
- doc
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ .pryrc
12
+ .versions.conf
data/.rspec CHANGED
@@ -1,2 +1,3 @@
1
1
  --format documentation
2
- --color
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,101 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+ # RuboCop has a bunch of cops enabled by default. This setting tells RuboCop
4
+ # to ignore them, so only the ones explicitly set in this file are enabled.
5
+ DisabledByDefault: true
6
+ Exclude:
7
+ - '**/templates/**/*'
8
+ - '**/vendor/**/*'
9
+
10
+ # Prefer &&/|| over and/or.
11
+ Style/AndOr:
12
+ Enabled: true
13
+
14
+ Metrics/LineLength:
15
+ Enabled: false
16
+
17
+ # Do not use braces for hash literals when they are the last argument of a
18
+ # method call.
19
+ Style/BracesAroundHashParameters:
20
+ Enabled: true
21
+
22
+ # Align `when` with `case`.
23
+ Style/CaseIndentation:
24
+ Enabled: true
25
+
26
+ # Align comments with method definitions.
27
+ Style/CommentIndentation:
28
+ Enabled: true
29
+
30
+ # No extra empty lines.
31
+ Style/EmptyLines:
32
+ Enabled: true
33
+
34
+ # In a regular class definition, no empty lines around the body.
35
+ Style/EmptyLinesAroundClassBody:
36
+ Enabled: true
37
+
38
+ # In a regular module definition, no empty lines around the body.
39
+ Style/EmptyLinesAroundModuleBody:
40
+ Enabled: true
41
+
42
+ # Use Ruby >= 1.9 syntax for hashes. Prefer { a: :b } over { :a => :b }.
43
+ Style/HashSyntax:
44
+ Enabled: true
45
+
46
+ # Method definitions after `private` or `protected` isolated calls need one
47
+ # extra level of indentation.
48
+ Style/IndentationConsistency:
49
+ Enabled: true
50
+ EnforcedStyle: rails
51
+
52
+ # Two spaces, no tabs (for indentation).
53
+ Style/IndentationWidth:
54
+ Enabled: true
55
+
56
+ # Defining a method with parameters needs parentheses.
57
+ Style/MethodDefParentheses:
58
+ Enabled: true
59
+
60
+ # Use `foo {}` not `foo{}`.
61
+ Style/SpaceBeforeBlockBraces:
62
+ Enabled: true
63
+
64
+ # Use `foo { bar }` not `foo {bar}`.
65
+ Style/SpaceInsideBlockBraces:
66
+ Enabled: true
67
+
68
+ # Use `{ a: 1 }` not `{a:1}`.
69
+ Style/SpaceInsideHashLiteralBraces:
70
+ Enabled: true
71
+
72
+ # Check quotes usage according to lint rule below.
73
+ Style/StringLiterals:
74
+ Enabled: true
75
+ EnforcedStyle: single_quotes
76
+
77
+ # Detect hard tabs, no hard tabs.
78
+ Style/Tab:
79
+ Enabled: true
80
+
81
+ # Blank lines should not have any spaces.
82
+ Style/TrailingBlankLines:
83
+ Enabled: true
84
+
85
+ # No trailing whitespace.
86
+ Style/TrailingWhitespace:
87
+ Enabled: true
88
+
89
+ # Use quotes for string literals when they are enough.
90
+ Style/UnneededPercentQ:
91
+ Enabled: true
92
+
93
+ # Align `end` with the matching keyword or starting expression except for
94
+ # assignments, where it should be aligned with the LHS.
95
+ Lint/EndAlignment:
96
+ Enabled: true
97
+ AlignWith: variable
98
+
99
+ # Use my_method(my_arg) not my_method( my_arg ) or my_method my_arg.
100
+ Lint/RequireParentheses:
101
+ Enabled: true
@@ -1,4 +1,6 @@
1
+ sudo: false
1
2
  language: ruby
2
3
  rvm:
3
- - 1.9.3
4
- - 2.0.0
4
+ - 2.2.2
5
+ - 2.3.1
6
+ before_install: gem install bundler -v 1.13.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at hao.xu@vpon.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile CHANGED
@@ -1,5 +1,11 @@
1
- # source 'https://rubygems.org'
2
- source 'http://ruby.taobao.org'
1
+ source 'https://rubygems.org'
3
2
 
4
- # Specify your gem's dependencies in lean.gemspec
5
- gemspec
3
+ # Specify your gem's dependencies in china_region_fu.gemspec
4
+ gemspec
5
+
6
+ group :development, :test do
7
+ gem 'pry-byebug'
8
+ gem 'rspec', '~> 3.5'
9
+ gem 'actionview', '>= 4.0'
10
+ gem 'codacy-coverage', require: false
11
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Xuhao
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,212 @@
1
+ # [中文说明](https://github.com/Xuhao/china_region_fu/blob/master/README.zh-cn.md)
2
+
3
+ # ChinaRegionFu
4
+
5
+ [![Build Status](https://travis-ci.org/Xuhao/china_region_fu.svg?branch=master)](https://travis-ci.org/Xuhao/china_region_fu)
6
+ [![Codacy Badge](https://api.codacy.com/project/badge/Grade/85643743367e47d6853b94431f4f503f)](https://www.codacy.com/app/xuhao/china_region_fu?utm_source=github.com&utm_medium=referral&utm_content=Xuhao/china_region_fu&utm_campaign=Badge_Grade)
7
+ [![Codacy Badge](https://api.codacy.com/project/badge/Coverage/85643743367e47d6853b94431f4f503f)](https://www.codacy.com/app/xuhao/china_region_fu?utm_source=github.com&utm_medium=referral&utm_content=Xuhao/china_region_fu&utm_campaign=Badge_Coverage)
8
+ [![Gem Version](https://badge.fury.io/rb/china_region_fu.svg)](https://badge.fury.io/rb/china_region_fu)
9
+
10
+ ChinaRegionFu is a rails engine(means only suit for rails) that provide region data of china and handy view helpers. You will got complete China region data after use this gem.
11
+
12
+ ![Screenshot](https://cloud.githubusercontent.com/assets/324973/19191241/2c5726a8-8cd4-11e6-960d-6edd1fa69427.gif "Screenshot")
13
+
14
+ ## Requirements
15
+
16
+ * CRuby 2.2 or greater
17
+ * Rails 4.0 or greater
18
+ * jQuery 1.8 or greater, ignore if don't need reactive effect
19
+
20
+ ## Installation
21
+
22
+ Put 'gem china_region_fu' to your Gemfile:
23
+
24
+ gem 'china_region_fu'
25
+
26
+ Run bundler command to install the gem:
27
+
28
+ bundle install
29
+
30
+ After you install the gem, you need run below tasks one by one:
31
+
32
+ 1. Copy migration file to your app.
33
+
34
+ <pre>rake china_region_fu:install:migrations</pre>
35
+
36
+ 2. Run db:migrate to create region tables.
37
+
38
+ <pre>rake db:migrate</pre>
39
+
40
+ 3. Download the latest regions.yml form [github](https://raw.github.com/Xuhao/china_region_data/master/regions.yml).
41
+
42
+ <pre>rake china_region_fu:download</pre>
43
+
44
+ 4. Import regions to database.
45
+
46
+ <pre>rake china_region_fu:import</pre>
47
+
48
+ You also can use below task to do the same things as four tasks above:
49
+
50
+ rake region:install
51
+
52
+ Region data is from [ChinaRegionData](https://github.com/Xuhao/china_region_data), check it out to see what kind of data you have now.
53
+
54
+ If you want to customize the region modules you can run the generator:
55
+
56
+ rails g china_region_fu:models
57
+
58
+ This will create:
59
+
60
+ create app/models/province.rb
61
+ create app/models/city.rb
62
+ create app/models/district.rb
63
+
64
+ So you can do what you want to do in this files.
65
+
66
+ ## Usage
67
+
68
+ ### Model
69
+
70
+ ```ruby
71
+ a = Province.last
72
+ a.name # => "台湾省"
73
+ a.cities.pluck(:name) # => ["嘉义市", "台南市", "新竹市", "台中市", "基隆市", "台北市"]
74
+
75
+ Province.first.districts.pluck(:name) # => ["延庆县", "密云县", "平谷区", ...]
76
+
77
+ c = City.last
78
+ c.name # => "酒泉市"
79
+ c.short_name # => "酒泉"
80
+ c.zip_code # => "735000"
81
+ c.pinyin # => "jiuquan"
82
+ c.pinyin_abbr # => "jq"
83
+ c.districts.pluck(:name) # => ["敦煌市", "玉门市", "阿克塞哈萨克族自治县", "肃北蒙古族自治县", "安西县", ...]
84
+ c.brothers.pluck(:name) # => ["甘南藏族自治州", "临夏回族自治州", "陇南市", ...]
85
+
86
+ d = District.last
87
+ d.name # => "吉木乃县"
88
+ d.short_name # => "吉木乃"
89
+ d.pinyin # => "jimunai"
90
+ d.pinyin_abbr # => "jmn"
91
+ d.city.name # => "阿勒泰地区"
92
+ d.province.name # => "新疆维吾尔自治区"
93
+ ```
94
+
95
+ ### View
96
+
97
+ #### Form helpers
98
+
99
+ ```erb
100
+ <%= form_for(@address) do |f| %>
101
+ <div class="field">
102
+ <%= f.label :province, '选择地区:' %><br />
103
+
104
+ # FormBuilder
105
+ <%= f.region_select :city %>
106
+ <%= f.region_select [:province_id, :city_id, :district_id], province_prompt: 'Do', city_prompt: 'it', district_prompt: 'like this' %>
107
+ <%= f.region_select [:province_id, :city_id], include_blank: true %>
108
+ <%= f.region_select [:city_id, :district_id] %>
109
+
110
+ # FormHelper
111
+ <%= region_select :address, :province_id %>
112
+ <%= region_select :address, [:province_id, :city_id, :district_id] %>
113
+ ...
114
+
115
+ # FormTagHelper
116
+ <%= region_select_tag :province_id, class: 'province-select', include_blank: true %>
117
+ <%= region_select_tag [:province_id, :city_id, :district_id], province_prompt: 'Do', city_prompt: 'it', district_prompt: 'like this', class: 'region-select' %>
118
+ ...
119
+ </div>
120
+ <% end %>
121
+ ```
122
+
123
+ #### SimpleForm
124
+
125
+ ```erb
126
+ <%= simple_form_for(@address) do |f| %>
127
+ <%= f.input :province_id, as: :region, collection: Province.select('id, name'), sub_region: :city_id %>
128
+ <%= f.input :city_id, as: :region, collection: City.where(province_id: f.object.province_id).select('id, name'), sub_region: :district_id %>
129
+ <%= f.input :district_id, as: :region, collection: District.where(city_id: f.object.city_id).select('id, name') %>
130
+ <%= china_region_fu_js %>
131
+ <% end %>
132
+ ```
133
+
134
+ #### Formtastic
135
+
136
+ ```erb
137
+ <%= semantic_form_for(@address) do |f| %>
138
+ <%= f.input :province_id, as: :region, collection: Province.select('id, name'), sub_region: :city_id) %>
139
+ <%= f.input :city_id, as: :region, collection: City.where(province_id: f.object.province_id).select('id, name'), sub_region: :district_id %>
140
+ <%= f.input :district_id, as: :region, collection: District.where(city_id: f.object.city_id).select('id, name') %>
141
+ <%= china_region_fu_js %>
142
+ <% end %>
143
+ ```
144
+
145
+ #### Reactive effect by Ajax
146
+
147
+ Once select one province, we want fetch cities of the selected province and fill the city select box automatically. to implement this, you need add below helper in your form:
148
+
149
+ ```erb
150
+ <%= china_region_fu_js %>
151
+ # or
152
+ <%= content_for :china_region_fu_js %>
153
+ ```
154
+
155
+ it will render:
156
+
157
+ ```html
158
+ <script type="text/javascript">
159
+ //<![CDATA[
160
+ window.chinaRegionFu = window.chinaRegionFu || {};
161
+ $(function(){
162
+ $('body').off('change', '.china-region-select').on('change', '.china-region-select', function(event) {
163
+ var $self, $targetDom;
164
+ $self = $(event.currentTarget);
165
+ $subRegionDom = $('[data-region-name="' + $self.data('sub-region') + '"]');
166
+ if ($subRegionDom.size() > 0) {
167
+ $.getJSON('/china_region_fu/fetch_options', {
168
+ columns: window.chinaRegionFu.fetchColumns || 'id,name',
169
+ sub_name: $self.data('sub-region'),
170
+ parent_name: $self.data('region-name'),
171
+ parent_id: $self.val()
172
+ }, function(json) {
173
+ if (window.chinaRegionFu.ajaxDone) {
174
+ window.chinaRegionFu.ajaxDone(json);
175
+ } else {
176
+ var options = [];
177
+ $self.parent().nextAll().find('.china-region-select > option[value!=""]').remove()
178
+ $.each(json.data, function(_, value) {
179
+ options.push("<option value='" + value.id + "'>" + value.name + "</option>");
180
+ });
181
+ $subRegionDom.append(options.join(''));
182
+ }
183
+ }).fail(function(xhr, textStatus, error) {
184
+ window.chinaRegionFu.ajaxFail && window.chinaRegionFu.ajaxFail(xhr, textStatus, error);
185
+ }).always(function(event, xhr, settings) {
186
+ window.chinaRegionFu.ajaxAlways && window.chinaRegionFu.ajaxAlways(event, xhr, settings);
187
+ });
188
+ }
189
+ });
190
+ });
191
+ //]]>
192
+ </script>
193
+ ```
194
+ hooks:
195
+
196
+ * `window.chinaRegionFu.fetchColumns`: Set columns of each returned region, default: `id,name`
197
+ * `window.chinaRegionFu.ajaxDone(json)`: Customize ajax `success` event,default: reload sub region options
198
+ * `window.chinaRegionFu.ajaxFail(xhr, textStatus, error)`: Customize ajax `fail` event
199
+ * `window.chinaRegionFu.ajaxAlways(event, xhr, settings)`: Customize ajax `always` event
200
+
201
+ ## Online example
202
+
203
+ [yihub.com](http://www.yihub.com/ "医院").
204
+
205
+ ## Contributing
206
+
207
+ Bug reports and pull requests are welcome on GitHub at https://github.com/xuhao/china_region_fu. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
208
+
209
+ ## License
210
+
211
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
212
+