Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/tasks/seeds_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def verify_school(school)
# rubocop:enable Rails/SkipsModelValidations
end

def create_school_class(teacher_id, school, name = Faker::Educator.course_name, description = Faker::Hacker.phrases.sample)
def create_school_class(teacher_id, school, name = Faker::Educator.course_name, description = Faker::Hacker.phrases.sample, join_code = nil)
SchoolClass.joins(:teachers)
.where(teachers: { teacher_id: }, school:)
.first_or_create! do |school_class|
Expand All @@ -73,6 +73,7 @@ def create_school_class(teacher_id, school, name = Faker::Educator.course_name,
school_class.description = description
school_class.school = school
school_class.teachers = [ClassTeacher.new(teacher_id:)]
school_class.join_code = join_code
end
end

Expand Down
32 changes: 25 additions & 7 deletions lib/tasks/test_seeds.rake
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

require_relative 'seeds_helper'

SchoolClassData = Data.define(:owner, :name, :description, :code, :assign_students)

namespace :test_seeds do
include SeedsHelper

Expand Down Expand Up @@ -48,6 +50,25 @@ namespace :test_seeds do
end
end

def school_class_data
[
SchoolClassData.new(
owner: ENV.fetch('SEEDING_CREATOR_ID', TEST_USERS[:jane_doe]),
name: "Jane Doe's Class",
description: "A class for Jane Doe's students",
code: 'C000-C000',
assign_students: false
),
SchoolClassData.new(
owner: ENV.fetch('SEEDING_CREATOR_ID', TEST_USERS[:john_doe]),
Comment thread
zetter-rpf marked this conversation as resolved.
name: "John Doe's Class",
description: "A class for John Doe's students",
code: 'C000-C001',
assign_students: true
)
]
end

desc 'Create a school with lessons and students'
task create: :environment do
if School.exists?(id: TEST_SCHOOL)
Expand All @@ -72,13 +93,10 @@ namespace :test_seeds do
assign_a_teacher(teacher_id, school)
assign_a_teacher(teacher_unassigned_id, school)

# for each of the owner and teacher, create a class and assign students
[creator_id, teacher_id].each do |user_id|
teacher_name = user_id == creator_id ? 'Jane Doe' : 'John Doe'
school_class = create_school_class(user_id, school, "#{teacher_name}'s Class", "A class for #{teacher_name}'s students")
assign_students(school_class, school)

create_lessons(user_id, school, school_class)
school_class_data.each do |school_class|
created_school_class = create_school_class(school_class.owner, school, school_class.name, school_class.description, school_class.code)
assign_students(created_school_class, school) if school_class.assign_students
create_lessons(school_class.owner, school, created_school_class)
end
Rails.logger.info 'Done...'
end
Expand Down
3 changes: 1 addition & 2 deletions spec/lib/test_seeds_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,7 @@
end

it 'assigns students' do
school_id = School.find_by(creator_id:).id
school_class_id = SchoolClass.find_by(school_id:).id
school_class_id = SchoolClass.joins(:teachers).find_by!(school_id:, teachers: { teacher_id: }).id
expect(Role.student.where(user_id: student_1, school_id:)).to exist
expect(ClassStudent.where(student_id: student_1, school_class_id:)).to exist
expect(Role.student.where(user_id: student_2, school_id:)).to exist
Expand Down
Loading