taiwan_city_dists_helper 0.93 → 0.95

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
- SHA1:
3
- metadata.gz: a3abe3b0a7302375b9b266062e1bebd8d7071d1e
4
- data.tar.gz: f1a1cdb1df008df6809adac4b8656ad6d9116206
2
+ SHA256:
3
+ metadata.gz: eab05e705bf22ad4d5c0a23840f69eb19bc2b990a8ce8c2d683aefe705b4ba52
4
+ data.tar.gz: 6e057c4cbac443eea3a158ecf4e09ecb9dbeb937713c6b42041bb45ceafe9cba
5
5
  SHA512:
6
- metadata.gz: 25d8397ed40af440fdf8e95d1fd4395318e2913d1f27e9006f26cf2703c7feed682f8fd8c842fa967ea215f11da24cc1cb575fa6fe70850bc2cd82fa4d083be3
7
- data.tar.gz: 84e57ad82dbd85262c05f7a3d5ffa6e8eb5869ba180fa9bf231f92d23537493810fe47b191b7c49a530086f576ff72bd3a87f633990295aa1910271eee6fa11a
6
+ metadata.gz: 39e902178e58308561cdd8cda65b80709cd6e35dec53023fac63ebb5a2f8ce0ad345f80cc84c9950b9a9096ce82299ee3f2f80516562d9fc834d519485eaf5a0
7
+ data.tar.gz: 49fdb51e5d440d46db709faf7d6eb648510e79d0ebc1bcf7d4d06057376457e18210de5532e002c4c06be3d96dab4e12569dcc3d3083eae39d1e7829bcba13f8
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Generate Taiwan City and Districts models with dynamic select boxes
4
4
 
5
- 輕鬆產生台灣鄉鎮市的 model 以及動態 select box
5
+ 輕鬆產生台灣鄉鎮市的 model relation 以及動態 select box
6
6
 
7
7
  ## Installation
8
8
 
@@ -12,14 +12,14 @@ Add this line to your application's Gemfile:
12
12
 
13
13
  And then execute:
14
14
 
15
- $ bundle
15
+ $ bundle install
16
16
 
17
17
  ## Preparation
18
18
 
19
- 1. rails g taiwan_city_dists_helper:table
20
- 2. rake db:migrate
21
- 3. rake taiwan_city_dists_helper:get_cities_and_dists
22
- 4. add //= require taiwan_city_dists_helper to your application.js
19
+ 1. bundle exec rails g tcdh:table
20
+ 2. bundle exec rails db:migrate
21
+ 3. bundle exec rails tcdh:fetch
22
+ 4. add //= require taiwan_city_dists_helper to your application.js ( webpacker not supported )
23
23
 
