skill-assessment-app/app/models/candidate.rb

60 lines
1.4 KiB
Ruby
Raw Normal View History

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-07-31 14:47:15 -05:00
before_validation(:generate_test_hash, on: :create)
2016-07-26 17:00:00 -05:00
validates_presence_of :recruiter_id
2016-07-31 14:47:15 -05:00
validates_presence_of :name
validates_presence_of :experience
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-07-27 11:17:50 -05:00
def submitted_answers
answers.where(submitted: true)
end
def answered_questions
answers.where.not(answer: nil)
.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
CandidateQuiz.new(id).fetch_question(qid).first
end
def my_quiz
@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
"#{my_status.progress}%"
2016-07-31 09:56:02 -05:00
end
2016-08-02 16:27:27 -05:00
def complete!
if update_attributes(completed: true)
CandidateMailer.submitted(self).deliver_now
RecruiterMailer.candidate_submitted(self).deliver_now
ReviewerMailer.candidate_submission(self).deliver_now
return true
end
false
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