diff --git a/schedules/Gemfile b/schedules/Gemfile index 85634cf5df1dbcadc5f00b3ef655ac25a733c35a..597f936e7b49b19a881226fc07c6dfa85a347d40 100644 --- a/schedules/Gemfile +++ b/schedules/Gemfile @@ -52,3 +52,5 @@ end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] + +gem 'rubyXL' \ No newline at end of file diff --git a/schedules/db/data/allsections.xlsx b/schedules/db/data/allsections.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..2ea3d4b8570f5aa6b761f842c99fa33f105325d5 Binary files /dev/null and b/schedules/db/data/allsections.xlsx differ diff --git a/schedules/db/data/small.xlsx b/schedules/db/data/small.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..a712936177fe367e7fb673ec65e0b364ed7b6b18 Binary files /dev/null and b/schedules/db/data/small.xlsx differ diff --git a/schedules/db/schema.rb b/schedules/db/schema.rb new file mode 100644 index 0000000000000000000000000000000000000000..512881ccf54ee47f0972c1b762fe1b438baee970 --- /dev/null +++ b/schedules/db/schema.rb @@ -0,0 +1,53 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 20180407190750) do + + create_table "courses", force: :cascade do |t| + t.string "subject" + t.string "course_number" + t.integer "semester_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["semester_id"], name: "index_courses_on_semester_id" + end + + create_table "sections", force: :cascade do |t| + t.string "name" + t.string "crn" + t.string "section_type" + t.string "title" + t.string "instructor" + t.date "start_date" + t.date "end_date" + t.string "days" + t.string "start_time" + t.string "end_time" + t.string "location" + t.string "status" + t.string "campus" + t.string "notes" + t.integer "size_limit" + t.integer "course_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["course_id"], name: "index_sections_on_course_id" + end + + create_table "semesters", force: :cascade do |t| + t.string "season" + t.string "year" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + +end diff --git a/schedules/db/seeds.rb b/schedules/db/seeds.rb index 1beea2accd7ed91d080b323eef17dba95e95e27f..745a3483362cbf4d9e6454d2884a885d71e2ea9b 100644 --- a/schedules/db/seeds.rb +++ b/schedules/db/seeds.rb @@ -5,3 +5,98 @@ # # movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) # Character.create(name: 'Luke', movie: movies.first) + +require 'rubyXL' + +# Open the data file...this takes a while +workbook = RubyXL::Parser.parse('db/data/allsections.xlsx') +rows = workbook[0] + +puts 'Done parsing!' + +# The first 16 rows are not actual data, so remove them +rows = rows.drop(16) + +# Keep track of the course each section is for +current_course = nil + +# Create the Semester object that all courses will belong to +semester = Semester.create season: "Fall", year: "2018" +semester.save + +# Loop through all the rows in the date +rows.each do | row | + # Get all the info out of the current row + course_name = row&.cells[1]&.value + section_name = row&.cells[2]&.value + crn = row&.cells[6]&.value + schedule_type = row&.cells[8]&.value + section_title = row&.cells[11]&.value + instructor = row&.cells[16]&.value + start_date = row&.cells[18]&.value + end_date = row&.cells[21]&.value + days = row&.cells[22]&.value + times = row&.cells[23]&.value + location = row&.cells[25]&.value + + # Ensure the course name is valid + if course_name && !course_name.empty? && course_name != 'Total' + + # Split the name into its two components, i.e. "CS 112" => ["CS", "112"] + name_components = course_name.split(' ') + + # Create and save the course, and set it to be the current_course + current_course = Course.create subject: name_components[0], course_number: name_components[1], semester: semester + current_course.save + + puts "Created course named: #{current_course.subject} #{current_course.course_number}" + end + + # If there is no valid section name, just continue to the next row + if !section_name || section_name&.empty? || section_name == 'Total' + next + else + # Create the new section + section = Section.new + + # Add all fields to the section, ensuring each is valid + section.name = section_name + section.course = current_course + if crn + section.crn = crn + end + if schedule_type + section.section_type = schedule_type + end + if section_title + section.title = section_title + end + if instructor + section.instructor = instructor + end + if start_date + section.start_date = start_date + end + if end_date + section.end_date = end_date + end + if days + section.days = days + end + if times + # The time field in the spreadsheet uses the format "start_time - end_time" i.e. "12:00 PM - 1:15 PM". + # So, split the times string by the - character + time_strs = times.split('-') + section.start_time = time_strs[0].strip + section.end_date = time_strs[1].strip + end + if location + section.location = location + end + + # Save the section to the database + section.save + + puts "Created section for named #{section.name}" + end +end