|
| 1 | + |
| 2 | + |
| 3 | +class StudentCsvImporter |
| 4 | + class InvalidClassroomError < StandardError; end |
| 5 | + class InvalidProgramError < StandardError; end |
| 6 | + class InvalidStudentError < StandardError; end |
| 7 | + class InvalidTeacherError < StandardError; end |
| 8 | + |
| 9 | + def initialize(csv:, school_id:) |
| 10 | + @csv = csv |
| 11 | + @school_id = school_id |
| 12 | + @error_messages = { |
| 13 | + classrooms: {}, |
| 14 | + teachers: {}, |
| 15 | + students: {} |
| 16 | + } |
| 17 | + @classrooms = {} |
| 18 | + @teachers = {} |
| 19 | + @students = [] |
| 20 | + @row_indexes = {} |
| 21 | + end |
| 22 | + |
| 23 | + |
| 24 | + def import |
| 25 | + @csv.each_with_index do |row, index| |
| 26 | + next if row.blank? |
| 27 | + |
| 28 | + teacher = find_or_build_teacher(row) |
| 29 | + classroom = find_or_build_classroom(row, teacher) |
| 30 | + |
| 31 | + student = Student.new( |
| 32 | + first_name: row["Student First Name"], |
| 33 | + last_name: row["Student Last Name"], |
| 34 | + grade_level: row["Grade Level"], |
| 35 | + school_id: @school_id, |
| 36 | + classroom: classroom |
| 37 | + ) |
| 38 | + |
| 39 | + collect_errors(:teachers, teacher, index) |
| 40 | + collect_errors(:classrooms, classroom, index) |
| 41 | + collect_errors(:students, student, index) |
| 42 | + |
| 43 | + @students << student |
| 44 | + end |
| 45 | + |
| 46 | + save_records |
| 47 | + end |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +private |
| 52 | + |
| 53 | + def save_records |
| 54 | + ActiveRecord::Base.transaction do |
| 55 | + raise_validation_errors! |
| 56 | + |
| 57 | + save_all(@teachers.values, InvalidTeacherError) |
| 58 | + save_all(@classrooms.values, InvalidClassroomError) |
| 59 | + save_all(@students, InvalidStudentError) |
| 60 | + |
| 61 | + create_program_associations |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + # A uniqueness collision between two rows of the same file survives `valid?`, |
| 66 | + # because neither record is persisted yet. Re-raise it in the shape the |
| 67 | + # validation errors already use so callers only have to handle one thing. |
| 68 | + def save_all(records, error_class) |
| 69 | + records.each do |record| |
| 70 | + record.save! |
| 71 | + rescue ActiveRecord::RecordInvalid => error |
| 72 | + raise error_class, { @row_indexes[record.object_id] => error.record.errors.full_messages }.to_s |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + def create_program_associations |
| 77 | + @csv.each do |row| |
| 78 | + teacher = @teachers[row["Teacher"]] |
| 79 | + classroom = @classrooms[[ teacher.name, row["Class Name"] ]] |
| 80 | + |
| 81 | + program = find_existing_program!(row["Program"]) |
| 82 | + |
| 83 | + classroom.classroom_programs.create_or_find_by!(program: program, level: row["Program Level"]) |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + def find_existing_program!(name) |
| 88 | + Program.find_by!(name: name) |
| 89 | + rescue ActiveRecord::RecordNotFound |
| 90 | + raise InvalidProgramError, "Program: #{name} does not exist" |
| 91 | + end |
| 92 | + |
| 93 | + def collect_errors(type, record, index) |
| 94 | + @row_indexes[record.object_id] ||= index |
| 95 | + |
| 96 | + return if record.valid? |
| 97 | + |
| 98 | + @error_messages[type][index] = record.errors.full_messages |
| 99 | + end |
| 100 | + |
| 101 | + def raise_validation_errors! |
| 102 | + raise InvalidClassroomError, @error_messages[:classrooms].to_s if @error_messages[:classrooms].any? |
| 103 | + raise InvalidStudentError, @error_messages[:students].to_s if @error_messages[:students].any? |
| 104 | + raise InvalidTeacherError, @error_messages[:teachers].to_s if @error_messages[:teachers].any? |
| 105 | + end |
| 106 | + |
| 107 | + def find_or_build_teacher(row) |
| 108 | + @teachers[row["Teacher"]] ||= Teacher.new( |
| 109 | + name: row["Teacher"], |
| 110 | + email: row["Teacher Email"], |
| 111 | + school_id: @school_id |
| 112 | + ) |
| 113 | + end |
| 114 | + |
| 115 | + def find_or_build_classroom(row, teacher) |
| 116 | + key = [ teacher.name, row["Class Name"] ] |
| 117 | + |
| 118 | + @classrooms[key] ||= Classroom.new( |
| 119 | + school_id: @school_id, |
| 120 | + name: row["Class Name"], |
| 121 | + teacher: teacher |
| 122 | + ) |
| 123 | + end |
| 124 | +end |
0 commit comments