china_regions 0.4.3 → 0.4.4

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
- SHA1:
3
- metadata.gz: cd71beadef1880c9f002ead5302f05619ac90b3b
4
- data.tar.gz: e5bbe35869c2e8e91e57999f463e7738a1810055
2
+ SHA256:
3
+ metadata.gz: 355fcf16235b294a0555cd4e759320ee12486509cef20405672bdaaedbf958bd
4
+ data.tar.gz: 4fdcee628fb6af5057cb20771a05b5bd2f4ab13b100715473d3d567d917f4504
5
5
  SHA512:
6
- metadata.gz: 0aa1c5abb2ac0bf8e100dcbb9f2c35edd7a94b6208a4cb595017a6baeafc7f9820a2ef00888890e98934f8897e00d739abbecbe07acdc527ad92aad7b6c057ed
7
- data.tar.gz: ad06057a462ce2cda7b8569439d5306352b8a1d07016a6a0aebf071ab1579275b7e3199f8267f1330ee0b90231f29a125d8178cb833783708cb339daf7efb1bf
6
+ metadata.gz: a433388666d30dcf2177653ade0bd6523e5a50151d2f54b3f06d6d553f4a21df8d9ec63eb066e7bbc64694d772456868f6d5fae937d27cc805130b006f19c37f
7
+ data.tar.gz: a26988d21b27a1898b6f17ccefd2cadbb5af352cc00205294954f02afa3f01b741f76e9cd10d7fe9dd16e3d7f033fd9e964eeeb1c030c063628eb2ca5d56f267
data/.rubocop.yml ADDED
@@ -0,0 +1,88 @@
1
+ # This is the configuration used to check the rubocop source code.
2
+
3
+ AllCops:
4
+ DisplayCopNames: true
5
+ TargetRubyVersion: 2.5.1
6
+ Exclude:
7
+ - '**/*.yml'
8
+ - 'db/**/*'
9
+ - 'config/**/*'
10
+ - 'doc/**/*'
11
+ - 'deployment/**/*'
12
+ - 'bin/**/*'
13
+ - 'tmp/**/*'
14
+ - 'lib/china_regions/helpers/form_helper.rb'
15
+
16
+ Style/StringLiterals:
17
+ Enabled: false
18
+
19
+ Documentation:
20
+ Enabled: false
21
+
22
+ Style/RegexpLiteral:
23
+ Enabled: false
24
+
25
+ Layout/SpaceInsideHashLiteralBraces:
26
+ Enabled: false
27
+
28
+ Style/FormatString:
29
+ Enabled: false
30
+
31
+ Lint/AssignmentInCondition:
32
+ Enabled: false
33
+
34
+ Layout/EmptyLinesAroundAccessModifier:
35
+ Enabled: false
36
+
37
+ Style/SingleLineBlockParams:
38
+ Enabled: false
39
+
40
+ Style/NumericLiterals:
41
+ Exclude:
42
+ - 'spec/**/*'
43
+
44
+ Style/StringLiteralsInInterpolation:
45
+ Enabled: false
46
+
47
+ Layout/CaseIndentation:
48
+ EnforcedStyle: end
49
+ IndentOneStep: false
50
+
51
+ Layout/EndAlignment:
52
+ EnforcedStyleAlignWith: variable
53
+
54
+ Lint/UselessAssignment:
55
+ Enabled: false
56
+
57
+ Style/NumericLiterals:
58
+ Enabled: false
59
+
60
+ Metrics/AbcSize:
61
+ Max: 56
62
+
63
+ Metrics/CyclomaticComplexity:
64
+ Max: 10
65
+
66
+ Metrics/ModuleLength:
67
+ Max: 156
68
+
69
+ Metrics/PerceivedComplexity:
70
+ Max: 15
71
+
72
+ Metrics/LineLength:
73
+ Max: 160
74
+
75
+ Metrics/MethodLength:
76
+ Max: 24
77
+
78
+ Metrics/ClassLength:
79
+ Enabled: false
80
+
81
+ Metrics/ParameterLists:
82
+ Max: 10
83
+
84
+ Metrics/BlockLength:
85
+ Max: 100
86
+
87
+ Security/MarshalLoad:
88
+ Enabled: false
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  # Specify your gem's dependencies in china_regions.gemspec
data/README.md CHANGED
@@ -1,53 +1,62 @@
1
1
  # ChinaRegions
