2016-09-08 10:25:33 -05:00
|
|
|
# frozen_string_literal: true
|
2016-07-26 17:00:00 -05:00
|
|
|
class Candidate < ApplicationRecord
|
2016-07-27 11:17:50 -05:00
|
|
|
belongs_to :quiz
|
2016-07-28 20:54:08 -05:00
|
|
|
has_many :questions, -> { order("sort") }, through: :quiz
|
2016-07-26 17:00:00 -05:00
|
|
|
has_many :answers
|
|
|
|
belongs_to :recruiter, class_name: "User"
|
2016-11-18 17:43:24 -06:00
|
|
|
has_many :votes, class_name: "ReviewerVote"
|
2016-11-20 13:07:53 -06:00
|
|
|
has_many :reviewers, through: :quiz
|
2017-02-10 12:03:50 -06:00
|
|
|
has_many :quiz_comments, foreign_key: :test_hash, primary_key: :test_hash
|
2016-07-26 17:00:00 -05:00
|
|
|
|
2016-08-26 16:03:55 -05:00
|
|
|
serialize :email, CryptSerializer
|
|
|
|
|
2016-07-31 14:47:15 -05:00
|
|
|
before_validation(:generate_test_hash, on: :create)
|
2016-07-26 17:00:00 -05:00
|
|
|
|
2016-09-08 11:52:34 -05:00
|
|
|
validates :recruiter_id, presence: true
|
|
|
|
validates :name, presence: true
|
|
|
|
validates :experience, presence: true
|
2017-02-08 16:05:37 -06:00
|
|
|
validates :project, presence: true
|
2017-02-27 11:11:28 -06:00
|
|
|
validates :position, presence: true
|
2016-07-31 14:47:15 -05:00
|
|
|
validates :email, uniqueness: true, presence: true, email_format: true
|
|
|
|
validates :test_hash, uniqueness: true, presence: true
|
2016-07-26 17:00:00 -05:00
|
|
|
|
2016-11-18 17:43:24 -06:00
|
|
|
enum review_status: {
|
2016-11-20 11:24:17 -06:00
|
|
|
pending: 0,
|
2016-11-18 17:43:24 -06:00
|
|
|
approved: 1,
|
|
|
|
declined: 2
|
|
|
|
}
|
|
|
|
|
2016-11-28 15:40:25 -06:00
|
|
|
def interview?
|
|
|
|
return 'yes' if approved?
|
2017-05-01 16:17:41 -05:00
|
|
|
return 'no' if declined?
|
|
|
|
'-'
|
2016-11-28 15:40:25 -06:00
|
|
|
end
|
|
|
|
|
2016-11-20 13:07:53 -06:00
|
|
|
def build_reviews
|
|
|
|
reviewers.each do |reviewer|
|
|
|
|
votes.find_or_create_by(user_id: reviewer.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-09 15:38:38 -06:00
|
|
|
def manager
|
|
|
|
manager_votes = votes.joins(:user).where("users.role = 'manager'")
|
2017-02-27 09:11:07 -06:00
|
|
|
return User.new(name: "No Manager") if manager_votes.empty?
|
2017-02-09 15:38:38 -06:00
|
|
|
|
|
|
|
manager_votes.first.user
|
|
|
|
end
|
|
|
|
|
2016-07-27 11:17:50 -05:00
|
|
|
def submitted_answers
|
|
|
|
answers.where(submitted: true)
|
|
|
|
end
|
|
|
|
|
2017-04-18 10:39:49 -05:00
|
|
|
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
|
|
|
|
|
2016-07-27 11:17:50 -05:00
|
|
|
def answered_questions
|
|
|
|
answers.where.not(answer: nil)
|
2016-08-12 12:44:19 -05:00
|
|
|
.where("answers.answer not like '%later:%'")
|
2016-07-27 11:17:50 -05:00
|
|
|
end
|
|
|
|
|
2016-07-29 09:46:47 -05:00
|
|
|
def fetch_question qid
|
2016-08-16 14:19:45 -05:00
|
|
|
CandidateQuiz.new(id).fetch_question(qid)
|
2016-07-29 09:46:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def my_quiz
|
2016-08-12 12:44:19 -05:00
|
|
|
@candidate_quiz ||= CandidateQuiz.new(id).build_my_quiz
|
|
|
|
end
|
|
|
|
|
|
|
|
def my_status
|
|
|
|
@candidate_status ||= QuizStatus.new(self)
|
2016-07-29 09:46:47 -05:00
|
|
|
end
|
|
|
|
|
2016-07-31 09:56:02 -05:00
|
|
|
def status
|
2016-08-12 12:44:19 -05:00
|
|
|
"#{my_status.progress}%"
|
2016-07-31 09:56:02 -05:00
|
|
|
end
|
|
|
|
|
2016-07-26 17:00:00 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def generate_test_hash
|
|
|
|
loop do
|
|
|
|
self[:test_hash] = SecureRandom.urlsafe_base64(8)
|
|
|
|
|
|
|
|
break unless Candidate.exists?(test_hash: self[:test_hash])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|