unidom-region-china 0.1 → 1.0
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
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e46a6679bd74407ba119f1e828fd9a3691bf28e7
|
4
|
+
data.tar.gz: 7fa84735cbd1107bc7b8c18c1abacad6efe7cd78
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 52112ed8fe01dad17dd0ca80f4a0633f5e83cb2fe65271371ae93ca7360448270e38322471fe141db6ae6b3fa4ea7c180eaf4613dabdb99ddb9b3d1a0a5ab003
|
7
|
+
data.tar.gz: 3205bb2ab03c3d9470a8628674b81217101289c77c506c5db57bbb2d059601a37d7b81e58ed29af636b6a7ceb2c64d423ff8a46cc90ea9f06ad5f6c7daa8b9e4
|
data/README.md
CHANGED
@@ -7,11 +7,17 @@ Unidom (统一领域对象模型)是一系列的领域模型引擎。中国行
|
|
7
7
|
```ruby
|
8
8
|
gem 'unidom-region-china'
|
9
9
|
```
|
10
|
+
|
10
11
|
## Run the Database Migration:
|
11
12
|
```shell
|
12
13
|
rake db:migrate
|
13
14
|
```
|
14
15
|
|
16
|
+
## Import Data:
|
17
|
+
```shell
|
18
|
+
bundle exec rake unidom:region:china:import file=/china-region-data/NBS-county/20141031.csv from_date=2014-10-31 scheme_id= scheme_type=
|
19
|
+
```
|
20
|
+
|
15
21
|
## Call the Model:
|
16
22
|
```ruby
|
17
23
|
Unidom::Region::China::Region.numeric_coded_as('120000').valid_at.alive.first # Tianjing (天津)
|
@@ -29,12 +29,12 @@ class Unidom::Region::China::Region < ActiveRecord::Base
|
|
29
29
|
validates :alphabetic_code, allow_blank: true, length: { minimum: 2 }
|
30
30
|
validates :name, presence: true, length: { maximum: self.columns_hash['name'].limit }
|
31
31
|
|
32
|
-
belongs_to :
|
32
|
+
belongs_to :scheme, polymorphic: true
|
33
33
|
has_many :locations, class_name: 'Unidom::Geo::Location', as: :region
|
34
34
|
|
35
|
-
scope :
|
36
|
-
scope :
|
37
|
-
scope :
|
35
|
+
scope :scheme_is, ->(scheme) { scheme.present? ? where(scheme: scheme) : scheme_id_is.scheme_type_is }
|
36
|
+
scope :scheme_id_is, ->(scheme_id = ::Unidom::Common::NULL_UUID) { where scheme_id: scheme_id }
|
37
|
+
scope :scheme_type_is, ->(scheme_type = '' ) { where scheme_type: scheme_type }
|
38
38
|
|
39
39
|
scope :name_is, ->(name) { where name: name }
|
40
40
|
scope :being_virtual, ->(virtual = true) { where virtual: virtual }
|
@@ -4,7 +4,7 @@ class CreateUnidomChinaRegions < ActiveRecord::Migration
|
|
4
4
|
|
5
5
|
create_table :unidom_china_regions, id: :uuid do |t|
|
6
6
|
|
7
|
-
t.references :
|
7
|
+
t.references :scheme, type: :uuid, null: false,
|
8
8
|
polymorphic: { null: false, default: '', limit: 200 }
|
9
9
|
|
10
10
|
t.column :numeric_code, 'char(6)', null: false, default: '0'*6
|
@@ -26,9 +26,9 @@ class CreateUnidomChinaRegions < ActiveRecord::Migration
|
|
26
26
|
|
27
27
|
end
|
28
28
|
|
29
|
-
add_index :unidom_china_regions, :
|
30
|
-
add_index :unidom_china_regions, [ :numeric_code, :
|
31
|
-
add_index :unidom_china_regions, [ :alphabetic_code, :
|
29
|
+
add_index :unidom_china_regions, :scheme_id
|
30
|
+
add_index :unidom_china_regions, [ :numeric_code, :scheme_id, :scheme_type ], unique: true, name: 'index_unidom_china_regions_on_numeric_code_and_scheme'
|
31
|
+
add_index :unidom_china_regions, [ :alphabetic_code, :scheme_id, :scheme_type ], unique: true, name: 'index_unidom_china_regions_on_alphabetic_code_and_scheme'
|
32
32
|
|
33
33
|
end
|
34
34
|
|
data/lib/tasks/data_tasks.rake
CHANGED
@@ -7,28 +7,28 @@ namespace :unidom do
|
|
7
7
|
# bundle exec rake unidom:region:china:import
|
8
8
|
# file=/data.csv
|
9
9
|
# from_date=2020-01-01
|
10
|
-
#
|
11
|
-
#
|
10
|
+
# scheme_id=
|
11
|
+
# scheme_type=
|
12
12
|
task import: :environment do
|
13
13
|
|
14
14
|
include ::Unidom::Common::DataHelper
|
15
15
|
|
16
16
|
file_name = ENV['file']
|
17
|
-
|
18
|
-
|
17
|
+
scheme_id = ENV['scheme_id']||::Unidom::Common::NULL_UUID
|
18
|
+
scheme_type = ENV['scheme_type']||''
|
19
19
|
opened_at = parse_time ENV['from_date']
|
20
20
|
|
21
21
|
updated_count = 0
|
22
22
|
created_count = 0
|
23
23
|
|
24
|
-
region_entities = ::Unidom::Region::China::Region.
|
24
|
+
region_entities = ::Unidom::Region::China::Region.scheme_id_is(scheme_id).scheme_type_is(scheme_type).select('id, name, virtual, numeric_code, alphabetic_code, scheme_id, scheme_type, opened_at, closed_at, defunct, updated_at').to_a
|
25
25
|
|
26
26
|
each_csv_row file_name do |region|
|
27
27
|
|
28
28
|
numeric_code = region['numeric_code']
|
29
29
|
alphabetic_code = region['alphabetic_code']
|
30
30
|
|
31
|
-
attributes = { name: region['name'], virtual: region['virtual'],
|
31
|
+
attributes = { name: region['name'], virtual: region['virtual'], scheme_id: scheme_id, scheme_type: scheme_type, opened_at: opened_at }
|
32
32
|
attributes[:alphabetic_code] = alphabetic_code if alphabetic_code.present?
|
33
33
|
|
34
34
|
if region_entities.present?
|