@@ -6,12 +6,17 @@ namespace :import_data do
6
6
7
7
# This is a starting point for the import process
8
8
# We will refactor for elegance
9
+ # It is not idempotent; it presumes a clean database except for seeds
10
+
11
+ desc "All"
12
+ task all : [ :regions , :providers , :topics ]
9
13
10
14
desc "Import Providers and Associate With Regions"
11
15
task providers : :environment do
12
16
file_path = Rails . root . join ( "import_files" , "Providers.csv" )
13
17
data = CSV . read ( file_path , headers : true )
14
18
data . each do |row |
19
+ # TODO: add old_provider_id to Provider model
15
20
provider = Provider . find_or_create_by! ( name : row [ "Provider_Name" ] , provider_type : row [ "Provider_Type" ] )
16
21
region = Region . find_or_create_by! ( name : row [ "region_name" ] )
17
22
# TODO: we need the association table
@@ -22,7 +27,7 @@ namespace :import_data do
22
27
puts "Provider #{ provider . name } associated with region #{ region . name } "
23
28
24
29
user = User . find_or_create_by! ( email_address : "#{ row [ "Provider_Name" ] . underscore . downcase } @update.me" , password_digest : BCrypt ::Password . create ( row [ "Provider_Password" ] ) , is_admin : false )
25
- # TODO: add provider_id once that association is in place
30
+ user . update ( provider_id : provider . id )
26
31
puts "User #{ user . email_address } created"
27
32
end
28
33
end
@@ -32,11 +37,18 @@ namespace :import_data do
32
37
file_path = Rails . root . join ( "import_files" , "Topics.csv" )
33
38
data = CSV . read ( file_path , headers : true )
34
39
data . each do |row |
35
- topic = Topic . find_or_create_by! ( name : row [ "Topic_Name" ] )
36
- provider = Provider . find_by ( name : row [ "Provider_Name" ] )
37
- topic . providers << provider
38
- puts "Topic #{ topic . name } associated with provider #{ provider . name } "
40
+ # look up provider by old_provider_id
41
+ provider = Provider . find_by ( old_provider_id : row [ "Provider_ID" ] )
42
+ # Language IDs should correspond to the IDs in old app
43
+ # TODO: verify before final import
44
+ topic = Topic . find_or_create_by! ( name : row [ "Topic_Name" ] , provider_id : provider . id , description : row [ "Topic_Desc" ] ,
45
+ language_id : row [ "Language_ID" ] , uid : row [ "Topic_UID" ] , state : determine_state ( row [ "Topic_Archived" ] , old_topic_id : row [ "topic_id" ] ) )
46
+ puts "Topic #{ topic . name } created"
39
47
end
40
-
48
+ end
49
+
50
+ def determine_state ( archived )
51
+ # TODO: validate against state enumerable
52
+ archived ? "archived" : "published"
41
53
end
42
54
end
0 commit comments