From 49035929fb03cab75c7888be171d110d516c6e59 Mon Sep 17 00:00:00 2001 From: Mark Moser Date: Tue, 18 Apr 2017 10:39:49 -0500 Subject: [PATCH] bug: resolve false positive "oops" redirect --- app/controllers/candidate_controller.rb | 1 + app/models/candidate.rb | 11 +++++++++++ test/models/candidate_test.rb | 19 +++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/app/controllers/candidate_controller.rb b/app/controllers/candidate_controller.rb index 0cd4f4c..e052666 100644 --- a/app/controllers/candidate_controller.rb +++ b/app/controllers/candidate_controller.rb @@ -45,6 +45,7 @@ class CandidateController < ApplicationController end def send_to_oops + redirect_to welcome_path and return if current_candidate && current_candidate.stale? redirect_to oops_path if current_candidate end end diff --git a/app/models/candidate.rb b/app/models/candidate.rb index 56083a1..8587779 100644 --- a/app/models/candidate.rb +++ b/app/models/candidate.rb @@ -48,6 +48,17 @@ class Candidate < ApplicationRecord answers.where(submitted: true) end + def last_answered_at + return Time.current unless submitted_answers.count.positive? + submitted_answers.order(updated_at: :desc).first.updated_at + end + + def stale? + return true unless answers.count.positive? + minutes_since_answered = (Time.current.minus_with_coercion(last_answered_at) / 60).round + minutes_since_answered > 45 + end + def answered_questions answers.where.not(answer: nil) .where("answers.answer not like '%later:%'") diff --git a/test/models/candidate_test.rb b/test/models/candidate_test.rb index 5b9ea44..4d5154e 100644 --- a/test/models/candidate_test.rb +++ b/test/models/candidate_test.rb @@ -34,4 +34,23 @@ class CandidateTest < ActiveSupport::TestCase candidate.build_reviews assert_equal 3, candidate.votes.count end + + test 'can get last answer timestamp' do + candidate = candidates(:roy) + roy_last = answers(:roy2).updated_at + + assert_equal roy_last, candidate.last_answered_at + end + + test 'gillian is stale with no answers' do + candidate = candidates(:gillian) + + assert candidate.stale? + end + + test 'roy is stale with answers' do + candidate = candidates(:roy) + + assert candidate.stale? + end end