Skip to content

Commit 20be4fd

Browse files
abcampo-iryzetter-rpf
authored andcommitted
added extra safeguards
1 parent 787fd3e commit 20be4fd

2 files changed

Lines changed: 27 additions & 13 deletions

File tree

lib/tasks/for_education.rake

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ namespace :for_education do
2929

3030
# Destroy the project and then the lesson itself (The lesson's `before_destroy` prevents us using destroy)
3131
lesson_ids = Lesson.where(school_id:).pluck(:id)
32-
Project.where(lesson_id: [lesson_ids]).destroy_all
33-
Lesson.where(id: [lesson_ids]).delete_all
32+
Project.where(lesson_id: lesson_ids).destroy_all
33+
Lesson.where(id: lesson_ids).delete_all
3434

3535
# Destroy the class members and then the class itself
3636
school_class_ids = SchoolClass.where(school_id:).pluck(:id)
37-
ClassStudent.where(school_class_id: [school_class_ids]).destroy_all
38-
SchoolClass.where(id: [school_class_ids]).destroy_all
37+
ClassStudent.where(school_class_id: school_class_ids).destroy_all
38+
SchoolClass.where(id: school_class_ids).destroy_all
3939

4040
# Destroy the school
4141
School.where(id: school_id).destroy_all
@@ -46,9 +46,9 @@ namespace :for_education do
4646

4747
desc 'Create an unverified school'
4848
task seed_an_unverified_school: :environment do
49-
if School.find_by(code: TEST_SCHOOL)
50-
puts "Test school (#{TEST_SCHOOL}) already exists, run the destroy_seed_data task to start over)."
51-
return
49+
if School.exists?(id: TEST_SCHOOL)
50+
puts "Test school (#{TEST_SCHOOL}) already exists, run the destroy_seed_data task to start over."
51+
next
5252
end
5353

5454
ActiveRecord::Base.transaction do
@@ -62,9 +62,9 @@ namespace :for_education do
6262

6363
desc 'Create a verified school'
6464
task seed_a_verified_school: :environment do
65-
if School.find_by(code: TEST_SCHOOL)
66-
puts "Test school (#{TEST_SCHOOL}) already exists, run the destroy_seed_data task to start over)."
67-
return
65+
if School.exists?(id: TEST_SCHOOL)
66+
puts "Test school (#{TEST_SCHOOL}) already exists, run the destroy_seed_data task to start over."
67+
next
6868
end
6969

7070
ActiveRecord::Base.transaction do
@@ -79,9 +79,9 @@ namespace :for_education do
7979

8080
desc 'Create a school with lessons and students'
8181
task seed_a_school_with_lessons_and_students: :environment do
82-
if School.find_by(code: TEST_SCHOOL)
83-
puts "Test school (#{TEST_SCHOOL}) already exists, run the destroy_seed_data task to start over)."
84-
return
82+
if School.exists?(id: TEST_SCHOOL)
83+
puts "Test school (#{TEST_SCHOOL}) already exists, run the destroy_seed_data task to start over."
84+
next
8585
end
8686

8787
ActiveRecord::Base.transaction do

spec/lib/tasks/for_education_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,20 @@
8888
expect(projects.length).to eq(2)
8989
end
9090

91+
it 'does not add more lessons or projects when run again' do
92+
allow($stdout).to receive(:puts)
93+
94+
expect do
95+
task.reenable
96+
task.invoke
97+
end.not_to change {
98+
[
99+
Lesson.where(school_id: school.id).count,
100+
Project.where(school_id: school.id).count
101+
]
102+
}
103+
end
104+
91105
it 'assigns a teacher' do
92106
expect(Role.teacher.where(user_id: teacher_id, school_id: school.id)).to exist
93107
end

0 commit comments

Comments
 (0)