Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
annnndie committed Jan 20, 2022
0 parents commit 213b534
Show file tree
Hide file tree
Showing 12 changed files with 403 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source "https://rubygems.org"

# Specify your gem's dependencies in taiwan_districts_form_helper.gemspec
gemspec

gem "rake", "~> 12.0"
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2021 eddie

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# TaiwanDistrictsFormHelper

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/taiwan_districts_form_helper`. To experiment with that code, run `bin/console` for an interactive prompt.

TODO: Delete this and the text above, and describe your gem

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'taiwan_districts_form_helper'
```

And then execute:

$ bundle install

Or install it yourself as:

$ gem install taiwan_districts_form_helper

## Usage

TODO: Write usage instructions here

## Development

After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/taiwan_districts_form_helper.


## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require "bundler/gem_tasks"
task :default => :spec
14 changes: 14 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env ruby

require "bundler/setup"
require "taiwan_districts_form_helper"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start

require "irb"
IRB.start(__FILE__)
8 changes: 8 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here
6 changes: 6 additions & 0 deletions lib/taiwan_districts_form_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require "taiwan_districts_form_helper/version"
require "taiwan_districts_form_helper/form_builder"

module TaiwanDistrictsFormHelper
class Error < StandardError; end
end
67 changes: 67 additions & 0 deletions lib/taiwan_districts_form_helper/form_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require "json"

class TWRegionCityAndDistrict < ActionView::Helpers::FormBuilder
def self.json_data
File.read(File.join(File.dirname(__FILE__), "location.json"))
end

def region_select(attribute, selected: nil, **options)
data = load_data

selected = selected || ""
select(
attribute,
data.map { |d| d["region"] },
{ selected: selected, disabled: "", prompt: "請選擇區域" },
**options
)
end

def city_select(attribute, selected: nil, region: nil, **options)
cities = []
if region
data = load_data
found_region = data.find { |d| d["region"] == region }
cities = found_region["cities"].map { |r| r["name"] }
end

selected = selected || ""

select(
attribute,
cities,
{ selected: selected, disabled: "", prompt: "請先選擇區域" },
**options
)
end

def district_select(attribute, selected: nil, city: nil, **options)
districts = []

if city
data = load_data
found_city = nil
data.each do |d|
found_city = d["cities"].find { |c| c["name"] == city }
if found_city
districts = found_city["districts"]
end
end
end

selected = selected || ""

select(
attribute,
districts,
{ selected: selected, disabled: "", prompt: "請先選擇縣市" },
**options
)
end

private

