taiwan_city_dists_helper 0.86 → 0.95
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +0 -0
- data/Gemfile +0 -0
- data/LICENSE.txt +0 -0
- data/README.md +6 -6
- data/Rakefile +1 -1
- data/app/models/city.rb +4 -2
- data/app/models/dist.rb +4 -2
- data/lib/generators/tcdh/table/table_generator.rb +24 -0
- data/lib/generators/tcdh/table/templates/migration.rb +22 -0
- data/lib/taiwan_city_dists_helper.rb +3 -2
- data/lib/taiwan_city_dists_helper/engine.rb +5 -2
- data/lib/taiwan_city_dists_helper/gem_tasks.rb +11 -0
- data/lib/taiwan_city_dists_helper/helper.rb +10 -8
- data/lib/tasks/fetch.rake +32 -0
- data/taiwan_city_dists_helper.gemspec +16 -13
- data/vendor/assets/javascripts/{taiwan_city_dists_helper.js → tcdh.js} +3 -3
- metadata +24 -12
- data/lib/generators/taiwan_city_dists_helper/table/table_generator.rb +0 -21
- data/lib/generators/taiwan_city_dists_helper/table/templates/migration.rb +0 -19
- data/lib/taiwan_city_dists_helper/version.rb +0 -5
- data/lib/tasks/taiwan_city_dists_helper.rake +0 -42
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: eab05e705bf22ad4d5c0a23840f69eb19bc2b990a8ce8c2d683aefe705b4ba52
|
4
|
+
data.tar.gz: 6e057c4cbac443eea3a158ecf4e09ecb9dbeb937713c6b42041bb45ceafe9cba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 39e902178e58308561cdd8cda65b80709cd6e35dec53023fac63ebb5a2f8ce0ad345f80cc84c9950b9a9096ce82299ee3f2f80516562d9fc834d519485eaf5a0
|
7
|
+
data.tar.gz: 49fdb51e5d440d46db709faf7d6eb648510e79d0ebc1bcf7d4d06057376457e18210de5532e002c4c06be3d96dab4e12569dcc3d3083eae39d1e7829bcba13f8
|
data/.gitignore
CHANGED
File without changes
|
data/Gemfile
CHANGED
File without changes
|
data/LICENSE.txt
CHANGED
File without changes
|
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
|
20
|
-
2.
|
21
|
-
3.
|
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
|
1
|
+
require 'bundler/gem_tasks'
|
data/app/models/city.rb
CHANGED
data/app/models/dist.rb
CHANGED
@@ -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,9 +1,12 @@
|
|
1
|
-
|
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
|
9
|
+
initializer 'taiwan_city_dists_helper.view_helpers' do
|
7
10
|
ActionView::Base.send :include, TaiwanCityDistsHelper::Helper
|
8
11
|
end
|
9
12
|
end
|
@@ -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(
|
4
|
-
|
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(
|
8
|
-
|
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(
|
12
|
-
|
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
|
-
#
|
2
|
-
|
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 =
|
8
|
-
gem.version =
|
9
|
-
gem.
|
10
|
-
gem.
|
11
|
-
gem.
|
12
|
-
gem.
|
13
|
-
gem.
|
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
|
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 = [
|
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
|
-
|
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
|
-
|
11
|
+
$('select[id*="_dist_id"]').html(options);
|
12
12
|
} else {
|
13
|
-
|
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.
|
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:
|
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/
|
43
|
-
- lib/generators/
|
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/
|
48
|
-
- lib/tasks/taiwan_city_dists_helper.rake
|
61
|
+
- lib/tasks/fetch.rake
|
49
62
|
- taiwan_city_dists_helper.gemspec
|
50
|
-
- vendor/assets/javascripts/
|
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:
|
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
|
-
|
70
|
-
rubygems_version: 2.2.2
|
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,42 +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 = "http://zh.wikipedia.org/wiki/%E4%B8%AD%E8%8F%AF%E6%B0%91%E5%9C%8B%E5%8F%B0%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
|
-
doc.search("//table[@class='wikitable']//tr").each do |tr|
|
24
|
-
city_name = tr.search("td[1]").text
|
25
|
-
if city_name != ""
|
26
|
-
@city = City.create(:name => "#{city_name}")
|
27
|
-
dist_string = tr.search("td[3]").text
|
28
|
-
dists = dist_string.split("、")
|
29
|
-
dists.each do |dist_name|
|
30
|
-
Dist.create(:name => "#{dist_name}", :city_id => @city.id)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
rescue Exception => e
|
35
|
-
puts "Please check your network connection, we can't get wikipedia :("
|
36
|
-
puts "maybe try later will do ?"
|
37
|
-
end
|
38
|
-
|
39
|
-
end
|
40
|
-
|
41
|
-
|
42
|
-
end
|