skill-assessment-app/app/models/candidate.rb
Mark Moser 4bbd93ded1 rubocop noise: fixes FrozenStringLiteralComment
Adding the .ruby-verison file triggered previously un-run cops, specifically:

  This cop is designed to help upgrade to Ruby 3.0. It will add the
  comment `# frozen_string_literal: true` to the top of files to enable
  frozen string literals. Frozen string literals will be default in Ruby
  3.0. The comment will be added below a shebang and encoding comment. The
  frozen string literal comment is only valid in Ruby 2.3+.

More info on rubocop [Automatic-Corrections](https://github.com/bbatsov/rubocop/wiki/Automatic-Corrections)
2016-09-08 10:30:13 -05:00

66 lines
1.6 KiB
Ruby

# frozen_string_literal: true
class Candidate < ApplicationRecord
belongs_to :quiz
has_many :questions, -> { order("sort") }, through: :quiz
has_many :answers
belongs_to :recruiter, class_name: "User"
serialize :email, CryptSerializer
before_validation(:generate_test_hash, on: :create)
validates_presence_of :recruiter_id
validates_presence_of :name
validates_presence_of :experience
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)
.where("answers.answer not like '%later:%'")
end
def fetch_question qid
CandidateQuiz.new(id).fetch_question(qid)
end
def my_quiz
@candidate_quiz ||= CandidateQuiz.new(id).build_my_quiz
end
def my_status
@candidate_status ||= QuizStatus.new(self)
end
def status
"#{my_status.progress}%"
end
# FIXME: This feels wrong here. Mail deliveries should be in controller.
# Privatize in QuizController
# also, bang methods in ruby do an action and replace, or return nil
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
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