From bbc58c3f535257ab424527205dfa4e3aa8cd080e Mon Sep 17 00:00:00 2001 From: Matt Bernhardt Date: Fri, 9 Apr 2021 17:03:51 -0400 Subject: [PATCH] Fix bugs around thesis graduation date ** Why are these changes being introduced: * There is a bug in the thesis model which prevents updating thesis records via the admin dashboard. We specify graduation dates in the UI via separate month and year fields, but the information gets stored internally as a unified date. There is a method to build this date, but it only called on record creation - not when the record is updated * Additionally, the admin dashboard currently imposes conflicting requirements on the graduation_month field, being both a number and the name of the month. ** Relevant ticket(s): * https://mitlibraries.atlassian.net/browse/ETD-216 ** How does this address that need: * This updates the thesis model to call combine_gradaution_date every time the record gets saved, not just for initial creation. * This change also adds unit tests to confirm that this happens on both creation and save. * Another change is to make the admin dashboard treat months as a text string, rather than a number. Ultimately it might need to be a dropdown, but we can start here. ** Document any side effects to this change: None --- app/dashboards/thesis_dashboard.rb | 2 +- app/models/thesis.rb | 2 +- test/models/thesis_test.rb | 27 +++++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/app/dashboards/thesis_dashboard.rb b/app/dashboards/thesis_dashboard.rb index 98d34aa2..c6491ce5 100644 --- a/app/dashboards/thesis_dashboard.rb +++ b/app/dashboards/thesis_dashboard.rb @@ -21,7 +21,7 @@ class ThesisDashboard < Administrate::BaseDashboard grad_date: Field::DateTime.with_options( format: "%Y %B" ), - graduation_month: Field::Number, + graduation_month: Field::Text, graduation_year: Field::Number, created_at: Field::DateTime, updated_at: Field::DateTime, diff --git a/app/models/thesis.rb b/app/models/thesis.rb index 83dfe2df..98d67704 100644 --- a/app/models/thesis.rb +++ b/app/models/thesis.rb @@ -82,7 +82,7 @@ class Thesis < ApplicationRecord VALID_MONTHS = ['February', 'May', 'June', 'September'] - before_create :combine_graduation_date + before_save :combine_graduation_date after_find :split_graduation_date #scope :name_asc, lambda { diff --git a/test/models/thesis_test.rb b/test/models/thesis_test.rb index 393a48b1..5bfd95f1 100644 --- a/test/models/thesis_test.rb +++ b/test/models/thesis_test.rb @@ -101,6 +101,33 @@ class ThesisTest < ActiveSupport::TestCase assert(thesis.invalid?) end + test 'combine grad_date from month and year for new theses' do + t = Thesis.new() + t.title = 'Sample' + t.abstract = 'abstract' + t.users.append(users(:yo)) + t.departments.append(departments(:one)) + t.graduation_month = "February" + t.graduation_year = "2020" + assert t.valid? + t.save + + assert_equal "2020-02-01", t.grad_date.to_s + end + + test 'combine grad_date from month and year for thesis updates' do + t = Thesis.second + old_year = t.graduation_year + old_date = t.grad_date + t.graduation_year = t.graduation_year.to_i + 1 + assert t.valid? + t.save + + t = Thesis.second + assert_not_equal old_year.to_s, t.graduation_year.to_s + assert_not_equal old_date.to_s, t.grad_date.to_s + end + test 'valid with multiple authors' do t = theses(:two) assert(t.authors.count > 1)