diff --git a/app/helpers/audit_helper.rb b/app/helpers/audit_helper.rb index 0eb913dfa..3762c1a86 100644 --- a/app/helpers/audit_helper.rb +++ b/app/helpers/audit_helper.rb @@ -3,7 +3,7 @@ def display_audit_value(value, field) return "(none)" if value.blank? return Questionnaire::POSSIBLE_ACC_STATUS[value] if field == "acc_status" return BusList.find(value)&.name || value if field == "bus_list_id" - return User.find(value)&.full_name || value if field == "checked_in_by_id" + return User.find_by_id(value)&.full_name || "(deleted user)" if field == "checked_in_by_id" return value.join(", ") if value.is_a? Array return display_datetime(value, relative: false) if value.is_a? Time diff --git a/app/models/questionnaire.rb b/app/models/questionnaire.rb index 024097343..0f692613a 100644 --- a/app/models/questionnaire.rb +++ b/app/models/questionnaire.rb @@ -159,7 +159,7 @@ def date_of_birth_formatted def acc_status_author return unless acc_status_author_id.present? - User.find(acc_status_author_id) + User.find_by_id(acc_status_author_id) end def checked_in? @@ -172,7 +172,7 @@ def boarded_bus? def checked_in_by return unless checked_in_by_id.present? - User.find(checked_in_by_id) + User.find_by_id(checked_in_by_id) end def fips_code diff --git a/app/views/manage/questionnaires/_checkin_card.html.haml b/app/views/manage/questionnaires/_checkin_card.html.haml index ed67a53c8..4a237f320 100644 --- a/app/views/manage/questionnaires/_checkin_card.html.haml +++ b/app/views/manage/questionnaires/_checkin_card.html.haml @@ -5,7 +5,13 @@ = render 'manage/questionnaires/check_in_badge' - if @questionnaire.checked_in_at %small - = @questionnaire.checked_in_by_id ? @questionnaire.checked_in_by.email : "(never checked in)" + - if @questionnaire.checked_in_by_id + - if @questionnaire.checked_in_by + = @questionnaire.checked_in_by.email + - else + = "(deleted user)" + - else + = "(never checked in)" = @questionnaire.checked_in_at ? display_datetime(@questionnaire.checked_in_at, in_sentence: true) : "(not checked in)" - if !@questionnaire.checked_in_at %p.card-text diff --git a/app/views/manage/questionnaires/show.html.haml b/app/views/manage/questionnaires/show.html.haml index a934d65bc..429872c48 100644 --- a/app/views/manage/questionnaires/show.html.haml +++ b/app/views/manage/questionnaires/show.html.haml @@ -32,7 +32,13 @@ %p.card-text = render 'acc_status_badge' %small - = @questionnaire.acc_status_author_id ? @questionnaire.acc_status_author.email : "(no author)" + - if @questionnaire.acc_status_author_id + - if @questionnaire.acc_status_author + = @questionnaire.acc_status_author.email + - else + = "(deleted user)" + - else + = "(no author)" = @questionnaire.acc_status_date ? display_datetime(@questionnaire.acc_status_date, in_sentence: true) : "(no date)" - if current_user.admin? = bs_vertical_simple_form @questionnaire, url: url_for(action: "update_acc_status", controller: "questionnaires") do |f| diff --git a/test/models/questionnaire_test.rb b/test/models/questionnaire_test.rb index cb60e67ea..e89c026f8 100644 --- a/test/models/questionnaire_test.rb +++ b/test/models/questionnaire_test.rb @@ -204,6 +204,13 @@ class QuestionnaireTest < ActiveSupport::TestCase assert_nil questionnaire.acc_status_author end + should "return nil if author deleted" do + user = create(:user, email: "admin@example.com") + questionnaire = create(:questionnaire, acc_status_author_id: user.id) + user.destroy + assert_nil questionnaire.acc_status_author + end + should "return the questionnaire's user" do user = create(:user, email: "admin@example.com") questionnaire = create(:questionnaire, acc_status_author_id: user.id) @@ -402,6 +409,13 @@ class QuestionnaireTest < ActiveSupport::TestCase assert_nil questionnaire.checked_in_by_id end + should "return no one if user who checked-in questionnaire is deleted" do + user = create(:user) + questionnaire = create(:questionnaire, checked_in_by_id: user.id) + user.destroy + assert_nil questionnaire.checked_in_by + end + should "return user who checked in ther questionnaire" do user = create(:user) questionnaire = create(:questionnaire, checked_in_by_id: user.id)