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
@@ -1,21 +1,15 @@
1
1
  require 'httparty'
2
2
 
3
- namespace :region do
4
-
5
- desc 'Install china region fu'
6
- task :install do
7
- Rake::Task['china_region_fu_engine:install:migrations'].invoke
8
- Rake::Task['db:migrate'].invoke
9
- Rake::Task['region:download'].invoke
10
- Rake::Task['region:import'].invoke
11
- end
3
+ namespace :china_region_fu do
4
+ desc 'Setup china region fu'
5
+ task setup: ['china_region_fu:install:migrations', 'db:migrate', 'china_region_fu:download', 'china_region_fu:import']
12
6
 
13
7
  desc 'Download regions.yml from https://raw.github.com/Xuhao/china_region_data/master/regions.yml'
14
8
  task :download do
15
9
  begin
16
10
  remote_url = 'https://raw.github.com/Xuhao/china_region_data/master/regions.yml'
17
11
  puts 'Downloading ...'
18
- File.open(Rails.root.join('config', 'regions.yml'), 'wb') { |f| f.write(HTTParty.get(remote_url).body)}
12
+ File.open(Rails.root.join('config', 'regions.yml'), 'wb') { |f| f.write(HTTParty.get(remote_url).body) }
19
13
  puts "Done! File located at: \e[32mconfig/regions.yml\e[0m"
20
14
  rescue
21
15
  puts "\e[31mWarnning!!!\e[0m"
@@ -26,14 +20,14 @@ namespace :region do
26
20
  end
27
21
  end
28
22
 
29
- desc "Import region data to database from config/regions.yml"
30
- task :import => :environment do
23
+ desc 'Import region data to database from config/regions.yml'
24
+ task import: :environment do
31
25
  puts 'Importing ...'
32
26
  file_path = Rails.root.join('config', 'regions.yml')
33
27
  regions = File.open(file_path) { |file| YAML.load(file) }
34
28
  cleanup_regins
35
29
  load_to_db(regions)
36
- puts "Regions import done!"
30
+ puts 'Regions import done!'
37
31
  end
38
32
 
39
33
  def cleanup_regins
@@ -56,4 +50,4 @@ namespace :region do
56
50
  end
57
51
  end
58
52
 
59
- end
53
+ end
@@ -1,9 +1,5 @@
1
- require 'spec_helper'
2
-
3
1
  describe ChinaRegionFu do
4
-
5
- it 'should have a version number' do
6
- ChinaRegionFu::VERSION.should_not be_nil
2
+ it 'has a version number' do
3
+ expect(ChinaRegionFu::VERSION).not_to be nil
7
4
  end
