Commit a8bb6587 authored by Zach Perkins's avatar Zach Perkins
Browse files

Made course adding a lot faster. Like a lot faster. Like really fast.

parent 5d53d711
...@@ -41,16 +41,36 @@ Semester.delete_all ...@@ -41,16 +41,36 @@ Semester.delete_all
semester = Semester.create! season: 'Fall', year: 2018 semester = Semester.create! season: 'Fall', year: 2018!!
all_sections = [] # Taking a course and a list of courses, checks if the course is already in that
# list. If it isn't, create an active record and store it in the list for that
# course. If it is, grab the pre-existing database entry.
def get_course(course, all_courses)
all_courses.each do |c|
if c[:section] == course[:section]
return c[:db_object]
course[:db_object] = Course.create!(course)
total.each do |subject, sections| total.each do |subject, sections|
puts "Adding courses for #{subject}..." puts "Adding courses for #{subject}..."
all_sections = []
all_courses = []
sections.each do |section| sections.each do |section|
if section.nil? || !section.key?(:subj) || !section.key?(:course_number) if section.nil? || !section.key?(:subj) || !section.key?(:course_number)
puts "#{subject} failed section: #{section.class}" puts "#{subject} failed section: #{section.class}"
next next
end end
course = get_course({ subject: section[:subj],
course_number: section[:course_number],
semester: semester }, all_courses)
section_name = "#{section[:subj]} #{section[:course_number]} #{section[:section]}" section_name = "#{section[:subj]} #{section[:course_number]} #{section[:section]}"
all_sections.push(name: section_name, all_sections.push(name: section_name,
...@@ -65,21 +85,10 @@ total.each do |subject, sections| ...@@ -65,21 +85,10 @@ total.each do |subject, sections|
end_time: section[:end_time], end_time: section[:end_time],
location: section[:location], location: section[:location],
course: course) course: course)
# Find or create a course and set its semester
# TODO: this breaks when you try to do more than one semester,
# since just the subject + course_number do not uniquely identify a course
# Check the semester as well
course = Course.find_or_create_by(subject: section[:subj],
course_number: section[:course_number])
course.semester = semester!
# puts "Adding #{section_name}..."
end end
CourseSection.create!(all_sections) CourseSection.create!(all_sections)
# create closures for the days there will be no classes # create closures for the days there will be no classes
# see: # see:
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment