china_regions 1.0.3 → 1.0.8

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 65852eef10a092038e498c3f7eec696ad6c2598072839bf3edafbc96d52579c6
4
- data.tar.gz: '0169f567da375fd7d4e9713fe6004991ab01b34ee0a8e4fb61f33284ee96d296'
3
+ metadata.gz: 3c8df7cd52e1ba2259ae5f84635fc7319c8ac993b22d148ed1f9bf8fbf89598a
4
+ data.tar.gz: 2c5eddaad1e5dba0dadfe7e41fd6c4a65ad2c0fd2ae6b530080d8992cb9205ba
5
5
  SHA512:
6
- metadata.gz: 639f67a31cf88385fec479e174e5d25999c8ee2f4e61fdac1fdf31e08012aefbfa8d5c32c40c97989dcad9629085ff656804f06d3c4931818e0158a07dd70f76
7
- data.tar.gz: 903e20a4be9c05635e05117ace6e8e429b44d50be7938c02d7708631c4462a20c1f8a04e846a3e9cc54ccbdf8886ef1b2b0f325d8847a53d7c78ed89fd35ed47
6
+ metadata.gz: 329fb0afdf5363cd5c2b5a8bdbed0dc3cc1ba006914090e591d4a2e9b69e8f526c5acd98ad4aebb5317875379cd0609c1fc6b1da58f30cfbe94fdf393aa709ca
7
+ data.tar.gz: 9255c6956892d177069482849d08b056e4653bf47eecf189a8cf26da61d873d5b87f6d7f86163a8407f757e1357eb6b925389bf9022a922ba88092804d3ce2a5
data/.gitignore CHANGED
@@ -3,6 +3,9 @@
3
3
  .DS_Store
4
4
  .ruby-version
5
5
  .rvmrc
6
+
6
7
  Gemfile.lock
