skill-assessment-app/app/controllers/admin/candidate_controller.rb

70 lines
1.8 KiB
Ruby
Raw Normal View History

2016-09-22 13:30:30 -05:00
# frozen_string_literal: true
module Admin
class CandidateController < AdminController
before_action :collect_quizzes, except: [:login, :auth]
def index
2016-09-22 16:29:19 -05:00
@candidates = policy_scope Candidate.order(:name)
2016-09-22 13:30:30 -05:00
end
def new
authorize Candidate
@candidate = Candidate.new
render :new
end
def create
authorize Candidate
2016-09-22 16:29:19 -05:00
@candidate = Candidate.create(candidate_params.merge(recruiter_id: current_user.id))
2016-09-22 13:30:30 -05:00
if @candidate.persisted?
send_notifications @candidate
2016-09-22 14:21:34 -05:00
redirect_to admin_candidates_path,
2016-09-22 13:30:30 -05:00
flash: { success: "Sucessfully created candidate #{@candidate.name}" }
else
flash[:error] = "Failed to save candidate."
render :new
end
end
def edit
authorize Candidate
@candidate = Candidate.find_by(id: params[:id])
end
def update
authorize Candidate
@candidate = Candidate.find_by(id: params[:id])
@candidate.update(candidate_params)
if @candidate.save
2016-09-22 14:21:34 -05:00
redirect_to admin_candidates_path, flash: { success: "#{@candidate.name} updated!" }
2016-09-22 13:30:30 -05:00
else
flash[:error] = "Failed to save candidate."
render :edit
end
end
def resend_welcome
authorize Candidate
candidate = Candidate.find_by(id: params[:id])
CandidateMailer.welcome(candidate).deliver_later
end
private
def candidate_params
params.require(:candidate).permit(:name, :email, :experience, :quiz_id, :project)
2016-09-22 13:30:30 -05:00
end
def collect_quizzes
@quizzes ||= Quiz.order(:name)
end
def send_notifications candidate
CandidateMailer.welcome(candidate).deliver_later
RecruiterMailer.candidate_created(candidate).deliver_later
end
end
end