Commit 108bfda6 authored by Zac Wood's avatar Zac Wood

Properly update course sections instead of creating new ones

parent 5bb04ff1
Pipeline #5331 passed with stage
in 11 seconds
import { Controller } from 'stimulus'
import Turbolinks from 'turbolinks'
import { buildUrl } from '../utils'
export default class extends Controller {
static targets = ['input']
......@@ -11,4 +10,3 @@ export default class extends Controller {
Turbolinks.visit(url)
}
}
console.log('AHHH')
......@@ -10,7 +10,6 @@ class CourseSection < ApplicationRecord
# Ensure all necessary fields are present.
validates :name, presence: true
validates :crn, presence: true
validates :title, presence: true
validates :course_id, presence: true
validates :semester_id, presence: true
......@@ -45,4 +44,23 @@ class CourseSection < ApplicationRecord
.where("instructors.name LIKE ?", "%#{name}%")
.select('course_sections.*, instructors.name as instructor_name')
end
def self.find_or_update_by!(s)
base_attrs = { crn: s[:crn], course: s[:course], semester: s[:semester] }
section = self.find_by(base_attrs)
if section.nil?
section = CourseSection.new(base_attrs)
end
section.title = s[:title]
section.start_date = s[:start_date]
section.end_date = s[:end_date]
section.days = s[:days]
section.start_time = s[:start_time]
section.end_time = s[:end_time]
section.location = s[:location]
section.instructor = s[:instructor]
section.save!
section
end
end
......@@ -13,10 +13,12 @@ class Semester < ApplicationRecord
"#{season} #{year}"
end
# Sorts semesters in descending temporal order.
# i.e. Fall 2020, Summer 2020, Spring 2020, Fall 2019, ...
def self.sorted_by_date(sems = Semester.all)
sems.sort do |s1, s2|
if s2.year != s1.year
s2.year <=> s1.year
s2.year <=> s1.year
else
case
when s1.season == "Fall"
......
......@@ -86,7 +86,9 @@ def load_sections(sections_in, semester)
semester: semester)
end
all_sections.each { |s| CourseSection.find_or_create_by! s }
all_sections.each do |s|
CourseSection.find_or_update_by!(s)
end
end
end
......
......@@ -4,15 +4,13 @@ class CourseSectionTest < ActiveSupport::TestCase
test 'fails with improper data' do
assert_raise do
CourseSection.create! name: nil,
crn: nil,
title: nil
crn: nil
end
end
test 'succeeds with proper data' do
CourseSection.create! name: 'Test section',
crn: '12345',
title: 'Test title',
course: courses(:cs211),
instructor: instructors(:luke),
semester: semesters(:fall2018)
......@@ -27,4 +25,55 @@ class CourseSectionTest < ActiveSupport::TestCase
s = CourseSection.latest_by_crn(70192)
assert_equal semesters(:fall2018).id, s.semester.id
end
test '#find_or_update_by! correctly updates' do
initial = {
name: 'CS 211 099',
crn: '99999',
title: 'Test title',
start_date: '2019-01-01',
end_date: '2019-01-02',
days: 'MWF',
start_time: '1:00pm',
end_time: '2:00pm',
location: 'ENGR',
course: courses(:cs211),
instructor: instructors(:luke),
semester: semesters(:fall2018)
}
a = CourseSection.create!(initial)
updated = {
name: 'CS 211 099',
crn: '99999',
title: 'New title',
start_date: '2019-02-01',
end_date: '2019-02-02',
days: 'TR',
start_time: '4:00pm',
end_time: '5:00pm',
location: 'SUB 1',
course: courses(:cs211),
instructor: instructors(:kinga),
semester: semesters(:fall2018)
}
b = CourseSection.find_or_update_by!(updated)
# reload a with the new attributes
a.reload
# ensure each field matches the updated value
updated.each do |key, value|
obj_val = b.method(key).call
if obj_val.is_a? Date
assert_equal value, obj_val.strftime("%Y-%m-%d")
else
assert_equal value, obj_val
end
assert_equal a.method(key).call, b.method(key).call
end
assert_equal a.id, b.id
end
end
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