8
+
7
9
  pkg/*
8
- doc
10
+ doc/
11
+ coverage/
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/.travis.yml CHANGED
@@ -1,8 +1,12 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.3
4
- - 2.0.0
5
- - 2.1.0
3
+ - 2.3.0
4
+ - 2.4.0
5
+ - 2.5.0
6
+ - 2.6.0
7
+ - 2.7.0
8
+ - 3.0.0
9
+ - 3.0.1
6
10
 
7
11
  before_install:
8
12
  - gem install bundler
data/Rakefile CHANGED
@@ -1,4 +1,7 @@
1
- #!/usr/bin/env rake
2
1
  # frozen_string_literal: true
3
2
 
4
3
  require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+ task default: :spec
data/app/models/city.rb CHANGED
@@ -10,7 +10,7 @@ class City < ApplicationRecord
10
10
 
11
11
  # Relationships
12
12
  belongs_to :province, counter_cache: true
13
- delegate :name, to: :province
13
+ delegate :name, to: :province, prefix: true
14
14
 
15
15
  has_many :districts, dependent: :destroy
16
16
 
@@ -9,19 +9,19 @@ class District < ApplicationRecord
9
9
 
10
10
  # Relationships
11
11
  belongs_to :city, counter_cache: true
12
- delegate :name, to: :city
12
+ delegate :name, to: :city, prefix: true
13
13
 
14
14
  # Filters
15
15
  scope :for_city, ->(city_id) { where(city_id: city_id) }
16
16
 
17
- def full_name
18
- [province.name, city_name, name].compact.join(' - ')
19
- end
20
-
21
17
  def province
22
18
  city.province
23
19
  end
24
20
 
21
+ def full_name
22
+ [province.name, city_name, name].compact.join(' - ')
23
+ end
24
+
25
25
  def short_name
26
26
  @short_name ||= name.gsub(/区|县|市|自治县/, '')
27
27
  end
@@ -10,4 +10,7 @@ class Province < ApplicationRecord
10
10
  # Relationships
11
11
  has_many :cities, dependent: :destroy
12
12
  has_many :districts, through: :cities
13
+
14
+ # Filter by name
15
+ scope :filter_by, ->(fname) { where(["name_en = :name or name = :name", name: fname]) }
13
16
  end
@@ -24,5 +24,6 @@ Gem::Specification.new do |gem|
24
24
  gem.add_dependency 'ruby-pinyin'
25
25
  gem.add_development_dependency 'coveralls'
26
26
  gem.add_development_dependency 'pry'
27
+ gem.add_development_dependency 'rspec'
27
28
  gem.add_development_dependency 'rubocop'
28
29
  end
@@ -10,18 +10,23 @@ en:
10
10
  attributes:
11
11
  province:
12
12
  name: Name
13
+ code: Code
13
14
  name_en: English Name
14
15
  name_abbr: Short Name
16
+ cities_count: Cities Count
15
17
  city:
16
- name: Nam
18
+ name: Name
17
19
  province_id: Province
18
20
  province: Province
19
21
  name_en: English Name
20
22
  name_abbr: Short Name
21
23
  level: Level
24
+ code: Code
25
+ districts_count: Districts Count
22
26
  district:
23
27
  name: Name
24
28
  city_id: City
25
29
  city: City
26
30
  name_en: English Name
27
31
  name_abbr: Short Name
32
+ code: Code
@@ -10,8 +10,10 @@ zh:
10
10
  attributes:
11
11
  province:
12
12
  name: 名称
13
+ code: 编码
13
14
  name_en: 拼音
14
15
  name_abbr: 简称
16
+ cities_count: 城市数
15
17
  city:
16
18
  name: 名称
17
19
  province_id: 省份
@@ -19,9 +21,12 @@ zh:
19
21
  name_en: 拼音
20
22
  name_abbr: 简称
21
23
  level: 等级
24
+ code: 编码
25
+ districts_count: 地区数
22
26
  district:
23
27
  name: 名称
24
28
  city_id: 城市
25
29
  city: 城市
26
30
  name_en: 拼音
27
31
  name_abbr: 简称
32
+ code: 编码
@@ -1,11 +1,10 @@
1
-
2
1
  # frozen_string_literal: true
3
2
 
4
3
  module ChinaRegions
5
4
  module Helpers
6
5
  module FormHelper
7
- def region_select(object_name, methods, options = {}, html_options = {})
8
- output = ''
6
+ def region_select(object_name, fields, options = {}, html_options = {})
7
+ output = []
9
8
 
10
9
  preselected_choices = setup_regions_options(options)
11
10
 
@@ -15,54 +14,56 @@ module ChinaRegions
15
14
 
16
15
  dropdown_prefix = options[:prefix] ? options[:prefix].to_s + '_' : ''
17
16
 
18
- if Array == methods
19
- methods.each_with_index do |method, index|
20
- if region_klass = method.to_s.classify.safe_constantize
21
- choices = fetch_choices(region_klass, method, preselected_choices, index)
22
- choices = prioritize_choices(options[:priority][method], choices) if options[:priority].try(:[], method)
17
+ if fields.is_a?(Array)
18
+ fields.each_with_index do |field, field_index|
19
+ if region_klass = field.to_s.classify.safe_constantize
20
+ choices = fetch_choices(region_klass, field, preselected_choices, field_index)
21
+ choices = prioritize_choices(options[:priority][field], choices) if options[:priority].try(:[], field)
23
22
 
24
- next_method = methods.at(index + 1)
23
+ next_method = fields.at(field_index + 1)
25
24
 
26
- set_prompt(method, options, region_klass)
27
- set_html_options(object_name, method, html_options, next_method, dropdown_prefix)
25
+ set_prompt(field, options, region_klass)
26
+ set_html_options(object_name, field, html_options, next_method, dropdown_prefix)
28
27
 
29
- if options[:default] && options[:default][method]
30
- options[:selected] = options[:default][method] if options[:default][method]
28
+ if options[:default] && options[:default][field]
29
+ options[:selected] = options[:default][field] if options[:default][field]
31
30
  end
32
31
 
33
- output << select(object_name, "#{dropdown_prefix}#{method}_id", choices, options, html_options)
32
+ output << select(object_name, "#{dropdown_prefix}#{field}_id", choices, options, html_options)
34
33
  else
35
- raise "Method '#{method}' is not a vaild attribute of #{object_name}"
34
+ raise "Method '#{field}' is not a vaild attribute of #{object_name}"
36
35
  end
37
36
  end
38
37
  else
39
- inner_methods = if methods.to_s.include?('_id')
40
- inner_methods = methods
41
- methods = methods.to_s.gsub(/(_id)$/, '')
38
+ inner_methods = if fields.to_s.include?('_id')
39
+ inner_methods = fields
40
+ fields = fields.to_s.gsub(/(_id)$/, '')
42
41
  inner_methods
43
42
  else
44
- (methods.to_s + '_id').to_sym
43
+ (fields.to_s + '_id').to_sym
45
44
  end
46
45
 
47
- if region_klass = methods.to_s.classify.safe_constantize
46
+ if region_klass = fields.to_s.classify.safe_constantize
48
47
  options[:prompt] = region_prompt(region_klass)
48
+ options[:selected] = preselected_choices[:province_id] if fields == :province && preselected_choices[:province_id]
49
49
 
50
- options[:selected] = preselected_choices[:province_id] if methods == :province && preselected_choices[:province_id]
51
-
52
- output << select(object_name, inner_methods, region_klass.where(nil).collect { |p| [p.name, p.id] }, options = options, html_options = html_options)
50
+ output << select(object_name, inner_methods, region_options(region_klass), options: options, html_options: html_options)
53
51
  else
54
- raise "Method '#{method}' is not a vaild attribute of #{object_name}"
52
+ raise "Method '#{fields}' is not a vaild attribute of #{object_name}"
55
53
  end
56
54
  end
57
- output.html_safe
55
+ output.join.html_safe
58
56
  end
59
57
 
60
58
  private
61
59
  def fetch_choices(region_klass, method, preselected_choices, index)
62
60
  return preselected_choices[method] if preselected_choices[method]
63
- return [] unless index.zero?
64
61
 
65
- region_klass.where(nil).collect { |p| [p.name, p.id] }
62
+ index.zero? ? region_options(region_klass) : []
63
+ end
64
+
65
+ def region_options(region_klass)
66
+ @region_options ||= region_klass.where(nil).collect { |p| [p.name, p.id] }
66
67
  end
67
68
 
68
69
  def prioritize_choices(priorities, choices)
@@ -89,10 +90,10 @@ module ChinaRegions
89
90
  def setup_regions_options(options)
90
91
  return {} unless options[:default] && options[:default][:province]
91
92
 
92
- # TODO: Add validator to check if the passed province, city or district exists within the models
93
+ # Add validator to check if the passed province, city or district exists within the models
93
94
  province_id = get_province_id(options[:default][:province])
94
- cities = City.where(province_id: province_id)
95
- districts = District.where(city_id: cities)
95
+ cities = City.for_province(province_id)
96
+ districts = District.for_city(city_id: cities)
96
97
 
97
98
  {
98
99
  province_id: province_id,
@@ -111,7 +112,8 @@ module ChinaRegions
111
112
 
112
113
  def get_province_id(province)
113
114
  return province if province.to_s =~ /\A[0-9]*\z/
114
- Province.where('name_en = ? OR name = ?', province.downcase, province).first.id
115
+
116
+ Province.filter(province.downcase).first.id
115
117
  end
116
118
 
117
119
  def set_html_options(object_name, method, html_options, next_region, prefix)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ChinaRegions
4
- VERSION = '1.0.3'
4
+ VERSION = '1.0.8'
5
5
  end
@@ -14,7 +14,7 @@ module ChinaRegions
14
14
  def copy_locales
15
15
  %w[en zh].each do |locale|
16
16
  config_file = "config/locales/regions.#{locale}.yml"
17
- copy_file "../../../../config/locales/#{locale}.yml", config_file unless File.exist?(config_file)
17
+ copy_file "../../../../config/locales/china_regions.#{locale}.yml", config_file unless File.exist?(config_file)
18
18
  end
19
19
  end
20
20
 
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'coveralls'
4
+ Coveralls.wear!
5
+
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ # rspec-expectations config goes here. You can use an alternate
9
+ # assertion/expectation library such as wrong or the stdlib/minitest
10
+ # assertions if you prefer.
11
+ config.expect_with :rspec do |expectations|
12
+ # This option will default to `true` in RSpec 4. It makes the `description`
13
+ # and `failure_message` of custom matchers include text for helper methods
14
+ # defined using `chain`, e.g.:
15
+ # be_bigger_than(2).and_smaller_than(4).description
16
+ # # => "be bigger than 2 and smaller than 4"
17
+ # ...rather than:
18
+ # # => "be bigger than 2"
19
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
20
+ end
21
+
22
+ # rspec-mocks config goes here. You can use an alternate test double
23
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
24
+ config.mock_with :rspec do |mocks|
25
+ # Prevents you from mocking or stubbing a method that does not exist on
26
+ # a real object. This is generally recommended, and will default to
27
+ # `true` in RSpec 4.
28
+ mocks.verify_partial_doubles = true
29
+ end
30
+
31
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
32
+ # have no way to turn it off -- the option exists only for backwards
33
+ # compatibility in RSpec 3). It causes shared context metadata to be
34
+ # inherited by the metadata hash of host groups and examples, rather than
35
+ # triggering implicit auto-inclusion in groups with matching metadata.
36
+ config.shared_context_metadata_behavior = :apply_to_host_groups
37
+
38
+ # The settings below are suggested to provide a good initial experience
39
+ # with RSpec, but feel free to customize to your heart's content.
40
+ # # This allows you to limit a spec run to individual examples or groups
41
+ # # you care about by tagging them with `:focus` metadata. When nothing
42
+ # # is tagged with `:focus`, all examples get run. RSpec also provides
43
+ # # aliases for `it`, `describe`, and `context` that include `:focus`
44
+ # # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
45
+ # config.filter_run_when_matching :focus
46
+ #
47
+ # # Allows RSpec to persist some state between runs in order to support
48
+ # # the `--only-failures` and `--next-failure` CLI options. We recommend
49
+ # # you configure your source control system to ignore this file.
50
+ # config.example_status_persistence_file_path = "spec/examples.txt"
51
+ #
52
+ # # Limits the available syntax to the non-monkey patched syntax that is
53
+ # # recommended. For more details, see:
54
+ # # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
55
+ # # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
56
+ # # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
57
+ # config.disable_monkey_patching!
58
+ #
59
+ # # This setting enables warnings. It's recommended, but in some cases may
60
+ # # be too noisy due to issues in dependencies.
61
+ # config.warnings = true
62
+ #
63
+ # # Many RSpec users commonly either run the entire suite or an individual
64
+ # # file, and it's useful to allow more verbose output when running an
65
+ # # individual spec file.
66
+ # if config.files_to_run.one?
67
+ # # Use the documentation formatter for detailed output,
68
+ # # unless a formatter has already been configured
69
+ # # (e.g. via a command-line flag).
70
+ # config.default_formatter = "doc"
71
+ # end
72
+ #
73
+ # # Print the 10 slowest examples and example groups at the
74
+ # # end of the spec run, to help surface which specs are running
75
+ # # particularly slow.
76
+ # config.profile_examples = 10
77
+ #
78
+ # # Run specs in random order to surface order dependencies. If you find an
79
+ # # order dependency and want to debug it, you can fix the order by providing
80
+ # # the seed, which is printed after each run.
81
+ # # --seed 1234
82
+ # config.order = :random
83
+ #
84
+ # # Seed global randomization in this process using the `--seed` CLI option.
85
+ # # Setting this allows you to use `--seed` to deterministically reproduce
86
+ # # test failures related to randomization by passing the same `--seed` value
87
+ # # as the one that triggered the failure.
88
+ # Kernel.srand config.seed
89
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: china_regions
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Encore Shao
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-09 00:00:00.000000000 Z
11
+ date: 2021-12-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: down
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: rubocop
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -97,14 +111,13 @@ dependencies:
97
111
  description: China Regions is a Ruby on rails interface
98
112
  email:
99
113
  - encore.shao@gmail.com
100
- executables:
101
- - console
102
- - setup
114
+ executables: []
103
115
  extensions: []
104
116
  extra_rdoc_files: []
105
117
  files:
106
118
  - ".coveralls.yml"
107
119
  - ".gitignore"
120
+ - ".rspec"
108
121
  - ".rubocop.yml"
109
122
  - ".travis.yml"
110
123
  - Gemfile
@@ -117,11 +130,9 @@ files:
117
130
  - app/models/city.rb
118
131
  - app/models/district.rb
119
132
  - app/models/province.rb
120
- - bin/console
121
- - bin/setup
122
133
  - china_regions.gemspec
123
- - config/locales/en.yml
124
- - config/locales/zh.yml
134
+ - config/locales/china_regions.en.yml
135
+ - config/locales/china_regions.zh.yml
125
136
  - config/routes.rb
126
137
  - lib/china_regions.rb
127
138
  - lib/china_regions/engine.rb
@@ -131,11 +142,12 @@ files:
131
142
  - lib/generators/china_regions/regions_generator.rb
132
143
  - lib/generators/china_regions/templates/migration.rb
133
144
  - lib/tasks/china_regions.rake
145
+ - spec/spec_helper.rb
134
146
  homepage: https://github.com/encoreshao/china_regions
135
147
  licenses:
136
148
  - MIT
137
149
  metadata: {}
138
- post_install_message:
150
+ post_install_message:
139
151
  rdoc_options: []
140
152
  require_paths:
141
153
  - lib
@@ -151,8 +163,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
151
163
  version: '0'
152
164
  requirements: []
153
165
  rubygems_version: 3.0.3
154
- signing_key:
166
+ signing_key:
155
167
  specification_version: 4
156
168
  summary: Rails 4+ version of dropdowns for all provinces, cities, and districts in
157
169
  China.
158
- test_files: []
170
+ test_files:
171
+ - spec/spec_helper.rb
data/bin/console DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'bundler/setup'
4
- require 'china_regions'
5
-
6
- # If you use this, don't forget to add pry to your Gemfile!
7
- require 'pry'
8
- Pry.start
data/bin/setup DELETED
@@ -1,3 +0,0 @@
1
- #!/usr/bin/env bash
2
-
3
- bundle install