@@ -8,6 +8,8 @@ Rails.logger = Logger.new($stdout) unless Rails.env.test?
8
8
# `SEEDING_CREATOR_ID=00000000-0000-0000-0000-000000000000 rails classroom_management:seed_an_unverified_school`
9
9
# `SEEDING_TEACHER_ID=00000000-0000-0000-0000-000000000000 rails classroom_management:seed_a_school_with_lessons`
10
10
11
+ # For students to match up the school needs to match with the school defined in profile (hard coded in the helper)
12
+
11
13
# rubocop:disable Metrics/BlockLength
12
14
namespace :classroom_management do
13
15
include ClassroomManagementHelper
@@ -16,23 +18,29 @@ namespace :classroom_management do
16
18
task destroy_seed_data : :environment do
17
19
ActiveRecord ::Base . transaction do
18
20
Rails . logger . info 'Destroying existing seeds...'
19
- creator_id = ENV . fetch ( 'SEEDING_CREATOR_ID' , TEST_USERS [ :jane ] )
20
- teacher_id = ENV . fetch ( 'SEEDING_TEACHER_ID' , TEST_USERS [ :john ] )
21
- school_id = ENV . fetch ( 'SEEDING_SCHOOL_ID' , School . find_by ( creator_id :) &.id )
21
+ creator_id = ENV . fetch ( 'SEEDING_CREATOR_ID' , TEST_USERS [ :jane_doe ] )
22
+ teacher_id = ENV . fetch ( 'SEEDING_TEACHER_ID' , TEST_USERS [ :john_doe ] )
22
23
23
- Role . where ( user_id : [ creator_id , teacher_id ] ) . destroy_all
24
+ # Hard coded as the student's school needs to match
25
+ student_ids = [ TEST_USERS [ :jane_smith ] , TEST_USERS [ :john_smith ] ]
26
+ school_id = TEST_SCHOOL
24
27
25
- if school_id . nil? || creator_id . nil?
26
- Rails . logger . info 'No school found for creator, exiting...'
27
- exit
28
- end
28
+ # Remove the roles first
29
+ Role . where ( user_id : [ creator_id , teacher_id ] + student_ids ) . destroy_all
30
+
31
+ # Destroy the project and then the lesson itself (The lesson's `before_destroy` prevents us using destroy)
32
+ lesson_ids = Lesson . where ( school_id :) . pluck ( :id )
33
+ Project . where ( lesson_id : [ lesson_ids ] ) . destroy_all
34
+ Lesson . where ( id : [ lesson_ids ] ) . delete_all
35
+
36
+ # Destroy the class members and then the class itself
37
+ school_class_ids = SchoolClass . where ( school_id :) . pluck ( :id )
38
+ ClassMember . where ( school_class_id : [ school_class_ids ] ) . destroy_all
39
+ SchoolClass . where ( id : [ school_class_ids ] ) . destroy_all
40
+
41
+ # Destroy the school
42
+ School . find ( school_id ) . destroy
29
43
30
- lesson_id = Lesson . where ( school_id :) . pluck ( :id )
31
- Project . where ( lesson_id :) . destroy_all
32
- # The `before_destroy` prevents us using destroy, but as we've removed projects already we can safely delete
33
- Lesson . where ( school_id :) . delete_all
34
- SchoolClass . where ( school_id :) . destroy_all
35
- School . where ( creator_id :) . destroy_all
36
44
Rails . logger . info 'Done...'
37
45
rescue StandardError => e
38
46
Rails . logger . error "Failed: #{ e . message } "
@@ -44,8 +52,9 @@ namespace :classroom_management do
44
52
task seed_an_unverified_school : :environment do
45
53
ActiveRecord ::Base . transaction do
46
54
Rails . logger . info 'Attempting to seed data...'
47
- creator_id = ENV . fetch ( 'SEEDING_CREATOR_ID' , TEST_USERS [ :jane ] )
48
- create_school ( creator_id )
55
+ creator_id = ENV . fetch ( 'SEEDING_CREATOR_ID' , TEST_USERS [ :jane_doe ] )
56
+ create_school ( creator_id , TEST_SCHOOL )
57
+
49
58
Rails . logger . info 'Done...'
50
59
rescue StandardError => e
51
60
Rails . logger . error "Failed: #{ e . message } "
@@ -57,9 +66,9 @@ namespace :classroom_management do
57
66
task seed_a_verified_school : :environment do
58
67
ActiveRecord ::Base . transaction do
59
68
Rails . logger . info 'Attempting to seed data...'
60
- creator_id = ENV . fetch ( 'SEEDING_CREATOR_ID' , TEST_USERS [ :jane ] )
61
- school_id = ENV . fetch ( 'SEEDING_SCHOOL_ID' , nil )
62
- school = create_school ( creator_id , school_id )
69
+ creator_id = ENV . fetch ( 'SEEDING_CREATOR_ID' , TEST_USERS [ :jane_doe ] )
70
+
71
+ school = create_school ( creator_id , TEST_SCHOOL )
63
72
verify_school ( school )
64
73
Rails . logger . info 'Done...'
65
74
rescue StandardError => e
@@ -68,19 +77,20 @@ namespace :classroom_management do
68
77
end
69
78
end
70
79
71
- desc 'Create a school with lessons'
72
- task seed_a_school_with_lessons : :environment do
80
+ desc 'Create a school with lessons and students '
81
+ task seed_a_school_with_lessons_and_students : :environment do
73
82
ActiveRecord ::Base . transaction do
74
83
Rails . logger . info 'Attempting to seed data...'
75
- creator_id = ENV . fetch ( 'SEEDING_CREATOR_ID' , TEST_USERS [ :jane ] )
76
- teacher_id = ENV . fetch ( 'SEEDING_TEACHER_ID' , TEST_USERS [ :john ] )
77
- school_id = ENV . fetch ( 'SEEDING_SCHOOL_ID' , nil )
84
+ creator_id = ENV . fetch ( 'SEEDING_CREATOR_ID' , TEST_USERS [ :jane_doe ] )
85
+ teacher_id = ENV . fetch ( 'SEEDING_TEACHER_ID' , TEST_USERS [ :john_doe ] )
78
86
79
- school = create_school ( creator_id , school_id )
87
+ school = create_school ( creator_id , TEST_SCHOOL )
80
88
verify_school ( school )
81
89
assign_a_teacher ( teacher_id , school )
82
90
83
91
school_class = create_school_class ( creator_id , school )
92
+ assign_students ( school_class , school )
93
+
84
94
lessons = create_lessons ( creator_id , school , school_class )
85
95
lessons . each do |lesson |
86
96
create_project ( creator_id , school , lesson )
0 commit comments