2024-01-16 23:10:18 -06:00
|
|
|
class ParticipantsController < ApplicationController
|
2024-01-19 23:38:15 -06:00
|
|
|
before_action :set_participant, only: [:show, :edit, :update, :destroy]
|
2024-01-16 23:10:18 -06:00
|
|
|
|
2024-01-19 23:38:15 -06:00
|
|
|
def index
|
2024-01-22 21:22:29 -06:00
|
|
|
@participants = Participant.order(:last_name).page(params[:page]).per(5) # Adjust the number per page as needed
|
2024-01-19 23:38:15 -06:00
|
|
|
end
|
2024-01-22 19:48:38 -06:00
|
|
|
|
2024-01-19 23:38:15 -06:00
|
|
|
|
|
|
|
def show
|
2024-01-31 02:19:24 -06:00
|
|
|
@participant = Participant.includes(:employments, :service_contracts).find(params[:id])
|
2024-01-22 22:57:37 -06:00
|
|
|
@workers = @participant.workers # Fetch associated workers
|
2024-01-29 21:03:16 -06:00
|
|
|
@employments = @participant.employments.includes(:worker).order('workers.last_name')
|
2024-01-31 02:19:24 -06:00
|
|
|
@service_contracts = @participant.service_contracts.includes(:vendor) # Fetch associated service contracts
|
|
|
|
@employment = Employment.new # Initialize a new Employment object
|
|
|
|
@service_contract = ServiceContract.new # Initialize a new Service Contract object
|
2024-01-19 23:38:15 -06:00
|
|
|
end
|
2024-01-22 22:57:37 -06:00
|
|
|
|
2024-01-19 23:38:15 -06:00
|
|
|
def new
|
|
|
|
@participant = Participant.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@participant = Participant.new(participant_params)
|
2024-01-30 16:34:18 -06:00
|
|
|
|
2024-01-22 22:57:37 -06:00
|
|
|
Participant.transaction do
|
|
|
|
if params[:is_employer] == 'yes'
|
2024-02-02 23:12:36 -06:00
|
|
|
create_employer_and_employer_record_for(@participant)
|
|
|
|
elsif params[:employer_id].present?
|
|
|
|
@participant.employer_id = params[:employer_id]
|
2024-01-22 22:57:37 -06:00
|
|
|
end
|
2024-01-30 16:34:18 -06:00
|
|
|
|
2024-01-22 22:57:37 -06:00
|
|
|
if @participant.save
|
|
|
|
redirect_to @participant, notice: 'Participant was successfully created.'
|
|
|
|
else
|
2024-02-02 23:12:36 -06:00
|
|
|
render :new
|
2024-01-16 23:10:18 -06:00
|
|
|
end
|
|
|
|
end
|
2024-01-22 22:57:37 -06:00
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
|
|
Rails.logger.error("Participant creation failed: #{e.message}")
|
|
|
|
flash.now[:alert] = "Error: #{e.message}"
|
2024-01-19 23:38:15 -06:00
|
|
|
render :new
|
|
|
|
end
|
2024-01-16 23:10:18 -06:00
|
|
|
|
2024-01-30 16:34:18 -06:00
|
|
|
|
2024-01-19 23:38:15 -06:00
|
|
|
def edit
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
if @participant.update(participant_params)
|
|
|
|
redirect_to @participant, notice: 'Participant was successfully updated.'
|
|
|
|
else
|
|
|
|
render :edit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-02-02 16:44:50 -06:00
|
|
|
def search
|
|
|
|
if params[:term].present?
|
|
|
|
@participants = Participant.where("first_name LIKE ? OR last_name LIKE ?", "%#{params[:term]}%", "%#{params[:term]}%")
|
|
|
|
else
|
|
|
|
@participants = Participant.none
|
|
|
|
end
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.json { render json: @participants.map { |participant| { label: participant.full_name, value: participant.id } } }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-01-19 23:38:15 -06:00
|
|
|
def destroy
|
|
|
|
@participant.destroy
|
|
|
|
redirect_to participants_url, notice: 'Participant was successfully destroyed.'
|
|
|
|
end
|
|
|
|
|
2024-01-22 22:57:37 -06:00
|
|
|
def link_worker
|
|
|
|
@participant = Participant.find(params[:id])
|
2024-01-29 21:03:16 -06:00
|
|
|
@employment = @participant.employments.new(employment_params)
|
|
|
|
|
|
|
|
if @employment.save
|
2024-01-22 22:57:37 -06:00
|
|
|
redirect_to @participant, notice: 'Worker was successfully linked.'
|
|
|
|
else
|
2024-01-29 21:03:16 -06:00
|
|
|
Rails.logger.debug @employment.errors.full_messages
|
|
|
|
flash[:alert] = @employment.errors.full_messages.to_sentence
|
|
|
|
render 'show'
|
2024-01-22 22:57:37 -06:00
|
|
|
end
|
|
|
|
end
|
2024-01-31 02:19:24 -06:00
|
|
|
|
|
|
|
def link_vendor
|
|
|
|
@participant = Participant.find(params[:id])
|
|
|
|
@service_contract = @participant.service_contracts.new(service_contract_params)
|
|
|
|
|
|
|
|
if @service_contract.save
|
|
|
|
redirect_to @participant, notice: 'Vendor was successfully linked.'
|
|
|
|
else
|
|
|
|
flash[:alert] = @service_contract.errors.full_messages.to_sentence
|
|
|
|
render 'show'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-01-19 23:38:15 -06:00
|
|
|
private
|
|
|
|
|
|
|
|
def set_participant
|
|
|
|
@participant = Participant.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def participant_params
|
2024-01-30 16:45:30 -06:00
|
|
|
params.require(:participant).permit(
|
|
|
|
:first_name,
|
|
|
|
:last_name,
|
|
|
|
:address_line_1,
|
|
|
|
:address_line_2,
|
|
|
|
:city,
|
|
|
|
:state,
|
|
|
|
:zip,
|
|
|
|
:phone,
|
|
|
|
:email,
|
|
|
|
:mci,
|
|
|
|
:dob,
|
|
|
|
:ssn,
|
|
|
|
:gender,
|
|
|
|
:employer_id
|
|
|
|
)
|
2024-01-19 23:38:15 -06:00
|
|
|
end
|
2024-01-20 00:28:17 -06:00
|
|
|
|
2024-01-30 16:45:30 -06:00
|
|
|
|
2024-01-19 23:38:15 -06:00
|
|
|
def employer_params_from_participant(participant)
|
|
|
|
{
|
2024-01-30 16:34:18 -06:00
|
|
|
first_name: participant.first_name,
|
|
|
|
last_name: participant.last_name,
|
|
|
|
address_line_1: participant.address_line_1,
|
|
|
|
address_line_2: participant.address_line_2,
|
|
|
|
city: participant.city,
|
|
|
|
state: participant.state,
|
|
|
|
zip: participant.zip,
|
2024-01-19 23:38:15 -06:00
|
|
|
phone: participant.phone,
|
|
|
|
email: participant.email,
|
2024-01-30 16:34:18 -06:00
|
|
|
dob: participant.dob,
|
|
|
|
ssn: participant.ssn,
|
|
|
|
gender: participant.gender
|
2024-01-19 23:38:15 -06:00
|
|
|
}
|
2024-01-16 23:10:18 -06:00
|
|
|
end
|
2024-01-30 16:34:18 -06:00
|
|
|
|
2024-01-22 22:57:37 -06:00
|
|
|
def employment_params
|
|
|
|
params.require(:employment).permit(:worker_id, :start_date, :end_date)
|
|
|
|
end
|
2024-01-31 02:19:24 -06:00
|
|
|
|
|
|
|
def service_contract_params
|
|
|
|
params.require(:service_contract).permit(:vendor_id, :start_date, :end_date)
|
|
|
|
end
|
2024-02-02 23:12:36 -06:00
|
|
|
|
|
|
|
def create_employer_and_employer_record_for(participant)
|
|
|
|
employer = Employer.new(employer_params_from_participant(participant))
|
|
|
|
if employer.save
|
|
|
|
participant.employer_id = employer.id
|
|
|
|
# Create an EmployerRecord with start_date as current date
|
|
|
|
EmployerRecord.create!(participant: participant, employer: employer, start_date: Date.today)
|
|
|
|
else
|
|
|
|
participant.errors.add(:base, "Employer creation failed: #{employer.errors.full_messages.join(', ')}")
|
|
|
|
raise ActiveRecord::Rollback
|
|
|
|
end
|
|
|
|
end
|
2024-01-19 23:38:15 -06:00
|
|
|
end
|