taiwan_city_dists_helper 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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +106 -0
- data/Rakefile +1 -0
- data/app/assets/javascripts/taiwan_city_dists_helper.js +17 -0
- data/app/models/city.rb +5 -0
- data/app/models/dist.rb +5 -0
- data/lib/generators/taiwan_city_dists_helper/table/table_generator.rb +21 -0
- data/lib/generators/taiwan_city_dists_helper/table/templates/migration.rb +19 -0
- data/lib/taiwan_city_dists_helper/engine.rb +11 -0
- data/lib/taiwan_city_dists_helper/helper.rb +16 -0
- data/lib/taiwan_city_dists_helper/version.rb +5 -0
- data/lib/taiwan_city_dists_helper.rb +11 -0
- data/lib/tasks/taiwan_city_dists_helper.rake +45 -0
- data/taiwan_city_dists_helper.gemspec +20 -0
- metadata +78 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2013 unayung
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# TaiwanCityDistsHelper
|
|
2
|
+
|
|
3
|
+
Generate Taiwan City and Districts models with dynamic select boxes
|
|
4
|
+
|
|
5
|
+
輕鬆產生台灣鄉鎮市的 model 以及動態 select box
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Add this line to your application's Gemfile:
|
|
10
|
+
|
|
11
|
+
gem 'taiwan_city_dists_helper'
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
$ bundle
|
|
16
|
+
|
|
17
|
+
## Preparation
|
|
18
|
+
|
|
19
|
+
1. rails g taiwan_city_dists_helper:table
|
|
20
|
+
2. rake db:migrate
|
|
21
|
+
3. rake taiwan_city_dists_helper:copy
|
|
22
|
+
4. rake taiwan_city_dists_helper:get_cities_and_dists
|
|
23
|
+
|
|
24
|
+
Make sure your object ( let's say Customer ) has relations with city and dist, so you have to
|
|
25
|
+
add city\_id and dist\_id to your customers table, and define belongs_to in your customer.rb
|
|
26
|
+
|
|
27
|
+
class Customer < ActiveRecord::Base
|
|
28
|
+
belongs_to :city
|
|
29
|
+
belongs_to :dist
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
## Helper
|
|
33
|
+
|
|
34
|
+
### render\_city\_select(FormObject, CollectionOfCities, IncludeBlank?=false)
|
|
35
|
+
will render
|
|
36
|
+
|
|
37
|
+
<select id="customer_city_id" name="customer[city_id]">
|
|
38
|
+
<option value="1">臺北市</option>
|
|
39
|
+
<option value="2">新北市</option>
|
|
40
|
+
<option value="3">臺中市</option>
|
|
41
|
+
.....
|
|
42
|
+
</select>
|
|
43
|
+
|
|
44
|
+
### render\_dist\_select(FormObject, CollectionOfDists, IncludeBlank?=false)
|
|
45
|
+
will render
|
|
46
|
+
|
|
47
|
+
<select id="customer_dist_id" name="customer[dist_id]">
|
|
48
|
+
<option value="1">松山區</option>
|
|
49
|
+
<option value="2">大安區</option>
|
|
50
|
+
<option value="3">大同區</option>
|
|
51
|
+
<option value="4">中山區</option>
|
|
52
|
+
<option value="5">內湖區</option>
|
|
53
|
+
.....
|
|
54
|
+
</select>
|
|
55
|
+
|
|
56
|
+
### render\_grouped\_dist\_select(FormObject, CollectionOfCities, IncludeBlank?=false)
|
|
57
|
+
will render
|
|
58
|
+
|
|
59
|
+
<select id="deal_dist_id" name="deal[dist_id]">
|
|
60
|
+
<optgroup label="臺北市">
|
|
61
|
+
<option value="1">松山區</option>
|
|
62
|
+
<option value="2">大安區</option>
|
|
63
|
+
<option value="3">大同區</option>
|
|
64
|
+
<option value="4">中山區</option>
|
|
65
|
+
<option value="5">內湖區</option>
|
|
66
|
+
<option value="6">南港區</option>
|
|
67
|
+
<option value="7">士林區</option>
|
|
68
|
+
<option value="8">北投區</option>
|
|
69
|
+
<option value="9">信義區</option>
|
|
70
|
+
<option value="10">中正區</option>
|
|
71
|
+
<option value="11">萬華區</option>
|
|
72
|
+
<option value="12">文山區</option>
|
|
73
|
+
</optgroup>
|
|
74
|
+
<optgroup label="新北市">
|
|
75
|
+
<option value="13">板橋區</option>
|
|
76
|
+
<option value="14">三重區</option>
|
|
77
|
+
<option value="15">永和區</option>
|
|
78
|
+
<option value="16">中和區</option>
|
|
79
|
+
.....
|
|
80
|
+
</select>
|
|
81
|
+
|
|
82
|
+
## Example
|
|
83
|
+
|
|
84
|
+
With simple\_form you can do this
|
|
85
|
+
|
|
86
|
+
<%= simple_form_for(@customer, :url => customers_path ) do |f| %>
|
|
87
|
+
<%= f.label "交易地點" %>
|
|
88
|
+
<%= render_city_select(f, @cities, false) %>
|
|
89
|
+
<%= render_grouped_dist_select(f, @cities, false) %>
|
|
90
|
+
<% end %>
|
|
91
|
+
|
|
92
|
+
Or with form\_for you still can do this
|
|
93
|
+
|
|
94
|
+
<%= form_for @customer do |f| %>
|
|
95
|
+
<%= f.label "交易地點" %>
|
|
96
|
+
<%= render_city_select(f, @cities, false) %>
|
|
97
|
+
<%= render_grouped_dist_select(f, @cities, false) %>
|
|
98
|
+
<% end %>
|
|
99
|
+
|
|
100
|
+
## Contributing
|
|
101
|
+
|
|
102
|
+
1. Fork it
|
|
103
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
104
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
105
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
106
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require "bundler/gem_tasks"
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
$(function() {
|
|
2
|
+
var dists;
|
|
3
|
+
dists = $('select[id*="_dist_id"]').html();
|
|
4
|
+
return $('select[id*="_city_id"]').change(function() {
|
|
5
|
+
var city, escaped_city, options;
|
|
6
|
+
city = $('select[id*="_city_id"] :selected').text();
|
|
7
|
+
escaped_city = city.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1');
|
|
8
|
+
if (dists.indexOf("optgroup") != -1){
|
|
9
|
+
options = $(dists).filter("optgroup[label=" + escaped_city + "]").html();
|
|
10
|
+
if (options) {
|
|
11
|
+
return $('select[id*="_dist_id"]').html(options);
|
|
12
|
+
} else {
|
|
13
|
+
return $('select[id*="_dist_id"]').empty();
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
});
|
data/app/models/city.rb
ADDED
data/app/models/dist.rb
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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
|
|
@@ -0,0 +1,19 @@
|
|
|
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
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
require "taiwan_city_dists_helper/helper"
|
|
2
|
+
|
|
3
|
+
module TaiwanCityDistsHelper
|
|
4
|
+
module Rails
|
|
5
|
+
class Engine < ::Rails::Engine
|
|
6
|
+
initializer "taiwan_city_dists_helper.view_helpers" do
|
|
7
|
+
ActionView::Base.send :include, TaiwanCityDistsHelper::Helper
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
module TaiwanCityDistsHelper
|
|
2
|
+
module Helper
|
|
3
|
+
def render_city_select(f, cities, blank=false)
|
|
4
|
+
f.collection_select :city_id, cities, :id, :name, include_blank: blank
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def render_grouped_dist_select(f, cities, blank=false)
|
|
8
|
+
f.grouped_collection_select :dist_id, cities, :dists, :name, :id, :name, include_blank: blank
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def render_dist_select(f, dists, blank=false)
|
|
12
|
+
f.collection_select :dist_id, dists, :id, :name, include_blank: blank
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
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 "Copy taiwan_city_dists.js to your app/assets/javascripts ..."
|
|
15
|
+
FileUtils.cp_r("#{source_root}/app/assets/javascripts/.", "#{Rails.root}/app/assets/javascripts", { :preserve => true })
|
|
16
|
+
puts "============================================================"
|
|
17
|
+
puts "Don't forget require taiwan_city_dists_helper js file in your application.js"
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
desc "Grab cities and dists from wikipedia"
|
|
21
|
+
task :get_cities_and_dists => :environment do
|
|
22
|
+
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"
|
|
23
|
+
begin
|
|
24
|
+
doc = Nokogiri::HTML(open(url))
|
|
25
|
+
puts "We got data from wikipedia, now processing ..."
|
|
26
|
+
doc.search("//table[@class='wikitable']//tr").each do |tr|
|
|
27
|
+
city_name = tr.search("td[1]").text
|
|
28
|
+
if city_name != ""
|
|
29
|
+
@city = City.create(:name => "#{city_name}")
|
|
30
|
+
dist_string = tr.search("td[3]").text
|
|
31
|
+
dists = dist_string.split("、")
|
|
32
|
+
dists.each do |dist_name|
|
|
33
|
+
Dist.create(:name => "#{dist_name}", :city_id => @city.id)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
rescue Exception => e
|
|
38
|
+
puts "Please check your network connection, we can't get wikipedia :("
|
|
39
|
+
puts "maybe try later will do ?"
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'taiwan_city_dists_helper/version'
|
|
5
|
+
|
|
6
|
+
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"
|
|
14
|
+
|
|
15
|
+
gem.add_dependency "nokogiri"
|
|
16
|
+
gem.files = `git ls-files`.split($/)
|
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
|
18
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
|
19
|
+
gem.require_paths = ["lib"]
|
|
20
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: taiwan_city_dists_helper
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: '0.8'
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- unayung
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2013-03-01 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: nokogiri
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
|
+
none: false
|
|
18
|
+
requirements:
|
|
19
|
+
- - ! '>='
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '0'
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
none: false
|
|
26
|
+
requirements:
|
|
27
|
+
- - ! '>='
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: '0'
|
|
30
|
+
description: Generate Taiwan cities and dists model and provide dynamic select box
|
|
31
|
+
helper
|
|
32
|
+
email:
|
|
33
|
+
- Unayung@gmail.com
|
|
34
|
+
executables: []
|
|
35
|
+
extensions: []
|
|
36
|
+
extra_rdoc_files: []
|
|
37
|
+
files:
|
|
38
|
+
- .gitignore
|
|
39
|
+
- Gemfile
|
|
40
|
+
- LICENSE.txt
|
|
41
|
+
- README.md
|
|
42
|
+
- Rakefile
|
|
43
|
+
- app/assets/javascripts/taiwan_city_dists_helper.js
|
|
44
|
+
- app/models/city.rb
|
|
45
|
+
- app/models/dist.rb
|
|
46
|
+
- lib/generators/taiwan_city_dists_helper/table/table_generator.rb
|
|
47
|
+
- lib/generators/taiwan_city_dists_helper/table/templates/migration.rb
|
|
48
|
+
- lib/taiwan_city_dists_helper.rb
|
|
49
|
+
- lib/taiwan_city_dists_helper/engine.rb
|
|
50
|
+
- lib/taiwan_city_dists_helper/helper.rb
|
|
51
|
+
- lib/taiwan_city_dists_helper/version.rb
|
|
52
|
+
- lib/tasks/taiwan_city_dists_helper.rake
|
|
53
|
+
- taiwan_city_dists_helper.gemspec
|
|
54
|
+
homepage: http://github.com/Unayung/taiwan_city_dists_helper
|
|
55
|
+
licenses: []
|
|
56
|
+
post_install_message:
|
|
57
|
+
rdoc_options: []
|
|
58
|
+
require_paths:
|
|
59
|
+
- lib
|
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
61
|
+
none: false
|
|
62
|
+
requirements:
|
|
63
|
+
- - ! '>='
|
|
64
|
+
- !ruby/object:Gem::Version
|
|
65
|
+
version: '0'
|
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
67
|
+
none: false
|
|
68
|
+
requirements:
|
|
69
|
+
- - ! '>='
|
|
70
|
+
- !ruby/object:Gem::Version
|
|
71
|
+
version: '0'
|
|
72
|
+
requirements: []
|
|
73
|
+
rubyforge_project:
|
|
74
|
+
rubygems_version: 1.8.24
|
|
75
|
+
signing_key:
|
|
76
|
+
specification_version: 3
|
|
77
|
+
summary: This gem is a test
|
|
78
|
+
test_files: []
|