2016-09-08 10:25:33 -05:00
|
|
|
# frozen_string_literal: true
|
2016-07-26 17:00:00 -05:00
|
|
|
class User < ApplicationRecord
|
|
|
|
has_secure_password
|
2016-09-15 13:26:31 -05:00
|
|
|
has_many :candidates, foreign_key: :recruiter_id
|
|
|
|
has_many :reviewer_to_quizzes
|
|
|
|
has_many :quizzes, through: :reviewer_to_quizzes
|
2016-11-17 22:43:19 -06:00
|
|
|
has_many :votes, class_name: 'ReviewerVote'
|
2016-08-18 15:35:17 -05:00
|
|
|
|
2016-11-20 09:51:46 -06:00
|
|
|
has_many :reviewees, through: :quizzes, source: :candidates
|
|
|
|
|
2016-08-25 19:10:58 -05:00
|
|
|
validates :email, presence: true, uniqueness: true
|
|
|
|
validates :name, presence: true
|
|
|
|
validates :role, presence: true
|
2016-08-24 15:02:32 -05:00
|
|
|
validates :reset_token, uniqueness: true, allow_nil: true
|
|
|
|
|
|
|
|
def setup_reset
|
|
|
|
gen_reset_token
|
|
|
|
save
|
|
|
|
end
|
|
|
|
|
2016-11-19 16:34:48 -06:00
|
|
|
# Voting
|
2017-02-09 16:26:33 -06:00
|
|
|
# TODO: Refactor this out of User, belongs on ReviewerVote
|
|
|
|
# ie: cast_yea(candidate, user)
|
2016-11-19 16:34:48 -06:00
|
|
|
def cast_yea_on candidate
|
2016-11-20 13:07:53 -06:00
|
|
|
vote = votes.find_by(candidate_id: candidate.to_i)
|
2016-11-19 16:34:48 -06:00
|
|
|
vote.vote = :yea
|
|
|
|
vote.save
|
|
|
|
end
|
|
|
|
|
|
|
|
def cast_nay_on candidate
|
2016-11-20 13:07:53 -06:00
|
|
|
vote = votes.find_by(candidate_id: candidate.to_i)
|
2016-11-19 16:34:48 -06:00
|
|
|
vote.vote = :nay
|
|
|
|
vote.save
|
|
|
|
end
|
|
|
|
|
|
|
|
def approve_candidate candidate
|
|
|
|
candidate = Candidate.find(candidate.to_i)
|
|
|
|
|
2016-11-20 13:07:53 -06:00
|
|
|
vote = votes.find_by(candidate_id: candidate.to_i)
|
2016-11-19 16:34:48 -06:00
|
|
|
vote.veto = :approved
|
|
|
|
candidate.update_attribute(:review_status, :approved) if vote.save
|
|
|
|
end
|
|
|
|
|
|
|
|
def decline_candidate candidate
|
|
|
|
candidate = Candidate.find(candidate.to_i)
|
|
|
|
|
2016-11-20 13:07:53 -06:00
|
|
|
vote = votes.find_by(candidate_id: candidate.to_i)
|
2016-11-19 16:34:48 -06:00
|
|
|
vote.veto = :rejected
|
|
|
|
candidate.update_attribute(:review_status, :declined) if vote.save
|
|
|
|
end
|
|
|
|
|
2016-11-20 11:24:17 -06:00
|
|
|
def my_vote candidate
|
|
|
|
candidate = Candidate.find(candidate.to_i)
|
|
|
|
|
2016-11-20 13:07:53 -06:00
|
|
|
my_vote = votes.find_by(candidate_id: candidate.id)
|
|
|
|
my_vote.vote unless my_vote.nil?
|
2016-11-20 11:24:17 -06:00
|
|
|
end
|
|
|
|
|
2016-09-21 17:04:08 -05:00
|
|
|
# Roles
|
2016-09-20 14:22:20 -05:00
|
|
|
def admin?
|
2016-09-20 17:19:11 -05:00
|
|
|
'admin' == role
|
|
|
|
end
|
|
|
|
|
2016-09-22 13:30:30 -05:00
|
|
|
def acts_as_admin?
|
|
|
|
'admin' == role
|
|
|
|
end
|
|
|
|
|
2016-09-20 17:19:11 -05:00
|
|
|
def manager?
|
2016-09-22 13:30:30 -05:00
|
|
|
'manager' == role
|
|
|
|
end
|
|
|
|
|
|
|
|
def acts_as_manager?
|
2016-09-20 17:19:11 -05:00
|
|
|
%w(admin manager).include? role
|
|
|
|
end
|
|
|
|
|
|
|
|
def recruiter?
|
|
|
|
'recruiter' == role
|
|
|
|
end
|
|
|
|
|
2016-09-22 13:30:30 -05:00
|
|
|
def acts_as_recruiter?
|
|
|
|
%w(admin recruiter).include? role
|
|
|
|
end
|
|
|
|
|
2016-09-20 17:19:11 -05:00
|
|
|
def reviewer?
|
|
|
|
'reviewer' == role
|
2016-09-20 14:22:20 -05:00
|
|
|
end
|
|
|
|
|
2016-09-22 13:30:30 -05:00
|
|
|
def acts_as_reviewer?
|
2016-11-20 13:07:53 -06:00
|
|
|
%w(admin manager reviewer).include? role
|
2016-09-22 13:30:30 -05:00
|
|
|
end
|
|
|
|
|
2016-08-24 15:02:32 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def gen_reset_token
|
|
|
|
loop do
|
|
|
|
self[:reset_token] = SecureRandom.urlsafe_base64(10)
|
2016-09-08 11:52:34 -05:00
|
|
|
self[:reset_timestamp] = DateTime.current
|
2016-08-24 15:02:32 -05:00
|
|
|
break unless User.exists?(reset_token: self[:reset_token])
|
|
|
|
end
|
|
|
|
end
|
2016-07-26 17:00:00 -05:00
|
|
|
end
|