2016-07-26 17:00:00 -05:00
|
|
|
class Candidate < ApplicationRecord
|
2016-07-27 11:17:50 -05:00
|
|
|
belongs_to :quiz
|
|
|
|
has_many :questions, through: :quiz
|
2016-07-26 17:00:00 -05:00
|
|
|
has_many :answers
|
|
|
|
belongs_to :recruiter, class_name: "User"
|
|
|
|
|
|
|
|
before_create :generate_test_hash
|
|
|
|
|
|
|
|
validates_presence_of :recruiter_id
|
|
|
|
validates_presence_of :test_hash
|
|
|
|
validates_uniqueness_of :test_hash
|
|
|
|
|
2016-07-27 11:17:50 -05:00
|
|
|
def submitted_answers
|
|
|
|
answers.where(submitted: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def answered_questions
|
|
|
|
answers.where.not(answer: nil)
|
|
|
|
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
|