china_region_fu 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/Gemfile +4 -0
- data/README.rdoc +62 -0
- data/Rakefile +1 -0
- data/app/models/city.rb +17 -0
- data/app/models/district.rb +16 -0
- data/app/models/province.rb +8 -0
- data/china_region_fu.gemspec +20 -0
- data/lib/china_region_fu/engine.rb +5 -0
- data/lib/china_region_fu/version.rb +3 -0
- data/lib/china_region_fu.rb +8 -0
- data/lib/generators/china_region_fu/install/USAGE +8 -0
- data/lib/generators/china_region_fu/install/install_generator.rb +30 -0
- data/lib/generators/china_region_fu/install/templates/migration.rb +45 -0
- data/lib/generators/china_region_fu/install/templates/regions.yml +10823 -0
- data/lib/generators/china_region_fu/mvc/USAGE +17 -0
- data/lib/generators/china_region_fu/mvc/mvc_generator.rb +11 -0
- data/lib/tasks/region.rake +34 -0
- metadata +63 -0
data/Gemfile
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
= ChinaRegionFu
|
2
|
+
|
3
|
+
ChinaRegionFu include the region data of china,and region CURD actions.Got ChinaRegionFu it means you got
|
4
|
+
a powerfull region controller system about china.
|
5
|
+
|
6
|
+
|
7
|
+
== Getting Started
|
8
|
+
|
9
|
+
1. Put 'gem china_region_fu' to your Gemfile:
|
10
|
+
|
11
|
+
gem china_region_fu
|
12
|
+
|
13
|
+
2. Run bundler command to install the gem:
|
14
|
+
|
15
|
+
bundle install
|
16
|
+
|
17
|
+
3. After you install the gem, you need run the generator:
|
18
|
+
|
19
|
+
rails g china_region_fu:install
|
20
|
+
|
21
|
+
It will:
|
22
|
+
* Generate <em>db/migrate/<timestamp>create_china_region_tables.rb</em> migrate file to your app, *_china_regions_* table is used for store the regions.
|
23
|
+
* Copy regions.yml to config/ directory.
|
24
|
+
* Run "rake db:migrate".
|
25
|
+
* Run "rake region:import".
|
26
|
+
|
27
|
+
Now you have there ActiveRecord modules: <b>Province, City, District</b>.
|
28
|
+
|
29
|
+
Run with <em>rails g</em> for get generator list.
|
30
|
+
|
31
|
+
4. If you want to customize the region modules you can run the generator:
|
32
|
+
rails g china_region_fu:mvc
|
33
|
+
|
34
|
+
This will create:
|
35
|
+
|
36
|
+
create app/models/province.rb
|
37
|
+
create app/models/city.rb
|
38
|
+
create app/models/district.rb
|
39
|
+
|
40
|
+
So you can do what you want to do in this files.
|
41
|
+
|
42
|
+
5. Examples
|
43
|
+
|
44
|
+
a = Province.last
|
45
|
+
a.name # => "台湾省"
|
46
|
+
a.cities.map(&:name) # => ["嘉义市", "台南市", "新竹市", "台中市", "基隆市", "台北市"]
|
47
|
+
|
48
|
+
Province.first.districts.map(&:name) # => ["延庆县", "密云县", "平谷区", ...]
|
49
|
+
|
50
|
+
c = City.last
|
51
|
+
c.name # => "酒泉市"
|
52
|
+
c.districts.map(&:name) # => ["敦煌市", "玉门市", "阿克塞哈萨克族自治县", "肃北蒙古族自治县", "安西县", ...]
|
53
|
+
c.brothers.map(&:name) # => ["甘南藏族自治州", "临夏回族自治州", "陇南市", ...]
|
54
|
+
|
55
|
+
== Contributing
|
56
|
+
|
57
|
+
Xuhao <{rubyfans.com}[http://www.rubyfans.com]>.
|
58
|
+
|
59
|
+
== License
|
60
|
+
|
61
|
+
ChinaRegionFu is released under the MIT license.
|
62
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/app/models/city.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
class City < ActiveRecord::Base
|
3
|
+
attr_accessible :name, :province_id, :level, :zip_code, :pinyin, :pinyin_abbr
|
4
|
+
|
5
|
+
belongs_to :province
|
6
|
+
has_many :districts, :dependent => :destroy
|
7
|
+
has_many :hospitals, :dependent => :nullify
|
8
|
+
|
9
|
+
def short_name
|
10
|
+
@short_name ||= name.gsub(/市|自治州|地区|特别行政区/,'')
|
11
|
+
end
|
12
|
+
|
13
|
+
def brothers
|
14
|
+
@brothers ||= City.where("province_id = #{province_id}")
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
class District < ActiveRecord::Base
|
3
|
+
attr_accessible :name, :city_id, :pinyin, :pinyin_abbr
|
4
|
+
|
5
|
+
belongs_to :city
|
6
|
+
has_many :hospitals, :dependent => :nullify
|
7
|
+
|
8
|
+
def short_name
|
9
|
+
@short_name ||= name.gsub(/区|县|市|自治县/,'')
|
10
|
+
end
|
11
|
+
|
12
|
+
def brothers
|
13
|
+
@brothers ||= District.where("city_id = #{city_id}")
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "china_region_fu/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "china_region_fu"
|
7
|
+
s.version = ChinaRegionFu::VERSION
|
8
|
+
s.authors = ["xuhao"]
|
9
|
+
s.email = ["xuhao@rubyfans.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{china region}
|
12
|
+
s.description = %q{china region}
|
13
|
+
|
14
|
+
s.rubyforge_project = "china_region_fu"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module ChinaRegionFu
|
2
|
+
class InstallGenerator < Rails::Generators::Base
|
3
|
+
include Rails::Generators::Migration
|
4
|
+
source_root File.expand_path('../templates', __FILE__)
|
5
|
+
|
6
|
+
def copy_migration_file
|
7
|
+
migration_template "migration.rb", "db/migrate/create_china_region_tables.rb"
|
8
|
+
end
|
9
|
+
|
10
|
+
def copy_region_config_file
|
11
|
+
copy_file 'regions.yml', 'config/regions.yml'
|
12
|
+
end
|
13
|
+
|
14
|
+
def execute_migrate
|
15
|
+
rake("db:migrate")
|
16
|
+
end
|
17
|
+
|
18
|
+
def import_region_to_data
|
19
|
+
rake('region:import')
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.next_migration_number(dirname)
|
23
|
+
if ActiveRecord::Base.timestamped_migrations
|
24
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
25
|
+
else
|
26
|
+
"%.3d" % (current_migration_number(dirname) + 1)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
class CreateChinaRegionTables < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :provinces do |t|
|
4
|
+
t.string :name
|
5
|
+
t.string :pinyin
|
6
|
+
t.string :pinyin_abbr
|
7
|
+
t.timestamps
|
8
|
+
end
|
9
|
+
create_table :cities do |t|
|
10
|
+
t.string :name
|
11
|
+
t.integer :province_id
|
12
|
+
t.integer :level
|
13
|
+
t.string :zip_code
|
14
|
+
t.string :pinyin
|
15
|
+
t.string :pinyin_abbr
|
16
|
+
t.timestamps
|
17
|
+
end
|
18
|
+
create_table :districts do |t|
|
19
|
+
t.string :name
|
20
|
+
t.integer :city_id
|
21
|
+
t.string :pinyin
|
22
|
+
t.string :pinyin_abbr
|
23
|
+
t.timestamps
|
24
|
+
end
|
25
|
+
add_index :districts, :name
|
26
|
+
add_index :districts, :city_id
|
27
|
+
add_index :districts, :pinyin
|
28
|
+
add_index :districts, :pinyin_abbr
|
29
|
+
add_index :cities, :name
|
30
|
+
add_index :cities, :province_id
|
31
|
+
add_index :cities, :level
|
32
|
+
add_index :cities, :zip_code
|
33
|
+
add_index :cities, :pinyin
|
34
|
+
add_index :cities, :pinyin_abbr
|
35
|
+
add_index :provinces, :name
|
36
|
+
add_index :provinces, :pinyin
|
37
|
+
add_index :provinces, :pinyin_abbr
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
def self.down
|
42
|
+
drop_table :provinces, :cities, :districts
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|