2016-09-08 10:25:33 -05:00
|
|
|
# frozen_string_literal: true
|
2016-07-31 09:56:02 -05:00
|
|
|
class RecruiterController < ApplicationController
|
|
|
|
before_action :authorize_recruiter, except: [:login, :auth]
|
2016-09-16 11:36:48 -05:00
|
|
|
before_action :collect_quizzes, except: [:login, :auth]
|
2016-07-31 09:56:02 -05:00
|
|
|
|
|
|
|
def index
|
|
|
|
@candidates = current_recruiter.candidates
|
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
@candidate = Candidate.new
|
2016-09-16 11:36:48 -05:00
|
|
|
render :new
|
2016-07-31 09:56:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2016-07-31 14:47:15 -05:00
|
|
|
@candidate = Candidate.create(candidate_params.merge(recruiter_id: current_recruiter.id))
|
|
|
|
|
|
|
|
if @candidate.persisted?
|
2016-09-14 14:38:26 -05:00
|
|
|
CandidateMailer.welcome(@candidate).deliver_later
|
|
|
|
RecruiterMailer.candidate_created(@candidate).deliver_later
|
2016-08-24 12:15:12 -05:00
|
|
|
redirect_to recruiter_path, flash: { success: "Sucessfully created candidate #{@candidate.name}" }
|
2016-07-31 14:47:15 -05:00
|
|
|
else
|
2016-08-04 08:51:54 -05:00
|
|
|
flash[:error] = "Failed to save candidate."
|
2016-09-16 11:36:48 -05:00
|
|
|
render :new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
@candidate = Candidate.find_by(id: params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
@candidate = Candidate.find_by(id: params[:id])
|
|
|
|
@candidate.update(candidate_params)
|
|
|
|
|
|
|
|
if @candidate.save
|
|
|
|
redirect_to recruiter_path, flash: { success: "#{@candidate.name} updated!" }
|
|
|
|
else
|
|
|
|
flash[:error] = "Failed to save candidate."
|
|
|
|
render :edit
|
2016-07-31 14:47:15 -05:00
|
|
|
end
|
2016-07-31 09:56:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def login
|
|
|
|
redirect_to recruiter_path unless current_recruiter.nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
def auth
|
2016-07-31 16:34:35 -05:00
|
|
|
recruiter = User.find_by(email: auth_params[:email], role: %w(admin recruiter))
|
2016-07-31 09:56:02 -05:00
|
|
|
|
|
|
|
if recruiter && recruiter.authenticate(auth_params[:password])
|
|
|
|
session[:user] = recruiter.to_i
|
|
|
|
redirect_to recruiter_path
|
|
|
|
else
|
2016-08-04 17:24:53 -05:00
|
|
|
redirect_to recruiter_login_path,
|
|
|
|
flash: { error: "Sorry, incorrect email or password. Please try again." }
|
2016-07-31 09:56:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def logout
|
|
|
|
reset_session
|
|
|
|
redirect_to recruiter_login_path
|
|
|
|
end
|
|
|
|
|
2016-09-15 10:01:31 -05:00
|
|
|
def resend_welcome
|
|
|
|
candidate = Candidate.find_by(id: params[:id])
|
|
|
|
CandidateMailer.welcome(candidate).deliver_later
|
|
|
|
render json: { message: "Email queued!" }.to_json
|
|
|
|
end
|
|
|
|
|
2016-07-31 09:56:02 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def candidate_params
|
|
|
|
params.require(:candidate).permit(:name, :email, :experience, :quiz_id)
|
|
|
|
end
|
2016-09-16 11:36:48 -05:00
|
|
|
|
|
|
|
def collect_quizzes
|
|
|
|
@quizzes ||= Quiz.order(:name)
|
|
|
|
end
|
2016-07-31 09:56:02 -05:00
|
|
|
end
|