2
2
 
3
- 中国省份,城市,地区[地级市]
4
- Ruby on Rails 程式代码, Ruby ( > 1.9.x ) And Rails (> 4.0)
3
+ 中国省份,城市,地区[地级市]Ruby on Rails 程式代码, Ruby (> 1.9.x) And Rails (> 4.0)
5
4
 
6
- ## How to update
5
+ ## 如何更新数据文件
7
6
 
8
7
  If you are using ChinaRegions version 0.1.x be sure to run:
9
8
 
10
- rails g china_regions:regions
9
+ >> rails g china_regions:regions
11
10
 
12
11
  to have the javascript file copied over into your project.
13
12
 
14
- ## How to use it
13
+ ## 如何引入china_regions到你的项目
15
14
 
16
15
  添加以下代码到你的 Gemfile:
17
16
 
18
- gem 'china_regions'
17
+ gem 'china_regions'
19
18
 
20
19
  OR
21
20
 
22
- gem 'china_regions', github: 'encoreshao/china_regions'
21
+ gem 'china_regions', github: 'encoreshao/china_regions'
23
22
 
24
- bundle install
23
+ 安装:
25
24
 
26
- 执行:
25
+ >> bundle install
27
26
 
28
- rails g china_regions:install
27
+ ### 开始构建城市数据
28
+
29
+ 复制所需文件到你的项目中:
30
+
31
+ >> rails g china_regions:install
29
32
 
30
33
  随后你可以看到控制台发生的变化:
31
34
  * 复制 db/migrate/xxxxxxxxxxx_create_china_regions_tables.rb 文件到db/migrate 目录
32
35
  * 复制 数据源 cities.yml 到 config 目录. config/cities.yml
33
36
  * 复制 regions.en.yml 和 regions.zh.yml 文件到 config/locales 目录
34
- * 执行 `rake db:migrate` 创建所需的表 (provinces, cities, districts).
35
- * 执行 `rake china_regions:import` 导入数据.
37
+
38
+ 创建所需的表 (provinces, cities, districts):
39
+
40
+ >> rake db:migrate
41
+
42
+ 导入数据到对应表中:
43
+
44
+ >> rake china_regions:import
36
45
 
37
46
 
38
- 此时 你可能需要添加三个 models[`Province`, `City`, `District`] 到你应用中:
47
+ 将所需的模型(Models) [`Province`, `City`, `District`] 到您的应用程式中:
39
48
 
40
49
  你可以执行 `rails g` 查看到 generator LIST.
41
50
 
42
- 执行 rails g china_regions:regions
51
+ >> rails g china_regions:regions
43
52
 
44
- 查看 app/models:
53
+ 查看 app/models:
45
54
 
46
55
  create app/models/province.rb
47
56
  create app/models/city.rb
48
57
  create app/models/district.rb
49
58
 
50
- ## How to view
59
+ ## 如何在View中使用
51
60
 
52
61
  范例:
53
62
 
@@ -76,7 +85,7 @@ bundle install
76
85
  = f.region_select [:province, :city, :district], :prefix => "work"
77
86
 
78
87
 
79
- Preselect Province:
88
+ 预选则省份:
80
89
 
81
90
  = form_for @article do |f|
82
91
 
@@ -86,7 +95,7 @@ Preselect Province:
86
95
 
87
96
  = f.region_select [:province, :city, :district], province: "重庆市"
88
97
 
89
- Prioritize Choice:
98
+ 优先选择:
90
99
 
91
100
  = form_for @article do |f|
92
101
 
data/Rakefile CHANGED
@@ -1,2 +1,4 @@
1
1
  #!/usr/bin/env rake
2
- require 'bundler/gem_tasks'
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/gem_tasks'
@@ -1,31 +1,31 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module ChinaRegions
4
4
  class FetchOptionsController < ::ActionController::Metal
5
-
6
5
  def index
7
- if params_valid?(params) and parent_klass = params[:parent_klass].classify.safe_constantize.find(params[:parent_id])
6
+ if params_valid?(params) && (parent_klass = params[:parent_klass].classify.safe_constantize.find(params[:parent_id]))
8
7
  table_name = params[:klass].tableize
9
8
  regions = parent_klass.__send__(table_name).select("#{table_name}.id, #{table_name}.name")
