PLEASE READ Tentative update fixing validation errors with the Participant and Employer models. Had to delete database and reclone last git push.

This commit is contained in:
Ben 2024-02-07 05:13:12 -06:00
parent 35d144327b
commit c1deea8463
7 changed files with 537 additions and 916 deletions

View File

@ -9,12 +9,12 @@ class EmployersController < ApplicationController
# GET /employers/:id
def show
Rails.logger.debug "Employer ID: #{params[:id]}"
@employer = Employer.includes(:employer_records, :direct_participants).find(params[:id])
@employer = Employer.includes(employer_records: :participant).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
@ -45,16 +45,17 @@ class EmployersController < ApplicationController
# 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.'
@employer = Employer.find_by(id: params[:id])
if @employer
@employer.destroy
redirect_to employers_url, notice: 'Employer 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

View File

@ -22,49 +22,58 @@ class ParticipantsController < ApplicationController
def create
@participant = Participant.new(participant_params)
Participant.transaction do
if ssn_already_taken?(@participant.ssn)
flash.now[:alert] = 'SSN is already taken by another participant or employer.'
render :new, status: :unprocessable_entity and return
end
ActiveRecord::Base.transaction do
if params[:is_employer] == 'yes'
create_employer_and_employer_record_for(@participant)
# This assumes you have a method `create_new_employer_for_participant` that handles creating a new employer and setting `@participant.employer_id`.
unless create_new_employer_for_participant(@participant)
render :new, status: :unprocessable_entity and return
end
elsif params[:employer_id].present?
# Directly assign the existing employer_id to the participant
@participant.employer_id = params[:employer_id]
EmployerRecord.create!(participant: @participant, employer_id: @participant.employer_id, start_date: Date.today)
end
if @participant.save
# Create an EmployerRecord only if necessary.
create_employer_record(@participant) if @participant.employer_id.present?
redirect_to @participant, notice: 'Participant was successfully created.'
else
flash.now[:alert] = 'There was an error creating the participant.'
render :new, status: :unprocessable_entity
end
end
rescue ActiveRecord::RecordInvalid => e
flash.now[:alert] = "Error: #{e.message}"
flash.now[:alert] = "Failed to create participant: #{e.message}"
render :new, status: :unprocessable_entity
end
def edit
end
def update
@participant = Participant.find(params[:id])
Participant.transaction do
if @participant.update(participant_params)
if @participant.employer_id.present?
update_employer_details(@participant)
end
redirect_to @participant, notice: 'Participant was successfully updated.'
if @participant.errors.any?
# If there are errors (e.g., from updating employer details), re-render the edit form.
render :edit
else
# If everything went well, redirect to the participant's show page.
redirect_to @participant, notice: 'Participant and associated employer details were successfully updated.'
end
else
render :edit
end
end
rescue => e
flash.now[:alert] = "Error: #{e.message}"
render :edit
end
def search
@ -117,10 +126,23 @@ class ParticipantsController < ApplicationController
private
def handle_employer_creation_for_participant(participant)
if params[:is_employer] == 'yes'
employer = Employer.create!(employer_params_from_participant(participant))
participant.employer_id = employer.id
elsif params[:employer_id].present?
participant.employer_id = params[:employer_id]
end
end
def set_participant
@participant = Participant.find(params[:id])
end
def ssn_already_taken?(ssn)
Participant.exists?(ssn: ssn) || Employer.exists?(ssn: ssn)
end
def participant_params
params.require(:participant).permit(
:first_name,
@ -166,38 +188,22 @@ class ParticipantsController < ApplicationController
params.require(:service_contract).permit(:vendor_id, :start_date, :end_date)
end
def create_employer_and_employer_record_for(participant)
# Check if an employer with the same email or similar details already exists
existing_employer = Employer.find_by(email: participant.email) ||
Employer.where(first_name: participant.first_name,
last_name: participant.last_name,
ssn: participant.ssn).first
# If an existing employer is found, assign it to the participant
if existing_employer.present?
participant.employer_id = existing_employer.id
else
# Create a new employer with participant's details
employer = Employer.new(employer_params_from_participant(participant))
if employer.save
def create_new_employer_for_participant(participant)
employer = Employer.create!(employer_params_from_participant(participant))
participant.employer_id = employer.id
else
participant.errors.add(:base, "Employer creation failed: #{employer.errors.full_messages.join(', ')}")
raise ActiveRecord::Rollback
end
employer.persisted?
end
# Create an EmployerRecord if it doesn't exist for this combination
unless EmployerRecord.exists?(participant: participant, employer_id: participant.employer_id)
def create_employer_record(participant)
EmployerRecord.create!(participant: participant, employer_id: participant.employer_id, start_date: Date.today)
end
end
def update_employer_details(participant)
employer = Employer.find(participant.employer_id)
employer.update(
update_attrs = {
# Include all fields you want to update, excluding SSN
first_name: participant.first_name,
last_name: participant.last_name,
address_line_1: participant.address_line_1,
@ -208,10 +214,15 @@ class ParticipantsController < ApplicationController
phone: participant.phone,
email: participant.email,
dob: participant.dob,
ssn: participant.ssn,
gender: participant.gender,
}
# Add other fields that need to be updated
)
unless employer.update(update_attrs)
# If the update fails, add a custom error to the participant object.
participant.errors.add(:base, "Employer details could not be updated: #{employer.errors.full_messages.join(', ')}")
# You might not need to throw :abort if you're handling this logic in the controller.
end
end
end

View File

@ -15,17 +15,20 @@ class Employer < ApplicationRecord
end
validate :unique_as_participant
validates :ssn, uniqueness: true, allow_blank: true, presence: true
validates :tin, uniqueness: true, allow_blank: true, presence: true
class Employer < ApplicationRecord
# Associations and other methods as before...
validate :unique_as_participant, if: -> { ssn_changed? || new_record? }
validates :ssn, uniqueness: { allow_blank: true }, if: -> { ssn.present? }
validates :tin, uniqueness: { allow_blank: true }, if: -> { tin.present? }
private
def unique_as_participant
# Check if there's a Participant with the same details
existing_participant = Participant.find_by(ssn: ssn)
if existing_participant && (existing_participant.id != id)
errors.add(:base, 'A participant with the same SSN already exists')
if Participant.exists?(ssn: ssn) && (id.blank? || Participant.where.not(id: id).exists?(ssn: ssn))
errors.add(:ssn, 'A participant with the same SSN already exists')
end
end
end
end

View File

@ -1,6 +1,6 @@
class Participant < ApplicationRecord
# Associations
belongs_to :employer, optional: true
belongs_to :employer, optional: false
has_many :employments
has_many :workers, through: :employments
has_many :service_contracts
@ -11,7 +11,8 @@ class Participant < ApplicationRecord
# Validations
validates :first_name, :last_name, :ssn, presence: true
validate :ssn_must_be_unique_across_models
validates :ssn, uniqueness: { message: "SSN is already taken by another participant" }, unless: :ssn_already_taken_by_employer?
validates :employer_id, presence: true
def full_name
"#{first_name} #{last_name}"
@ -19,9 +20,8 @@ class Participant < ApplicationRecord
private
def ssn_must_be_unique_across_models
if Participant.where.not(id: id).exists?(ssn: ssn) || Employer.exists?(ssn: ssn)
errors.add(:ssn, 'SSN is already taken by another participant or employer')
end
def ssn_already_taken_by_employer?
# Check if the SSN already exists in the Employer model, excluding the current participant's employer
Employer.where.not(id: employer_id).exists?(ssn: ssn)
end
end

3
db/schema.rb generated
View File

@ -79,9 +79,6 @@ ActiveRecord::Schema[7.1].define(version: 2024_02_06_215805) do
t.date "start_date"
t.date "end_date"
t.index ["employer_id"], name: "index_participants_on_employer_id"
t.index ["last_name"], name: "index_participants_on_last_name"
t.index ["mci"], name: "index_participants_on_mci"
t.index ["ssn"], name: "index_participants_on_ssn"
t.index ["worker_id"], name: "index_participants_on_worker_id"
end

View File

@ -12,8 +12,8 @@
"@babel/core": "^7.23.9",
"@babel/plugin-proposal-optional-chaining": "^7.21.0",
"@babel/preset-env": "^7.23.9",
"webpack": "^5.90.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^4.15.1"
"webpack": "^4.46.0",
"webpack-cli": "^3.3.12",
"webpack-dev-server": "^3"
}
}

1287
yarn.lock

File diff suppressed because it is too large Load Diff