skill-assessment-app/app/models/candidate.rb
2016-07-31 14:47:15 -05:00

47 lines
1.0 KiB
Ruby

class Candidate < ApplicationRecord
belongs_to :quiz
has_many :questions, -> { order("sort") }, through: :quiz
has_many :answers
belongs_to :recruiter, class_name: "User"
before_validation(:generate_test_hash, on: :create)
validates_presence_of :name
validates_presence_of :email
validates_presence_of :experience
validates_presence_of :recruiter_id
validates :email, uniqueness: true, presence: true, email_format: true
validates :test_hash, uniqueness: true, presence: true
def submitted_answers
answers.where(submitted: true)
end
def answered_questions
answers.where.not(answer: nil)
end
def fetch_question qid
CandidateQuiz.new(id).fetch_question(qid).first
end
def my_quiz
CandidateQuiz.new(id).build_my_quiz
end
def status
# TODO: quiz status: not started, started, completed
"--"
end
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