# frozen_string_literal: true module Admin class CandidateController < AdminController before_action :collect_quizzes, except: [:login, :auth] helper_method :sort_column def index @candidates = policy_scope Candidate.order("#{sort_column} #{sort_direction}") .page(params[:page]) end def new authorize Candidate @candidate = Candidate.new render :new end def create authorize Candidate @candidate = Candidate.create(candidate_params.merge(recruiter_id: current_user.id)) if @candidate.persisted? send_notifications @candidate redirect_to admin_candidates_path, 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 redirect_to admin_candidates_path, flash: { success: "#{@candidate.name} updated!" } 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) 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 end end