10
- if has_level_column?(params[:klass])
11
- regions = regions.order('level ASC')
9
+ regions = if level_column?(params[:klass])
10
+ regions.order('level ASC')
12
11
  else
13
- regions = regions.order('name ASC')
12
+ regions.order('name ASC')
14
13
  end
15
- self.response_body = regions.to_json
16
14
  else
17
- self.response_body = [].to_json
15
+ regions = []
18
16
  end
17
+
18
+ self.response_body = regions.to_json
19
19
  end
20
20
 
21
21
  protected
22
- def has_level_column?(klass_name)
22
+
23
+ def level_column?(klass_name)
23
24
  klass_name.classify.safe_constantize.try(:column_names).to_a.include?('level')
24
25
  end
25
26
 
26
27
  def params_valid?(params)
27
- params[:klass].present? and params[:parent_klass] =~ /^province|city$/i and params[:parent_id].present?
28
+ params[:klass].present? && params[:parent_klass] =~ /^province|city$/i && params[:parent_id].present?
28
29
  end
29
-
30
30
  end
31
31
  end
data/app/models/city.rb CHANGED
@@ -1,7 +1,6 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  class City < ActiveRecord::Base
4
-
5
4
  belongs_to :province
6
5
  has_many :districts, dependent: :destroy
7
6
 
@@ -12,7 +11,6 @@ class City < ActiveRecord::Base
12
11
  end
13
12
 
14
13
  def siblings
15
- @siblings ||= where(nil).with_province(self.province_id)
14
+ @siblings ||= where(nil).with_province(province_id)
16
15
  end
17
-
18
16
  end
@@ -1,7 +1,6 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  class District < ActiveRecord::Base
4
-
5
4
  belongs_to :city
6
5
 
7
6
  scope :with_city, ->(city) { where(city_id: city) }
@@ -15,7 +14,6 @@ class District < ActiveRecord::Base
15
14
  end
16
15
 
17
16
  def siblings
18
- @siblings ||= where(nil).with_city(self.city_id)
17
+ @siblings ||= where(nil).with_city(city_id)
19
18
  end
20
-
21
19
  end
@@ -1,8 +1,6 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  class Province < ActiveRecord::Base
4
-
5
4
  has_many :cities, dependent: :destroy
6
5
  has_many :districts, through: :cities
7
-
8
6
  end
@@ -1,19 +1,23 @@
1
- # encoding: utf-8
2
- require File.expand_path('../lib/china_regions/version', __FILE__)
1
+
2
+ # frozen_string_literal: true
3
+
4
+ require File.expand_path('lib/china_regions/version', __dir__)
3
5
 
4
6
  Gem::Specification.new do |gem|
5
- gem.authors = ["Encore Shao", "Trey Springer"]
6
- gem.email = ["encore.shao@gmail.com", "dspringer@enova.com"]
7
- gem.description = %q{ China regions Ruby on rails interface }
8
- gem.summary = %q{ Rails 4 version of dropdowns for all provinces, cities, and districts in China. }
9
- gem.homepage = "http://github.com/encoreshao"
7
+ gem.authors = ['Encore Shao']
8
+ gem.email = ['encore.shao@gmail.com']
9
+ gem.description = 'China regions Ruby on rails interface'
10
+ gem.summary = 'Rails 4 version of dropdowns for all provinces, cities,
11
+ and districts in China.'
12
+ gem.homepage = 'http://github.com/encoreshao'
10
13
 
11
- gem.files = `git ls-files`.split($\)
12
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
+ gem.files = `git ls-files`.split($OUTPUT_RECORD_SEPARATOR)
15
+ gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
13
16
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
- gem.name = "china_regions"
15
- gem.require_paths = ["lib"]
17
+ gem.name = 'china_regions'
18
+ gem.require_paths = ['lib']
16
19
  gem.version = ChinaRegions::VERSION
17
20
 
18
21
  gem.add_dependency 'jquery-rails'
22
+ gem.add_development_dependency 'rubocop'
19
23
  end
data/config/routes.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  Rails.application.routes.draw do
2
4
  match '/china_regions/fetch_options' => ChinaRegions::FetchOptionsController.action(:index), via: [:get]
3
5
  end
@@ -1,7 +1,9 @@
1
- require "rails"
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails'
2
4
 
