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

77 lines
2.0 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]
helper_method :sort_column
2016-09-22 13:30:30 -05:00
def index
@candidates = policy_scope Candidate.order("#{sort_column} #{sort_direction}")
.page(params[:page])
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
def sort_column
Candidate.column_names.include?(params[:sort]) ? params[:sort] : 'name'
end
2016-09-22 13:30:30 -05:00
end
end