Commit 212e258a authored by Zac Wood's avatar Zac Wood
Browse files

Added data + DB seed script

parent 6968dd63
......@@ -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
# 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"
create_table "sections", force: :cascade do |t|
t.string "name"
t.string "crn"
t.string "section_type"
t.string "title"
t.string "instructor" "start_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"
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
......@@ -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"
# 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
puts "Created course named: #{current_course.subject} #{current_course.course_number}"
# If there is no valid section name, just continue to the next row
if !section_name || section_name&.empty? || section_name == 'Total'
# Create the new section
section =
# Add all fields to the section, ensuring each is valid = section_name
section.course = current_course
if crn
section.crn = crn
if schedule_type
section.section_type = schedule_type
if section_title
section.title = section_title
if instructor
section.instructor = instructor
if start_date
section.start_date = start_date
if end_date
section.end_date = end_date
if days
section.days = days
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
if location
section.location = location
# Save the section to the database
puts "Created section for named #{}"
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