3
5
  module ChinaRegions
4
- if ::Rails.version > "3.1"
6
+ if ::Rails.version > '3.1'
5
7
  class Engine < ::Rails::Engine; end
6
8
  else
7
9
  class Railtie < ::Rails::Railtie
@@ -1,22 +1,24 @@
1
- # encoding: utf-8
1
+
2
+ # frozen_string_literal: true
3
+
2
4
  module ChinaRegions
3
5
  module Helpers
4
6
  module FormHelper
5
7
  def region_select(object_name, methods, options = {}, html_options = {})
6
8
  output = ''
7
9
 
8
- preselected_choices = set_regions_options(options)
10
+ preselected_choices = setup_regions_options(options)
9
11
 
10
12
  html_options[:class] ?
11
- (html_options[:class].prepend('region_select ')) :
13
+ html_options[:class].prepend('region_select ') :
12
14
  (html_options[:class] = 'region_select')
13
15
 
14
- dropdown_prefix = options[:prefix] ? options[:prefix].to_s + "_" : ""
16
+ dropdown_prefix = options[:prefix] ? options[:prefix].to_s + '_' : ''
15
17
 
16
- if Array === methods
18
+ if Array == methods
17
19
  methods.each_with_index do |method, index|
18
20
  if region_klass = method.to_s.classify.safe_constantize
19
- choices = get_choices(region_klass, method, preselected_choices, index)
21
+ choices = fetch_choices(region_klass, method, preselected_choices, index)
20
22
  choices = prioritize_choices(options[:priority][method], choices) if options[:priority].try(:[], method)
21
23
 
22
24
  next_method = methods.at(index + 1)
@@ -28,28 +30,26 @@ module ChinaRegions
28
30
  options[:selected] = options[:default][method] if options[:default][method]
29
31
  end
30
32
 
31
- output << select(object_name, "#{dropdown_prefix}#{method.to_s}_id", choices, options, html_options)
33
+ output << select(object_name, "#{dropdown_prefix}#{method}_id", choices, options, html_options)
32
34
  else
33
35
  raise "Method '#{method}' is not a vaild attribute of #{object_name}"
34
36
  end
35
37
  end
36
38
  else
37
- _methods = unless methods.to_s.include?('_id')
38
- (methods.to_s + ('_id')).to_sym
39
- else
40
- _methods = methods
39
+ inner_methods = if methods.to_s.include?('_id')
40
+ inner_methods = methods
41
41
  methods = methods.to_s.gsub(/(_id)$/, '')
42
- _methods
42
+ inner_methods
43
+ else
44
+ (methods.to_s + '_id').to_sym
43
45
  end
44
46
 
45
47
  if region_klass = methods.to_s.classify.safe_constantize
46
48
  options[:prompt] = region_prompt(region_klass)
47
49
 
48
- if methods == :province && preselected_choices[:province_id]
49
- options[:selected] = preselected_choices[:province_id]
50
- end
50
+ options[:selected] = preselected_choices[:province_id] if methods == :province && preselected_choices[:province_id]
51
51
 
52
- output << select(object_name, _methods, region_klass.where(nil).collect {|p| [ p.name, p.id ] }, options = options, html_options = html_options)
52
+ output << select(object_name, inner_methods, region_klass.where(nil).collect { |p| [p.name, p.id] }, options = options, html_options = html_options)
53
53
  else
54
54
  raise "Method '#{method}' is not a vaild attribute of #{object_name}"
55
55
  end
@@ -58,15 +58,11 @@ module ChinaRegions
58
58
  end
59
59
 
60
60
  private
61
-
62
- def get_choices(region_klass, method, preselected_choices, index)
61
+ def fetch_choices(region_klass, method, preselected_choices, index)
63
62
  return preselected_choices[method] if preselected_choices[method]
63
+ return [] unless index.zero?
64
64
 
65
- if index == 0
66
- region_klass.where(nil).collect { |p| [p.name, p.id] }
67
- else
68
- []
69
- end
65
+ region_klass.where(nil).collect { |p| [p.name, p.id] }
70
66
  end
71
67
 
72
68
  def prioritize_choices(priorities, choices)
@@ -90,27 +86,26 @@ module ChinaRegions
90
86
  priority_choices.compact + temp_choices
91
87
  end
92
88
 
93
- def set_regions_options(options)
89
+ def setup_regions_options(options)
94
90
  return {} unless options[:default] && options[:default][:province]
95
91
 
96
- #TODO: Add validator to check if the passed province, city or district exists within the models
97
-
92
+ # TODO: Add validator to check if the passed province, city or district exists within the models
98
93
  province_id = get_province_id(options[:default][:province])
99
94
  cities = City.where(province_id: province_id)
100
95
  districts = District.where(city_id: cities)
101
96
 
102
97
  {
103
98
  province_id: province_id,
104
- city: cities.collect { |c| [ c.name, c.id ] },
105
- district: districts.collect { |d| [ d.name, d.id ] }
99
+ city: cities.collect { |c| [c.name, c.id] },
100
+ district: districts.collect { |d| [d.name, d.id] }
106
101
  }
107
102
  end
108
103
 
109
104
  def set_prompt(method, options, region_klass)
110
- if respond_to?("#{method}_select_prompt")
111
- options[:prompt] = __send__("#{method}_select_prompt")
105
+ options[:prompt] = if respond_to?("#{method}_select_prompt")
106
+ __send__("#{method}_select_prompt")
112
107
  else
113
- options[:prompt] = region_prompt(region_klass)
108
+ region_prompt(region_klass)
114
109
  end
115
110
  end
116
111
 
@@ -120,11 +115,11 @@ module ChinaRegions
120
115
  end
121
116
 
122
117
  def set_html_options(object_name, method, html_options, next_region, prefix)
123
- html_options[:data] ? (html_options[:data][:region_klass] = "#{method.to_s}") : (html_options[:data] = { region_klass: "#{method.to_s}" })
118
+ html_options[:data] ? (html_options[:data][:region_klass] = method.to_s) : (html_options[:data] = { region_klass: method.to_s })
124
119
  if next_region
125
- object_name = object_name.dup.gsub(/\[/, '_')
126
- object_name = object_name.dup.gsub(/\]/, '')
127
- html_options[:data].merge!(region_target: "#{object_name}_#{prefix}#{next_region.to_s}_id", region_target_klass: next_region.to_s)
120
+ object_name = object_name.dup.tr('[', '_')
121
+ object_name = object_name.dup.delete(']')
122
+ html_options[:data].merge!(region_target: "#{object_name}_#{prefix}#{next_region}_id", region_target_klass: next_region.to_s)
128
123
  else
129
124
  html_options[:data].delete(:region_target)
130
125
  html_options[:data].delete(:region_target_klass)
@@ -141,7 +136,6 @@ module ChinaRegions
141
136
  @template.region_select(@object_name.to_s, methods, options = options, html_options = html_options)
142
137
  end
143
138
  end
144
-
145
139
  end
146
140
  end
147
141
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ChinaRegions
2
- VERSION = "0.4.3"
4
+ VERSION = '0.4.4'
3
5
  end
data/lib/china_regions.rb CHANGED
@@ -1,5 +1,7 @@
1
- require "china_regions/version"
1
+ # frozen_string_literal: true
2
+
3
+ require 'china_regions/version'
2
4
  require 'china_regions/engine' if defined? Rails
3
- require "china_regions/helpers/form_helper"
5
+ require 'china_regions/helpers/form_helper'
4
6
 
