obdev/app/controllers/employers_controller.rb

124 lines
3.2 KiB
Ruby
Raw Normal View History

class EmployersController < ApplicationController
before_action :set_employer, only: [:show, :edit, :update, :destroy]
# GET /employers
def index
@employers = Employer.order(:last_name).page(params[:page]).per(5) # Adjust the number per page as needed
end
# GET /employers/:id
def show
Rails.logger.debug "Employer ID: #{params[:id]}"
@employer = Employer.includes(:employer_records, :direct_participants).find(params[:id])
Rails.logger.debug "Employer: #{@employer.inspect}"
@employer_records = @employer.employer_records.includes(:participant)
end
# GET /employers/new
def new
@employer = Employer.new
end
# POST /employers
def create
@employer = Employer.new(employer_params)
if @employer.save
redirect_to @employer, notice: 'Employer was successfully created.'
else
render :new
end
end
# GET /employers/:id/edit
def edit
end
# PATCH/PUT /employers/:id
def update
if @employer.update(employer_params)
redirect_to @employer, notice: 'Employer was successfully updated.'
else
render :edit
end
end
# DELETE /employers/:id
def destroy
employer_record = EmployerRecord.find_by(id: params[:id])
if employer_record
employer_record.destroy
redirect_to employers_url, notice: 'Employer record was successfully deleted.'
else
redirect_to employers_url, alert: 'Employer record not found.'
end
end
# GET /employers/search
# This action responds to the auto-complete AJAX requests
def search
if params[:term].present?
@employers = Employer.where(
"first_name LIKE :term OR last_name LIKE :term",
term: "%#{params[:term]}%"
)
else
@employers = Employer.none
end
render json: @employers.map { |e| { label: e.full_name, value: e.id } }
end
def link_employer
@worker = Worker.find(params[:id])
employer_record = @worker.employer_records.new(employer_record_params)
# Handle the logic for creating the employer_record
# ...
end
def link_worker
@employer = Employer.find(params[:id])
participant_id = params[:employer_record][:participant_id] # Assuming this is passed from the form
start_date = params[:employer_record][:start_date]
end_date = params[:employer_record][:end_date]
employer_record = EmployerRecord.new(
employer: @employer,
participant_id: participant_id,
start_date: start_date,
end_date: end_date
)
if employer_record.save
redirect_to @employer, notice: 'Worker was successfully linked.'
else
flash.now[:alert] = employer_record.errors.full_messages.to_sentence
render :show
end
rescue ActiveRecord::RecordNotFound
# Handle case where Employer is not found
redirect_to some_path, alert: 'Employer not found.'
end
private
def set_employer
@employer = Employer.find(params[:id])
end
def employer_params
params.require(:employer).permit(
:first_name, :last_name,
:address_line_1, :address_line_2,
:city, :state, :zip,
:phone, :email, :tin, :dob, :ssn, :gender
)
end
def employer_record_params
params.require(:employer_record).permit(:employer_id, :start_date, :end_date)
end
end