def load_data
@data ||= JSON.parse(File.read(File.join(File.dirname(__FILE__), "location.json")))
end
end
203 changes: 203 additions & 0 deletions lib/taiwan_districts_form_helper/location.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
[
{
"region": "北區",
"cities": [
{
"name": "臺北市",
"districts": [
"中正區", "中山區", "大同區", "松山區", "大安區", "萬華區", "信義區",
"士林區", "北投區", "內湖區", "南港區", "文山區"
]
},
{
"name": "新北市",
"districts": [
"萬里區", "金山區", "板橋區", "汐止區", "深坑區", "石碇區", "瑞芳區",
"平溪區", "雙溪區", "貢寮區", "新店區", "坪林區", "烏來區", "永和區",
"中和區", "土城區", "三峽區", "樹林區", "鶯歌區", "三重區", "新莊區",
"泰山區", "林口區", "蘆洲區", "五股區", "八里區", "淡水區", "三芝區",
"石門區"
]
},
{
"name": "基隆市",
"districts": [
"仁愛區", "信義區", "中正區", "中山區", "安樂區", "暖暖區", "七堵區"
]
},
{
"name": "宜蘭縣",
"districts": [
"宜蘭市", "頭城鎮", "礁溪鄉", "壯圍鄉", "員山鄉", "羅東鎮", "三星鄉",
"大同鄉", "五結鄉", "冬山鄉", "蘇澳鎮", "南澳鄉"
]
},
{
"name": "桃園市",
"districts": [
"中壢區", "平鎮區", "龍潭區", "楊梅區", "新屋區", "觀音區", "桃園區",
"龜山區", "八德區", "大溪區", "復興區", "大園區", "蘆竹區"
]
},
{
"name": "新竹市",
"districts": [
"東區", "北區", "香山區"
]
},
{
"name": "新竹縣",
"districts": [
"竹北市", "湖口鄉", "新豐鄉", "新埔鎮", "關西鎮", "芎林鄉", "寶山鄉",
"竹東鎮", "五峰鄉", "橫山鄉", "尖石鄉", "北埔鄉", "峨眉鄉"
]
}
]
},
{
"region": "中區",
"cities": [
{
"name": "苗栗縣",
"districts": [
"竹南鎮", "頭份鎮", "三灣鄉", "南庄鄉", "獅潭鄉", "後龍鎮", "通霄鎮",
"苑裡鎮", "苗栗市", "造橋鄉", "頭屋鄉", "公館鄉", "大湖鄉", "泰安鄉",
"銅鑼鄉", "三義鄉", "西湖鄉", "卓蘭鎮"
]
},
{
"name": "臺中市",
"districts": [
"中區", "東區", "南區", "西區", "北區", "北屯區", "西屯區", "南屯區",
"太平區", "大里區", "霧峰區", "烏日區", "豐原區", "后里區", "石岡區",
"東勢區", "和平區", "新社區", "潭子區", "大雅區", "神岡區", "大肚區",
"沙鹿區", "龍井區", "梧棲區", "清水區", "大甲區", "外埔區", "大安區"
]
},
{
"name": "彰化縣",
"districts": [
"彰化市", "芬園鄉", "花壇鄉", "秀水鄉", "鹿港鎮", "福興鄉", "線西鄉",
"和美鎮", "伸港鄉", "員林鎮", "社頭鄉", "永靖鄉", "埔心鄉", "溪湖鎮",
"大村鄉", "埔鹽鄉", "田中鎮", "北斗鎮", "田尾鄉", "埤頭鄉", "溪州鄉",
"竹塘鄉", "二林鎮", "大城鄉", "芳苑鄉", "二水鄉"
]
},
{
"name": "南投縣",
"districts": [
"南投市", "中寮鄉", "草屯鎮", "國姓鄉", "埔里鎮", "仁愛鄉", "名間鄉",
"集集鎮", "水里鄉", "魚池鄉", "信義鄉", "竹山鎮", "鹿谷鄉"
]
},
{
"name": "雲林縣",
"districts": [
"斗南鎮", "大埤鄉", "虎尾鎮", "土庫鎮", "褒忠鄉", "東勢鄉", "臺西鄉",
"崙背鄉", "麥寮鄉", "斗六市", "林內鄉", "古坑鄉", "莿桐鄉", "西螺鎮",
"二崙鄉", "北港鎮", "水林鄉", "口湖鄉", "四湖鄉", "元長鄉"
]
}
]
},
{
"region": "南區",
"cities": [
{
"name": "嘉義市",
"districts": [
"西區", "東區"
]
},
{
"name": "嘉義縣",
"districts": [
"番路鄉", "梅山鄉", "竹崎鄉", "阿里山鄉", "中埔鄉", "大埔鄉", "水上鄉",
"鹿草鄉", "太保市", "朴子市", "東石鄉", "六腳鄉", "新港鄉", "民雄鄉",
"大林鎮", "溪口鄉", "義竹鄉", "布袋鎮"
]
},
{
"name": "臺南市",
"districts": [
"中西區", "東區", "南區", "北區", "安平區", "安南區", "永康區",
"歸仁區", "新化區", "左鎮區", "玉井區", "楠西區", "南化區", "仁德區",
"關廟區", "龍崎區", "官田區", "麻豆區", "佳里區", "西港區", "七股區",
"將軍區", "學甲區", "北門區", "新營區", "後壁區", "白河區", "東山區",
"六甲區", "下營區", "柳營區", "鹽水區", "善化區", "新市區", "大內區",
"山上區", "安定區"
]
},
{
"name": "高雄市",
"districts": [
"新興區", "前金區", "苓雅區", "鹽埕區", "鼓山區", "旗津區", "前鎮區",
"三民區", "楠梓區", "小港區", "左營區", "仁武區", "大社區", "岡山區",
"路竹區", "阿蓮區", "田寮區", "燕巢區", "橋頭區", "梓官區", "彌陀區",
"永安區", "湖內區", "鳳山區", "大寮區", "林園區", "鳥松區", "大樹區",
"旗山區", "美濃區", "六龜區", "內門區", "杉林區", "甲仙區", "桃源區",
"那瑪夏", "茂林區", "茄萣區"
]
},
{
"name": "屏東縣",
"districts": [
"屏東市", "三地門", "霧臺鄉", "瑪家鄉", "九如鄉", "里港鄉", "高樹鄉",
"鹽埔鄉", "長治鄉", "麟洛鄉", "竹田鄉", "內埔鄉", "萬丹鄉", "潮州鎮",
"泰武鄉", "來義鄉", "萬巒鄉", "崁頂鄉", "新埤鄉", "南州鄉", "林邊鄉",
"東港鎮", "琉球鄉", "佳冬鄉", "新園鄉", "枋寮鄉", "枋山鄉", "春日鄉",
"獅子鄉", "車城鄉", "牡丹鄉", "恆春鎮", "滿州鄉"
]
}
]
},
{
"region": "東區",
"cities": [
{
"name": "花蓮縣",
"districts": [
"花蓮市", "新城鄉", "秀林鄉", "吉安鄉", "壽豐鄉", "鳳林鎮", "光復鄉",
"豐濱鄉", "瑞穗鄉", "萬榮鄉", "玉里鎮", "卓溪鄉", "富里鄉"
]
},
{
"name": "臺東縣",
"districts": [
"臺東市", "綠島鄉", "蘭嶼鄉", "延平鄉", "卑南鄉", "鹿野鄉", "關山鎮",
"海端鄉", "池上鄉", "東河鄉", "成功鎮", "長濱鄉", "太麻里", "金峰鄉",
"大武鄉", "達仁鄉"
]
}
]
},
{
"region": "離島",
"cities": [
{
"name": "澎湖縣",
"districts": [
"馬公市", "西嶼鄉", "望安鄉", "七美鄉", "白沙鄉", "湖西鄉"
]
},
{
"name": "金門縣",
"districts": [
"金沙鎮", "金湖鎮", "金寧鄉", "金城鎮", "烈嶼鄉", "烏坵鄉"
]
},
{
"name": "連江縣",
"districts": [
"南竿鄉", "北竿鄉", "莒光鄉", "東引鄉"
]
},
{
"name": "離島地區",
"districts": [
"釣魚臺", "東沙群島", "南沙群島"
]
}
]
}
]
3 changes: 3 additions & 0 deletions lib/taiwan_districts_form_helper/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module TaiwanDistrictsFormHelper
VERSION = "0.0.1"
end
Loading

0 comments on commit 213b534

Please sign in to comment.