5
- module ChinaRegions;end
7
+ module ChinaRegions; end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+
5
+ namespace :china_regions do
6
+ desc 'Import regions to database from config/cities.yml'
7
+ task import: :environment do
8
+ file_path = File.join(Rails.root, 'config', 'cities.yml')
9
+ data = File.open(file_path) { |file| YAML.safe_load(file) }
10
+ remove_china_regions && load_to_db(data)
11
+
12
+ puts "\n China's provinces, city, region data import is complete."
13
+ end
14
+
15
+ def remove_china_regions
16
+ Province.delete_all && City.delete_all && District.delete_all
17
+ end
18
+
19
+ def load_to_db(data)
20
+ data.each do |province_name, province_hash|
21
+ province = creating_province(province_name, province_hash)
22
+
23
+ province_hash['cities'].each do |city_name, city_hash|
24
+ city = creating_city(province.id, city_name, city_hash)
25
+
26
+ creating_districts(city.id, city_hash)
27
+ end
28
+ print '.'
29
+ end
30
+ end
31
+
32
+ def creating_province(province_name, province_hash)
33
+ province_parameters = province_params(
34
+ name: province_name,
35
+ name_en: province_hash['name_en'],
36
+ name_abbr: province_hash['name_abbr']
37
+ )
38
+
39
+ Province.create(province_parameters)
40
+ end
41
+
42
+ def creating_city(province_id, city_name, city_hash)
43
+ city_parameters = city_params(
44
+ province_id: province_id,
45
+ name: city_name,
46
+ name_en: city_hash['name_en'],
47
+ name_abbr: city_hash['name_abbr'],
48
+ zip_code: city_hash['zip_code'],
49
+ level: city_hash['level'] || 4
50
+ )
51
+
52
+ City.create(city_parameters)
53
+ end
54
+
55
+ def creating_districts(city_id, city_hash)
56
+ districts_hash = city_hash['districts']
57
+
58
+ districts_hash.each do |district_name, district_hash|
59
+ creating_district(city_id, district_name, district_hash)
60
+ end
61
+ end
62
+
63
+ def creating_district(city_id, district_name, district_hash)
64
+ district_parameters = district_params(
65
+ city_id: city_id,
66
+ name: district_name,
67
+ name_en: district_hash['name_en'],
68
+ name_abbr: district_hash['name_abbr']
69
+ )
70
+ District.create(district_parameters)
71
+ end
72
+
73
+ private
74
+ def province_params(raw_parameters)
75
+ parameters = ActionController::Parameters.new(raw_parameters)
76
+ parameters.permit(:name, :name_en, :name_abbr)
77
+ end
78
+
79
+ def city_params(raw_parameters)
80
+ parameters = ActionController::Parameters.new(raw_parameters)
81
+ parameters.permit(:province_id, :name, :name_en, :name_abbr, :zip_code, :level)
82
+ end
83
+
84
+ def district_params(raw_parameters)
85
+ parameters = ActionController::Parameters.new(raw_parameters)
86
+ parameters.permit(:city_id, :name, :name_en, :name_abbr)
87
+ end
88
+ end
@@ -1,49 +1,46 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module ChinaRegions
4
4
  class InstallGenerator < Rails::Generators::Base
5
5
  include Rails::Generators::Migration
6
- source_root File.expand_path('../templates', __FILE__)
6
+ source_root File.expand_path('templates', __dir__)
7
7
 
8
8
  def copy_migration
9
- Dir["db/migrate/*_china_regions_tables.rb"].each{ |file| File.delete(file) }
9
+ Dir['db/migrate/*_china_regions_tables.rb'].each { |file| File.delete(file) }
10
10
 
11
- migration_template "migration.rb", "db/migrate/create_china_regions_tables.rb"
11
+ migration_template 'migration.rb', 'db/migrate/create_china_regions_tables.rb'
12
12
  end
13
13
 
14
14
  def copy_cities
15
- copy_file('cities.yml', 'config/cities.yml') unless File::exists?("config/cities.yml")
15
+ copy_file('cities.yml', 'config/cities.yml') unless File.exist?('config/cities.yml')
16
16
  end
17
17
 
18
18
  def copy_locales
19
- unless File::exists?("config/locales/regions.en.yml")
20
- copy_file "../../../../config/locales/en.yml", "config/locales/regions.en.yml"
21
- end
22
- unless File::exists?("config/locales/regions.zh.yml")
23
- copy_file "../../../../config/locales/zh.yml", "config/locales/regions.zh.yml"
24
- end
19
+ copy_file '../../../../config/locales/en.yml', 'config/locales/regions.en.yml' unless File.exist?('config/locales/regions.en.yml')
20
+ copy_file '../../../../config/locales/zh.yml', 'config/locales/regions.zh.yml' unless File.exist?('config/locales/regions.zh.yml')
25
21
  end
26
22
 
27
23
  def copy_rake_tasks
28
- unless File::exists?("lib/tasks/china_regions.rake")
29
- copy_file "../../../../lib/tasks/china_regions.rake", "lib/tasks/china_regions.rake"
30
- end
31
- end
32
-
33
- def execute_migrate
34
- rake("db:migrate")
35
- end
36
-
37
- def import_cities_to_database
38
- rake('china_regions:import')
24
+ copy_file '../../../../lib/custom_tasks/china_regions.rake', 'lib/tasks/china_regions.rake' unless File.exist?('lib/tasks/china_regions.rake')
39
25
  end