8
-
9
- end
5
+ end
@@ -0,0 +1,113 @@
1
+ require 'action_view'
2
+ require 'china_region_fu/helpers/helpers'
3
+
4
+ RSpec.shared_examples 'render region select correctly' do |invalid_proc, single_proc, multi_proc|
5
+ context 'when pass invalid regions' do
6
+ it 'raise error' do
7
+ expect { invalid_proc.call }.to raise_error(ChinaRegionFu::InvalidFieldError)
8
+ end
9
+ end
10
+
11
+ context 'when pass one region' do
12
+ it 'return one select tag wrapped by a div' do
13
+ output = single_proc.call
14
+ expect(output).to start_with('<div class="input region province_id"><select')
15
+ expect(output).to include('china-region-select')
16
+ expect(output).to include('data-region-name="province_id"')
17
+ expect(output).to include('value="16">河南省</option>')
18
+ expect(output).not_to include('data-sub-region')
19
+ end
20
+ end
21
+
22
+ context 'when pass multiple regions' do
23
+ it 'return multiple select tags' do
24
+ output = multi_proc.call
25
+ expect(output).to start_with('<div class="input region province_id"><select')
26
+ expect(output).to include('<div class="input region city_id"><select')
27
+ expect(output).to include('<div class="input region district_id"><select')
28
+ expect(output).to include('data-region-name="province_id"')
29
+ expect(output).to include('data-region-name="city_id"')
30
+ expect(output).to include('data-region-name="district_id"')
31
+ expect(output).to include('data-sub-region="city_id"')
32
+ expect(output).to include('data-sub-region="district_id"')
33
+ end
34
+ end
35
+ end
36
+
37
+ RSpec.describe ChinaRegionFu::Helpers do
38
+ describe ChinaRegionFu::Helpers::FormHelper do
39
+ view = ActionView::Base.new
40
+ view.extend(ChinaRegionFu::Helpers::FormHelper)
41
+
42
+ describe '#region_select_tag' do
43
+ include_examples(
44
+ 'render region select correctly',
45
+ ->() { view.region_select_tag(:invalid_region) },
46
+ ->() { view.region_select_tag(:province_id) },
47
+ ->() { view.region_select_tag([:province_id, :city_id, :district_id]) }
48
+ )
49
+ end
50
+
51
+ describe '#region_select' do
52
+ include_examples(
53
+ 'render region select correctly',
54
+ ->() { view.region_select(:address, :invalid_region) },
55
+ ->() { view.region_select(:address, :province_id) },
56
+ ->() { view.region_select(:address, [:province_id, :city_id, :district_id]) }
57
+ )
58
+
59
+ it 'act as a objet field' do
60
+ output = view.region_select(:address, [:province_id, :city_id, :district_id])
61
+ expect(output).to include('name="address[province_id]"')
62
+ expect(output).to include('name="address[city_id]"')
63
+ expect(output).to include('name="address[district_id]"')
64
+ expect(output).to include('id="address_province_id"')
65
+ expect(output).to include('id="address_city_id"')
66
+ expect(output).to include('id="address_district_id"')
67
+ end
68
+ end
69
+ end
70
+
71
+ describe ChinaRegionFu::Helpers::FormBuilder do
72
+ template = ActionView::Base.new
73
+ template.extend(ChinaRegionFu::Helpers::FormHelper)
74
+
75
+ context 'build form for object with region' do
76
+ view = ActionView::Helpers::FormBuilder.new('address', Address.new(province_id: 16, city_id: 166, district_id: 1513), template, {})
77
+ view.extend(ChinaRegionFu::Helpers::FormBuilder)
78
+
79
+ include_examples(
80
+ 'render region select correctly',
81
+ ->() { view.region_select(:invalid_region) },
82
+ ->() { view.region_select(:province_id) },
83
+ ->() { view.region_select([:province_id, :city_id, :district_id]) }
84
+ )
85
+
86
+ it 'set correct value for each region field' do
87
+ output = view.region_select([:province_id, :city_id, :district_id])
88
+ expect(output).to include('<option selected="selected" value="16">河南省</option>')
89
+ expect(output).to include('<option selected="selected" value="166">信阳市</option>')
90
+ expect(output).to include('<option selected="selected" value="1513">商城县</option>')
91
+ end
92
+ end
93
+
94
+ context 'build form for object without region' do
95
+ view = ActionView::Helpers::FormBuilder.new('address', Address.new, template, {})
96
+ view.extend(ChinaRegionFu::Helpers::FormBuilder)
97
+
98
+ include_examples(
99
+ 'render region select correctly',
100
+ ->() { view.region_select(:invalid_region) },
101
+ ->() { view.region_select(:province_id) },
102
+ ->() { view.region_select([:province_id, :city_id, :district_id]) }
103
+ )
104
+
105
+ it 'fill options for first region field' do
106
+ output = view.region_select([:province_id, :city_id, :district_id])
107
+ expect(output).to include('<option value="16">河南省</option>')
108
+ end
109
+ end
110
+
111
+ end
112
+
113
+ end
@@ -0,0 +1,87 @@
1
+ require 'china_region_fu/helpers/utils'
2
+
3
+ RSpec.describe ChinaRegionFu::Utils do
4
+ let(:proxy) { Class.new { extend ChinaRegionFu::Utils } }
5
+
6
+ describe '#china_region_fu_js' do
7
+ it 'return trusted Javascript snippet' do
8
+ js = proxy.china_region_fu_js
9
+ expect(js).to match(/^\s*<script type="text\/javascript">/)
10
+ expect(js).to match(/<script type="text\/javascript">\s*/)
11
+ end
12
+ end
13
+
14
+ describe '#append_html_class_option' do
15
+ context 'when option not have key `:class`' do
16
+ let (:html_options) { {} }
17
+ it 'append china-region-select to `:class` option' do
18
+ new_html_options = proxy.append_html_class_option(html_options)
19
+ expect(new_html_options[:class]).to eq(' china-region-select')
20
+ expect(html_options[:class]).to be_nil
21
+ end
22
+ end
23
+
24
+ context 'when option have key `:class`' do
25
+ let (:html_options) { { class: 'some class' } }
26
+ it 'append china-region-select to `:class` option' do
27
+ new_html_options = proxy.append_html_class_option(html_options)
28
+ expect(new_html_options[:class]).to eq('some class china-region-select')
29
+ expect(html_options[:class]).to eq('some class')
30
+ end
31
+ end
32
+ end
33
+
34
+ describe '#append_html_data_option' do
35
+ context 'when `sub_region` be passed' do
36
+ let(:origin_html_options) { {} }
37
+ let(:html_options) { proxy.append_html_data_option('province_id', 'city_id', origin_html_options) }
38
+ it 'append `sub_region` to options[:data][:sub_region]' do
39
+ expect(html_options[:data]).to eq(region_name: 'province_id', sub_region: 'city_id')
40
+ expect(origin_html_options).to eq({})
41
+ end
42
+ end
43
+
44
+ context 'when `sub_region` not be passed' do
45
+ let(:origin_html_options) { {} }
46
+ let(:html_options) { proxy.append_html_data_option('province_id', nil, origin_html_options) }
47
+ it 'append `sub_region` to options[:data][:sub_region]' do
48
+ expect(html_options[:data]).to eq(region_name: 'province_id')
49
+ expect(origin_html_options).to eq({})
50
+ end
51
+ end
52
+ end
53
+
54
+ describe '#append_html_options' do
55
+ context 'when `sub_region` be passed' do
56
+ let(:origin_html_options) { {} }
57
+ let(:html_options) { proxy.append_html_options('province_id', 'city_id', origin_html_options) }
58
+
59
+ it 'append css class and data' do
60
+ expect(html_options[:data]).to eq(region_name: 'province_id', sub_region: 'city_id')
61
+ expect(html_options[:class]).to eq(' china-region-select')
62
+ expect(origin_html_options).to eq({})
63
+ end
64
+ end
65
+
66
+ context 'when `sub_region` not be passed' do
67
+ let(:origin_html_options) { { class: 'some class' } }
68
+ let(:html_options) { proxy.append_html_options('province_id', nil, origin_html_options) }
69
+ it 'append `sub_region` to options[:data][:sub_region]' do
70
+ expect(html_options[:data]).to eq(region_name: 'province_id')
71
+ expect(html_options[:class]).to eq('some class china-region-select')
72
+ expect(origin_html_options).to eq(class: 'some class')
73
+ end
74
+ end
75
+ end
76
+
77
+ describe '#append_prompt' do
78
+ let(:options) { { include_blank: false, province_prompt: 'select province', city_prompt: 'select city', district_prompt: 'select district' } }
79
+
80
+ it 'extract promp for passed region' do
81
+ expect(proxy.append_prompt('province', options)).to eq(include_blank: false, prompt: 'select province')
82
+ expect(proxy.append_prompt('city_id', options)).to eq(include_blank: false, prompt: 'select city')
83
+ expect(proxy.append_prompt('district', options)).to eq(include_blank: false, prompt: 'select district')
84
+ end
85
+ end
86
+
87
+ end
@@ -1,2 +1,7 @@
1
+ require 'pry-byebug'
2
+ require 'codacy-coverage'
1
3
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
- require 'china_region_fu'
4
+ require 'china_region_fu'
5
+ Dir[File.expand_path('../support/**/*.rb', __FILE__)].each { |f| require f }
6
+
7
+ Codacy::Reporter.start
@@ -0,0 +1,39 @@
1
+ require 'ostruct'
2
+
3
+ module ActiveRecord
4
+ class Base < ::OpenStruct; end
5
+ end
6
+
7
+ class Address < ActiveRecord::Base; end
8
+
9
+ MockModel = Struct.new(:id, :name, :pinyin, :pinyin_abbr, :short_name) do
10
+ def self.pluck(*args)
11
+ []
12
+ end
13
+
14
+ def self.where(*args)
15
+ self
16
+ end
17
+ end
18
+
19
+ class Province < MockModel
20
+ def self.pluck(*args)
21
+ [['河南省', 16]]
22
+ end
23
+ end
24
+
25
+ class City < MockModel
26
+ attr_accessor :province_id, :level, :zip_code
27
+
28
+ def self.pluck(*args)
29
+ [['信阳市', 166]]
30
+ end
31
+ end
32
+
33
+ class District < MockModel
34
+ attr_accessor :city_id
35
+
36
+ def self.pluck(*args)
37
+ [['商城县', 1513]]
38
+ end
39
+ end
metadata CHANGED
@@ -1,133 +1,118 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: china_region_fu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Xuhao
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-02 00:00:00.000000000 Z
11
+ date: 2016-10-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.3'
19
+ version: '1.13'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.3'
26
+ version: '1.13'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '10.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: rspec
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - '>='
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - '>='
53
- - !ruby/object:Gem::Version
54
- version: '0'
40
+ version: '10.0'
55
41
  - !ruby/object:Gem::Dependency
