china_regions 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 72b433dc0d51468a2a5e06c4b51aa450e2e9ff661f13995019e7bc05f99d5b7f
4
- data.tar.gz: 748f7542e2a8a7c6267d9bb543505706c1c1104eb08d3d71c4eb5891180d9c20
3
+ metadata.gz: 5d726fa153fb5770afedd23fdde97565f696a2bfb46f1b253f882735455f596e
4
+ data.tar.gz: f6be4b23f39780341158f36ce78c4c06aa48d7c77b6d1ebfd5131e0f083692e5
5
5
  SHA512:
6
- metadata.gz: 64083c36bff08087d65f17a820cb24245f7279d5d2d10f5973b8dd9e39aba2fb2e57176a3322788af92b9b43c560f4d32664114a3768f0f91edeb6804c29e1f9
7
- data.tar.gz: 314f8737b7bdc229d1f442260947ee0f4df14dade593f3a34252cc8497beed94885653ecf22d82f117f6f83d196d76d52b084384140f8c7d24ad0f4031701cc0
6
+ metadata.gz: 2af7ef16ade55d4a5b4c3cf6c6e7a4a383e7942fa10a89883ca16d5283c822fa7de672c95032a4c1f5821b99897fa17e855015e163287a495a518d9f4a7443d1
7
+ data.tar.gz: 90a9efaed4c9dda5c6b980b46b27b551447538b5f7adafaf723d9f8b076378912cd72a8cd56a7b6c903ec629e08b5e48957d080b072d17caa12561f4877d4ecb
data/.gitignore CHANGED
@@ -5,4 +5,5 @@
5
5
  .rvmrc
6
6
  Gemfile.lock
7
7
  pkg/*
8
- doc
8
+ doc/
9
+ coverage/
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
@@ -1,8 +1,10 @@
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
6
8
 
7
9
  before_install:
8
10
  - gem install bundler
data/Rakefile CHANGED
@@ -1,4 +1,8 @@
1
1
  #!/usr/bin/env rake
2
- # frozen_string_literal: true
2
+
3
+ # require 'rspec/core/rake_task'
4
+
5
+ # RSpec::Core::RakeTask.new(:spec)
6
+ # task default: :spec
3
7
 
4
8
  require 'bundler/gem_tasks'
@@ -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, ->(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
@@ -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.4'
4
+ VERSION = '1.0.5'
5
5
  end
@@ -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.4
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Encore Shao
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-09 00:00:00.000000000 Z
11
+ date: 2020-02-19 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
@@ -105,6 +119,7 @@ extra_rdoc_files: []
105
119
  files:
106
120
  - ".coveralls.yml"
107
121
  - ".gitignore"
122
+ - ".rspec"
108
123
  - ".rubocop.yml"
109
124
  - ".travis.yml"
110
125
  - Gemfile
@@ -131,6 +146,7 @@ files:
131
146
  - lib/generators/china_regions/regions_generator.rb
132
147
  - lib/generators/china_regions/templates/migration.rb
133
148
  - lib/tasks/china_regions.rake
149
+ - spec/spec_helper.rb
134
150
  homepage: https://github.com/encoreshao/china_regions
135
151
  licenses:
136
152
  - MIT
@@ -155,4 +171,5 @@ signing_key:
155
171
  specification_version: 4
156
172
  summary: Rails 4+ version of dropdowns for all provinces, cities, and districts in
157
173
  China.
158
- test_files: []
174
+ test_files:
175
+ - spec/spec_helper.rb