40
26
 
41
27
  def self.next_migration_number(dirname)
42
28
  if ActiveRecord::Base.timestamped_migrations
43
- Time.now.utc.strftime("%Y%m%d%H%M%S")
29
+ Time.now.utc.strftime('%Y%m%d%H%M%S')
44
30
  else
45
- "%.3d" % (current_migration_number(dirname) + 1)
31
+ format('%.3d', (current_migration_number(dirname) + 1))
46
32
  end
47
33
  end
34
+
35
+ def add_rails_version_for_migration
36
+ rails_version = Gem.loaded_specs["activesupport"].version
37
+ return if rails_version < Gem::Version.create('4.0')
38
+
39
+ migration_version = rails_version.to_s.split(/\./)[0..1].join('.')
40
+ filename = 'db/migrate/*create_china_regions_tables.rb'
41
+ kclass = 'ActiveRecord::Migration'
42
+
43
+ system(`grep -rl "#{kclass}$" #{filename} | xargs sed -i "" "s/#{kclass}/#{kclass}[#{migration_version}]/g"`)
44
+ end
48
45
  end
49
46
  end
@@ -1,17 +1,17 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module ChinaRegions
4
4
  class RegionsGenerator < Rails::Generators::Base
5
- source_root File.expand_path('../../../../app', __FILE__)
5
+ source_root File.expand_path('../../../app', __dir__)
6
6
 
7
7
  def copy_models_file
8
- copy_file "models/province.rb", "app/models/province.rb"
9
- copy_file "models/city.rb", "app/models/city.rb"
10
- copy_file "models/district.rb", "app/models/district.rb"
8
+ copy_file 'models/province.rb', 'app/models/province.rb'
9
+ copy_file 'models/city.rb', 'app/models/city.rb'
10
+ copy_file 'models/district.rb', 'app/models/district.rb'
11
11
  end
12
12
 
13
13
  def copy_js_file
14
- copy_file "assets/javascripts/region_select.js", "app/assets/javascripts/region_select.js"
14
+ copy_file 'assets/javascripts/region_select.js', 'app/assets/javascripts/region_select.js'
15
15
  end
16
16
  end
17
17
  end
@@ -1,44 +1,48 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  class CreateChinaRegionsTables < ActiveRecord::Migration
4
4
  def change
5
- unless table_exists? 'provinces'
6
- create_table :provinces do |t|
7
- t.string :name
8
- t.string :name_en
9
- t.string :name_abbr
5
+ setup_provinces unless table_exists? 'provinces'
6
+ setup_citites unless table_exists? 'cities'
7
+ setup_districts unless table_exists? 'districts'
8
+ end
10
9
 
11
- t.timestamps
12
- end
10
+ def setup_provinces
11
+ create_table :provinces do |t|
12
+ t.string :name
13
+ t.string :name_en
14
+ t.string :name_abbr
13
15
 
14
- add_index :provinces, :name
16
+ t.timestamps
15
17
  end
16
18
 
17
- unless table_exists? 'cities'
18
- create_table :cities do |t|
19
- t.string :name
20
- t.integer :province_id
21
- t.integer :level
22
- t.string :zip_code
23
- t.string :name_en
24
- t.string :name_abbr
19
+ add_index :provinces, :name
20
+ end
25
21
 
26
- t.timestamps
27
- end
22
+ def setup_citites
23
+ create_table :cities do |t|
24
+ t.string :name
25
+ t.integer :province_id
26
+ t.integer :level
27
+ t.string :zip_code
28
+ t.string :name_en
29
+ t.string :name_abbr
28
30
 
29
- add_index :cities, :name
31
+ t.timestamps
30
32
  end
31
33
 
32
- unless table_exists? 'districts'
33
- create_table :districts do |t|
34
- t.string :name
35
- t.integer :city_id
36
- t.string :name_en
37
- t.string :name_abbr
34
+ add_index :cities, :name
35
+ end
36
+
37
+ def setup_districts
38
+ create_table :districts do |t|
39
+ t.string :name
40
+ t.integer :city_id
41
+ t.string :name_en
42
+ t.string :name_abbr
38
43
 
39
- t.timestamps
40
- end
41
- add_index :districts, :name
44
+ t.timestamps
42
45
  end