56
42
  name: activesupport
57
43
  requirement: !ruby/object:Gem::Requirement
58
44
  requirements:
59
- - - '>='
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :runtime
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - '>='
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: actionpack
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - '>='
45
+ - - ">="
74
46
  - !ruby/object:Gem::Version
75
- version: '0'
47
+ version: '4.0'
76
48
  type: :runtime
77
49
  prerelease: false
78
50
  version_requirements: !ruby/object:Gem::Requirement
79
51
  requirements:
80
- - - '>='
52
+ - - ">="
81
53
  - !ruby/object:Gem::Version
82
- version: '0'
54
+ version: '4.0'
83
55
  - !ruby/object:Gem::Dependency
84
56
  name: httparty
85
57
  requirement: !ruby/object:Gem::Requirement
86
58
  requirements:
87
- - - '>='
59
+ - - "~>"
88
60
  - !ruby/object:Gem::Version
89
- version: '0'
61
+ version: 0.14.0
90
62
  type: :runtime
91
63
  prerelease: false
92
64
  version_requirements: !ruby/object:Gem::Requirement
93
65
  requirements:
94
- - - '>='
66
+ - - "~>"
95
67
  - !ruby/object:Gem::Version
96
- version: '0'
97
- description: China region Ruby on rails interface
68
+ version: 0.14.0
69
+ description: china_region_fu provides simple helpers to get an HTML select list of
70
+ countries, cities and districts.
98
71
  email:
99
72
  - xuhao@rubyfans.com
100
- executables: []
73
+ executables:
74
+ - console
75
+ - rspec
76
+ - setup
101
77
  extensions: []
102
78
  extra_rdoc_files: []
103
79
  files:
104
- - .gitignore
105
- - .rspec
106
- - .travis.yml
80
+ - ".gitignore"
81
+ - ".rspec"
82
+ - ".rubocop.yml"
83
+ - ".travis.yml"
84
+ - CODE_OF_CONDUCT.md
107
85
  - Gemfile
108
- - LICENSE
86
+ - LICENSE.txt
87
+ - README.en.md
109
88
  - README.md
110
89
  - Rakefile
111
90
  - app/controllers/china_region_fu/fetch_options_controller.rb
112
91
  - app/models/city.rb
113
92
  - app/models/district.rb
114
93
  - app/models/province.rb
94
+ - bin/console
95
+ - bin/rspec
96
+ - bin/setup
115
97
  - china_region_fu.gemspec
116
98
  - config/routes.rb
117
99
  - db/migrate/20111111111111_create_china_region_tables.rb
118
100
  - lib/china_region_fu.rb
119
101
  - lib/china_region_fu/engine.rb
120
- - lib/china_region_fu/exceptions.rb
102
+ - lib/china_region_fu/errors.rb
121
103
  - lib/china_region_fu/helpers/formtastic.rb
122
104
  - lib/china_region_fu/helpers/helpers.rb
123
105
  - lib/china_region_fu/helpers/simple_form.rb
124
- - lib/china_region_fu/helpers/utilities.rb
106
+ - lib/china_region_fu/helpers/utils.rb
125
107
  - lib/china_region_fu/version.rb
126
108
  - lib/generators/china_region_fu/models/USAGE
127
109
  - lib/generators/china_region_fu/models/models_generator.rb
128
- - lib/tasks/region.rake
110
+ - lib/tasks/china_region_fu_tasks.rake
129
111
  - spec/china_region_fu_spec.rb
112
+ - spec/helpers/helpers_spec.rb
113
+ - spec/helpers/utils_spec.rb
130
114
  - spec/spec_helper.rb
115
+ - spec/support/models.rb
131
116
  homepage: https://github.com/Xuhao/china_region_fu
132
117
  licenses:
133
118
  - MIT
@@ -138,20 +123,24 @@ require_paths:
138
123
  - lib
139
124
  required_ruby_version: !ruby/object:Gem::Requirement
140
125
  requirements:
141
- - - '>='
126
+ - - ">="
142
127
  - !ruby/object:Gem::Version
143
- version: '0'
128
+ version: 2.2.2
144
129
  required_rubygems_version: !ruby/object:Gem::Requirement
145
130
  requirements:
146
- - - '>='
131
+ - - ">="
147
132
  - !ruby/object:Gem::Version
148
- version: '0'
133
+ version: 1.8.11
149
134
  requirements: []
150
135
  rubyforge_project:
151
- rubygems_version: 2.0.6
136
+ rubygems_version: 2.6.6
152
137
  signing_key:
153
138
  specification_version: 4
154
- summary: China region Ruby on rails interface
139
+ summary: china_region_fu provides simple helpers to get an HTML select list of countries,
140
+ cities and districts.
155
141
  test_files:
156
142
  - spec/china_region_fu_spec.rb
143
+ - spec/helpers/helpers_spec.rb
144
+ - spec/helpers/utils_spec.rb
157
145
  - spec/spec_helper.rb
146
+ - spec/support/models.rb