24
24
  Make sure your object ( let's say Customer ) has relations with city and dist, so you have to
25
25
  add city\_id and dist\_id to your customers table, and define belongs_to in your customer.rb
data/Rakefile CHANGED
@@ -1 +1 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
@@ -1,4 +1,7 @@
1
- # -*- encoding : utf-8 -*-
1
+ # frozen_string_literal: true
2
+
3
+ # city model
2
4
  class City < ActiveRecord::Base
5
+ self.table_name = 'tcdh_cities'
3
6
  has_many :dists
4
7
  end
@@ -1,4 +1,7 @@
1
- # -*- encoding : utf-8 -*-
1
+ # frozen_string_literal: true
2
+
3
+ # dist model
2
4
  class Dist < ActiveRecord::Base
5
+ self.table_name = 'tcdh_dists'
3
6
  belongs_to :city
4
7
  end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+ require 'rails/generators/migration'
5
+ require 'rails/generators/active_record/migration'
6
+
7
+ module Tcdh
8
+ module Generators
9
+ # table generator
10
+ class TableGenerator < ::Rails::Generators::Base
11
+ include ::Rails::Generators::Migration
12
+
13
+ source_paths << File.join(File.dirname(__FILE__), 'templates')
14
+
15
+ def create_migration_file
16
+ migration_template 'migration.rb', 'db/migrate/create_cities_and_dists.rb'
17
+ end
18
+
19
+ def self.next_migration_number(_path)
20
+ @migration_number = Time.now.utc.strftime('%Y%m%d%H%M%S').to_i.to_s
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ # create cities and dists table
4
+ class CreateCitiesAndDists < ActiveRecord::Migration[4.2]
5
+ def self.up
6
+ create_table :tcdh_cities, force: true do |table|
7
+ table.string :name
8
+ table.timestamps
9
+ end
10
+
11
+ create_table :tcdh_dists, force: true do |table|
12
+ table.string :name
13
+ table.integer :city_id
14
+ table.timestamps
15
+ end
16
+ end
17
+
18
+ def self.down
19
+ drop_table :tcdh_cities
20
+ drop_table :tcdh_dists
21
+ end
22
+ end
@@ -1,7 +1,8 @@
1
- require "taiwan_city_dists_helper/version"
1
+ # frozen_string_literal: true
2
2
 
3
3
  module TaiwanCityDistsHelper
4
+ # injected in rails
4
5
  module Rails
5
- require "taiwan_city_dists_helper/engine"
6
+ require 'taiwan_city_dists_helper/engine'
6
7
  end
7
8
  end
@@ -1,9 +1,12 @@
1
- require "taiwan_city_dists_helper/helper"
1
+ # frozen_string_literal: true
2
+
3
+ require 'taiwan_city_dists_helper/helper'
2
4
 
3
5
  module TaiwanCityDistsHelper
4
6
  module Rails
7
+ # included in rails engine
5
8
  class Engine < ::Rails::Engine
6
- initializer "taiwan_city_dists_helper.view_helpers" do
9
+ initializer 'taiwan_city_dists_helper.view_helpers' do
7
10
  ActionView::Base.send :include, TaiwanCityDistsHelper::Helper
8
11
  end
9
12
  end
@@ -0,0 +1,11 @@
1
+ # require 'rake'
2
+
3
+ # module TaiwanCityDistsHelper
4
+ # class GemTasks
5
+ # include Rake::DSL if defined? Rake::DSL
6
+ # def install_tasks
7
+ # load 'tasks/fetch.rake'
8
+ # end
9
+ # end
10
+ # end
11
+ # TaiwanCityDistsHelper::GemTasks.new.install_tasks
@@ -1,16 +1,18 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module TaiwanCityDistsHelper
4
+ # view helpers
2
5
  module Helper
3
- def render_city_select(f, cities, options={}, html_options={})
4
- f.collection_select :city_id, cities, :id, :name, options, html_options
6
+ def render_city_select(form, cities, options = {}, html_options = {})
7
+ form.collection_select :city_id, cities, :id, :name, options, html_options
5
8
  end
6
9
 
7
- def render_grouped_dist_select(f, cities, options={}, html_options={})
8
- f.grouped_collection_select :dist_id, cities, :dists, :name, :id, :name, options, html_options
10
+ def render_grouped_dist_select(form, cities, options = {}, html_options = {})
11
+ form.grouped_collection_select :dist_id, cities, :dists, :name, :id, :name, options, html_options
9
12
  end
10
13
 
11
- def render_dist_select(f, dists, options={}, html_options={})
12
- f.collection_select :dist_id, dists, :id, :name, options, html_options
14
+ def render_dist_select(form, dists, options = {}, html_options = {})
15
+ form.collection_select :dist_id, dists, :id, :name, options, html_options
13
16
  end
14
-
15
17
  end
16
- end
18
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'nokogiri'
4
+ require 'open-uri'
5
+ require 'fileutils'
6
+ require 'ruby-progressbar'
7
+
8
+ namespace :tcdh do
9
+ desc 'Fetch cities and dists from OpenData API'
10
+ task fetch: :environment do
11
+ progressbar = ProgressBar.create(total: 368, format: '%t: |%B| %c/%C')
12
+ county_url = 'https://api.nlsc.gov.tw/other/ListCounty'
13
+ town_url = 'https://api.nlsc.gov.tw/other/ListTown1/'
14
+ progressbar.log '連線至 https://api.nlsc.gov.tw/other/ListCounty 取得縣市資料'
15
+ counties = Nokogiri::XML(URI.parse(county_url).open)
16
+ counties.css('countyItem').each do |county|
17
+ city_name = county.css('countyname').text
18
+ city_code = county.css('countycode').text
19
+ progressbar.log "建立 #{city_name} 記錄"
20
+ city = City.create(name: city_name)
21
+ progressbar.log "連線至 https://api.nlsc.gov.tw/other/ListTown1/#{city_code} 取得 #{city_name} 資料"
22
+ towns = Nokogiri::XML(URI.parse(town_url + city_code).open)
23
+ towns.css('townItem').each do |dist|
24
+ dist_name = dist.css('townname').text
25
+ progressbar.log "建立 #{dist_name} 記錄"
26
+ Dist.create(name: dist_name, city: city)
27
+ progressbar.increment
28
+ end
29
+ sleep(1)
30
+ end
31
+ end
32
+ end
@@ -1,20 +1,23 @@
1
- # -*- encoding: utf-8 -*-
2
- lib = File.expand_path('../lib', __FILE__)
1
+ # frozen_string_literal: true
2
+
3
+ # gemspec
4
+ lib = File.expand_path('/lib', __dir__)
3
5
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'taiwan_city_dists_helper/version'
5
6
 
6
7
  Gem::Specification.new do |gem|
7
- gem.name = "taiwan_city_dists_helper"
8
- gem.version = TaiwanCityDistsHelper::Rails::VERSION
9
- gem.authors = ["unayung"]
10
- gem.email = ["Unayung@gmail.com"]
11
- gem.description = %q{Generate Taiwan cities and dists model and provide dynamic select box helper}
12
- gem.summary = %q{This gem is a test}
13
- gem.homepage = "http://github.com/Unayung/taiwan_city_dists_helper"
8
+ gem.name = 'taiwan_city_dists_helper'
9
+ gem.version = '0.95'
10
+ gem.required_ruby_version = '>= 2.5.7'
11
+ gem.authors = 'unayung'
12
+ gem.email = 'Unayung@gmail.com'
13
+ gem.description = 'Generate Taiwan cities and dists model and provide dynamic select box helper'
14
+ gem.summary = 'This gem is a test'
15
+ gem.homepage = 'http://github.com/Unayung/taiwan_city_dists_helper'
14
16
 
15
- gem.add_dependency "nokogiri"
17
+ gem.add_dependency 'nokogiri'
18
+ gem.add_dependency 'ruby-progressbar'
16
19
  gem.files = `git ls-files`.split($/)
17
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
20
+ gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
18
21
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
- gem.require_paths = ["lib"]
22
+ gem.require_paths = ['lib']
20
23
  end
@@ -1,16 +1,16 @@
1
1
  $(function() {
2
2
  var dists;
3
3
  dists = $('select[id*="_dist_id"]').html();
4
- return $('select[id*="_city_id"]').change(function() {
4
+ $('select[id*="_city_id"]').change(function() {
5
5
  var city, escaped_city, options;
6
6
  city = $('select[id*="_city_id"] :selected').text();
7
7
  escaped_city = city.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1');
8
8
  if (dists.indexOf("optgroup") != -1){
9
9
  options = $(dists).filter("optgroup[label=" + escaped_city + "]").html();
10
10
  if (options) {
11
- return $('select[id*="_dist_id"]').html(options);
11
+ $('select[id*="_dist_id"]').html(options);
12
12
  } else {
13
- return $('select[id*="_dist_id"]').empty();
13
+ $('select[id*="_dist_id"]').empty();
14
14
  }
15
15
  }
16
16
  });
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: taiwan_city_dists_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.93'
4
+ version: '0.95'
5
5
  platform: ruby
6
6
  authors:
7
7
  - unayung
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-25 00:00:00.000000000 Z
11
+ date: 2020-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -24,10 +24,23 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: ruby-progressbar
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  description: Generate Taiwan cities and dists model and provide dynamic select box
28
42
  helper
29
- email:
30
- - Unayung@gmail.com
43
+ email: Unayung@gmail.com
31
44
  executables: []
32
45
  extensions: []
33
46
  extra_rdoc_files: []
@@ -39,15 +52,15 @@ files:
39
52
  - Rakefile
40
53
  - app/models/city.rb
41
54
  - app/models/dist.rb
42
- - lib/generators/taiwan_city_dists_helper/table/table_generator.rb
43
- - lib/generators/taiwan_city_dists_helper/table/templates/migration.rb
55
+ - lib/generators/tcdh/table/table_generator.rb
56
+ - lib/generators/tcdh/table/templates/migration.rb
44
57
  - lib/taiwan_city_dists_helper.rb
45
58
  - lib/taiwan_city_dists_helper/engine.rb
59
+ - lib/taiwan_city_dists_helper/gem_tasks.rb
46
60
  - lib/taiwan_city_dists_helper/helper.rb
47
- - lib/taiwan_city_dists_helper/version.rb
48
- - lib/tasks/taiwan_city_dists_helper.rake
61
+ - lib/tasks/fetch.rake
49
62
  - taiwan_city_dists_helper.gemspec
50
- - vendor/assets/javascripts/taiwan_city_dists_helper.js
63
+ - vendor/assets/javascripts/tcdh.js
51
64
  homepage: http://github.com/Unayung/taiwan_city_dists_helper
52
65
  licenses: []
53
66
  metadata: {}
@@ -59,15 +72,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
59
72
  requirements:
60
73
  - - ">="
61
74
  - !ruby/object:Gem::Version
62
- version: '0'
75
+ version: 2.5.7
63
76
  required_rubygems_version: !ruby/object:Gem::Requirement
64
77
  requirements:
65
78
  - - ">="
66
79
  - !ruby/object:Gem::Version
67
80
  version: '0'
68
81
  requirements: []
69
- rubyforge_project:
70
- rubygems_version: 2.5.1
82
+ rubygems_version: 3.0.3
71
83
  signing_key:
72
84
  specification_version: 4
73
85
  summary: This gem is a test
@@ -1,21 +0,0 @@
1
- require 'rails/generators'
2
- require 'rails/generators/migration'
3
- require 'rails/generators/active_record/migration'
4
-
5
- module TaiwanCityDistsHelper
6
- module Generators
7
- class TableGenerator < ::Rails::Generators::Base
8
- include ::Rails::Generators::Migration
9
-
10
- self.source_paths << File.join(File.dirname(__FILE__), 'templates')
11
-
12
- def create_migration_file
13
- migration_template "migration.rb", "db/migrate/create_cities_and_dists.rb"
14
- end
15
-
16
- def self.next_migration_number(path)
17
- @migration_number = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i.to_s
18
- end
19
- end
20
- end
21
- end
@@ -1,19 +0,0 @@
1
- class CreateCitiesAndDists < ActiveRecord::Migration
2
- def self.up
3
- create_table :cities, :force => true do |table|
4
- table.string :name
5
- table.timestamps
6
- end
7
-
8
- create_table :dists, :force => true do |table|
9
- table.string :name
10
- table.integer :city_id
11
- table.timestamps
12
- end
13
- end
14
-
15
- def self.down
16
- drop_table :cities
17
- drop_table :dists
18
- end
19
- end
@@ -1,5 +0,0 @@
1
- module TaiwanCityDistsHelper
2
- module Rails
3
- VERSION = "0.93"
4
- end
5
- end
@@ -1,67 +0,0 @@
1
- # -*- encoding : utf-8 -*-
2
- require 'nokogiri'
3
- require 'open-uri'
4
- require "fileutils"
5
-
6
- namespace :taiwan_city_dists_helper do
7
-
8
- desc "Copy city.rb and dist.rb"
9
- task :copy => :environment do
10
- source_root = File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
11
- puts "Copy City.rb and Dist.rb to your app/models ..."
12
- FileUtils.cp_r("#{source_root}/app/models/.", "#{Rails.root}/app/models", { :preserve => true })
13
- puts "============================================================"
14
- puts "Don't forget require taiwan_city_dists_helper js file in your application.js"
15
- end
16
-
17
- desc "Grab cities and dists from wikipedia"
18
- task :get_cities_and_dists => :environment do
19
- url = "https://zh.wikipedia.org/wiki/%E4%B8%AD%E8%8F%AF%E6%B0%91%E5%9C%8B%E8%87%BA%E7%81%A3%E5%9C%B0%E5%8D%80%E9%84%89%E9%8E%AE%E5%B8%82%E5%8D%80%E5%88%97%E8%A1%A8"
20
- begin
21
- doc = Nokogiri::HTML(open(url))
22
- puts "We got data from wikipedia, now processing ..."
23
- #直轄市
24
- doc.search("//table[@class='wikitable'][3]//tr").each do |tr|
25
- city_name = tr.search("td[1]").text
26
- if city_name != ""
27
- @city = City.create(:name => "#{city_name}")
28
- dist_string = tr.search("td[3]").text
29
- dists = dist_string.split("、")
30
- dists.each do |dist_name|
31
- Dist.create(:name => "#{dist_name}", :city_id => @city.id)
32
- end
33
- end
34
- end
35
- #市
36
- doc.search("//table[@class='wikitable'][4]//tr").each do |tr|
37
- city_name = tr.search("td[1]").text
38
- if city_name != ""
39
- @city = City.create(:name => "#{city_name}")
40
- dist_string = tr.search("td[3]").text
41
- dists = dist_string.split("、")
42
- dists.each do |dist_name|
43
- Dist.create(:name => "#{dist_name}", :city_id => @city.id)
44
- end
45
- end
46
- end
47
- #縣
48
- doc.search("//table[@class='wikitable'][5]//tr").each do |tr|
49
- city_name = tr.search("td[1]").text
50
- if city_name != ""
51
- @city = City.create(:name => "#{city_name}")
52
- dist_string = tr.search("td[3]").text
53
- dists = dist_string.split("、")
54
- dists.each do |dist_name|
55
- Dist.create(:name => "#{dist_name}", :city_id => @city.id)
56
- end
57
- end
58
- end
59
- rescue Exception => e
60
- puts "Please check your network connection, we can't connect wikipedia :("
61
- puts "maybe try later ?"
62
- end
63
-
64
- end
65
-
66
-
67
- end