46
+ add_index :districts, :name
43
47
  end
44
48
  end
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: china_regions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Encore Shao
8
- - Trey Springer
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2016-07-17 00:00:00.000000000 Z
11
+ date: 2018-06-03 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: jquery-rails
@@ -25,15 +24,29 @@ dependencies:
25
24
  - - ">="
26
25
  - !ruby/object:Gem::Version
27
26
  version: '0'
28
- description: " China regions Ruby on rails interface "
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: China regions Ruby on rails interface
29
42
  email:
30
43
  - encore.shao@gmail.com
31
- - dspringer@enova.com
32
44
  executables: []
33
45
  extensions: []
34
46
  extra_rdoc_files: []
35
47
  files:
36
48
  - ".gitignore"
49
+ - ".rubocop.yml"
37
50
  - Gemfile
38
51
  - LICENSE
39
52
  - README.md
@@ -51,11 +64,11 @@ files:
51
64
  - lib/china_regions/engine.rb
52
65
  - lib/china_regions/helpers/form_helper.rb
53
66
  - lib/china_regions/version.rb
67
+ - lib/custom_tasks/china_regions.rake
54
68
  - lib/generators/china_regions/install_generator.rb
55
69
  - lib/generators/china_regions/regions_generator.rb
56
70
  - lib/generators/china_regions/templates/cities.yml
57
71
  - lib/generators/china_regions/templates/migration.rb
58
- - lib/tasks/china_regions.rake
59
72
  homepage: http://github.com/encoreshao
60
73
  licenses: []
61
74
  metadata: {}
@@ -75,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
88
  version: '0'
76
89
  requirements: []
77
90
  rubyforge_project:
78
- rubygems_version: 2.4.5
91
+ rubygems_version: 2.7.6
79
92
  signing_key:
80
93
  specification_version: 4
81
94
  summary: Rails 4 version of dropdowns for all provinces, cities, and districts in
@@ -1,70 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'yaml'
4
-
5
- namespace :china_regions do
6
- desc "Import regions to database from config/cities.yml"
7
-
8
- task :import => :environment do
9
- file_path = File.join(Rails.root, 'config', 'cities.yml')
10
- data = File.open(file_path) { |file| YAML.load(file) }
11
- remove_china_regions && load_to_db(data)
12
-
13
- puts "\n China's provinces, city, region data import is complete."
14
- end
15
-
16
- def remove_china_regions
17
- Province.delete_all && City.delete_all && District.delete_all
18
- end
19
-
20
- def load_to_db(data)
21
- data.each do |province_name, province_hash|
22
- province_parameters = province_params({
23
- name: province_name,
24
- name_en: province_hash['name_en'],
25
- name_abbr: province_hash['name_abbr']
26
- })
27
- province = Province.create(province_parameters)
28
-
29
- province_hash['cities'].each do |city_name, city_hash|
30
- city_parameters = city_params({
31
- province_id: province.id,
32
- name: city_name,
33
- name_en: city_hash['name_en'],
34
- name_abbr: city_hash['name_abbr'],
35
- zip_code: city_hash['zip_code'],
36
- level: city_hash['level'] || 4
37
- })
38
- city = City.create(city_parameters)
39
-
40
- districts_hash = city_hash['districts']
41
- districts_hash.each do |district_name, district_hash|
42
- district_parameters = district_params({
43
- city_id: city.id,
44
- name: district_name,
45
- name_en: district_hash['name_en'],
46
- name_abbr: district_hash['name_abbr']
47
- })
48
- District.create(district_parameters)
49
- end
50
- end
51
- print "."
52
- end
53
- end
54
-
55
- private
56
- def province_params(raw_parameters)
57
- parameters = ActionController::Parameters.new(raw_parameters)
58
- parameters.permit(:name, :name_en, :name_abbr)
59
- end
60
-
61
- def city_params(raw_parameters)
62
- parameters = ActionController::Parameters.new(raw_parameters)
63
- parameters.permit(:province_id, :name, :name_en, :name_abbr, :zip_code, :level)
64
- end
65
-
66
- def district_params(raw_parameters)
67
- parameters = ActionController::Parameters.new(raw_parameters)
68
- parameters.permit(:city_id, :name, :name_en, :name_abbr)
69
- end
70
- end