Entering a Participant as an Employer now correctly associates the two in Employer records. Still need to work out selecting a pre existing Employer.

This commit is contained in:
Ben 2024-02-02 23:12:36 -06:00
parent 205fb7fbd6
commit fedc2e67b7
16 changed files with 238 additions and 19 deletions

View File

@ -0,0 +1,54 @@
class EmployerRecordsController < ApplicationController
before_action :set_employer_record, only: [:edit, :update]
def edit
# Logic to edit an EmployerRecord
# Display a form to edit the EmployerRecord
end
def update
# Logic to update an EmployerRecord
# Called when the edit form is submitted
if @employer_record.update(employer_record_params)
redirect_to employer_path(@employer_record.employer), notice: 'Employer record was successfully updated.'
else
render :edit
end
end
def link_participant_to_employer
# Assuming you have the employer and participant IDs
employer_id = params[:employer_id]
participant_id = params[:participant_id]
employer_record = EmployerRecord.new(employer_id: employer_id, participant_id: participant_id, start_date: Date.today)
if employer_record.save
# Redirect or render success message
else
# Handle error
end
end
def destroy
if @employer_record
@employer_record.destroy
redirect_to employers_url, notice: 'Employer record was successfully deleted.'
else
redirect_to some_error_handling_path, alert: 'Employer record not found.'
end
end
private
def set_employer_record
@employer_record = EmployerRecord.find(params[:id])
end
def employer_record_params
params.require(:employer_record).permit(:participant_id, :employer_id, :start_date, :end_date)
end
end

View File

@ -8,7 +8,10 @@ class EmployersController < ApplicationController
# GET /employers/:id
def show
@employer = Employer.includes(:employer_records, :participants).find(params[:id])
@employer_records = @employer.employer_records.includes(:participant)
end
# GET /employers/new
def new

View File

@ -24,19 +24,15 @@ class ParticipantsController < ApplicationController
Participant.transaction do
if params[:is_employer] == 'yes'
employer = Employer.new(employer_params_from_participant(@participant))
if employer.save
@participant.employer = employer
else
Rails.logger.debug "Employer creation failed: #{employer.errors.full_messages}"
raise ActiveRecord::Rollback, "Employer could not be saved"
end
create_employer_and_employer_record_for(@participant)
elsif params[:employer_id].present?
@participant.employer_id = params[:employer_id]
end
if @participant.save
redirect_to @participant, notice: 'Participant was successfully created.'
else
raise ActiveRecord::Rollback, "Participant could not be saved"
render :new
end
end
rescue ActiveRecord::RecordInvalid => e
@ -99,14 +95,6 @@ class ParticipantsController < ApplicationController
end
end
private
def service_contract_params
# Define your service contract parameters here
end
private
def set_participant
@ -157,6 +145,16 @@ class ParticipantsController < ApplicationController
def service_contract_params
params.require(:service_contract).permit(:vendor_id, :start_date, :end_date)
end
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
end

View File

@ -0,0 +1,2 @@
module EmployerRecordsHelper
end

View File

@ -1,6 +1,9 @@
class Employer < ApplicationRecord
has_many :participants
has_many :workers, through: :participants
has_many :employer_records
has_many :participants, through: :employer_records
# Association with Vendor if needed
def full_name

View File

@ -0,0 +1,4 @@
class EmployerRecord < ApplicationRecord
belongs_to :employer
belongs_to :participant
end

View File

@ -5,6 +5,9 @@ class Participant < ApplicationRecord
has_many :workers, through: :employments
has_many :service_contracts
has_many :vendors, through: :service_contracts
has_many :employer_records
has_many :employers, through: :employer_records
# Validations
validates :first_name, presence: true

View File

@ -0,0 +1,46 @@
<div class="container mt-5">
<h1>Edit Employer Record</h1>
<%= form_with(model: @employer_record, class: 'form') do |form| %>
<% if @employer_record.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@employer_record.errors.count, "error") %> prohibited this employer record from being saved:</h2>
<ul>
<% @employer_record.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :participant_id %>
<%= form.number_field :participant_id, class: 'form-control' %>
</div>
<div class="field">
<%= form.label :employer_id %>
<%= form.number_field :employer_id, class: 'form-control' %>
</div>
<div class="field">
<%= form.label :start_date %>
<%= form.date_field :start_date, class: 'form-control' %>
</div>
<div class="field">
<%= form.label :end_date %>
<%= form.date_field :end_date, class: 'form-control' %>
</div>
<div class="actions">
<%= form.submit 'Save Changes', class: 'btn btn-primary' %>
<%= link_to 'Delete Record', employer_record_path(@employer_record),
method: :delete,
data: { confirm: 'Are you sure?' },
class: 'btn btn-danger' %>
</div>
<% end %>
</div>

View File

@ -53,3 +53,44 @@
</div>
</div>
</div>
<div class="container mt-5">
<!-- Employer Details ... -->
<!-- Linked Participants Section -->
<div class="row mt-4">
<div class="col-md-12">
<h2 class="mt-4">Linked Participants</h2>
<% if @employer.employer_records.any? %>
<table class="table table-striped">
<thead class="table-light">
<tr>
<th>Participant Name</th>
<th>Start Date</th>
<th>End Date</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<% @employer.employer_records.each do |employer_record| %>
<tr>
<% participant = employer_record.participant %>
<td><%= "#{participant.first_name} #{participant.last_name}" %></td>
<td><%= employer_record.start_date.strftime('%B %d, %Y') if employer_record.start_date %></td>
<td><%= employer_record.end_date.strftime('%B %d, %Y') if employer_record.end_date %></td>
<td>
<%= link_to 'View', participant_path(participant), class: 'btn btn-sm btn-secondary' %>
<%= link_to 'Edit', edit_employer_record_path(employer_record), class: 'btn btn-sm btn-info' %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<p>No linked participants.</p>
<% end %>
</div>
</div>

View File

@ -36,6 +36,11 @@ Rails.application.routes.draw do
end
resources :employments, only: [:edit, :update, :destroy]
resources :employer_records, only: [:edit, :update, :destroy] do
collection do
post 'link_participant'
end
end
resources :service_contracts
resources :employers do

View File

@ -0,0 +1,6 @@
class AddDatesToParticipants < ActiveRecord::Migration[7.1]
def change
add_column :participants, :start_date, :date
add_column :participants, :end_date, :date
end
end

View File

@ -0,0 +1,12 @@
class CreateEmployerRecords < ActiveRecord::Migration[7.1]
def change
create_table :employer_records do |t|
t.references :participant, null: false, foreign_key: true
t.references :employer, null: false, foreign_key: true
t.date :start_date
t.date :end_date
t.timestamps
end
end
end

17
db/schema.rb generated
View File

@ -10,7 +10,18 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.1].define(version: 2024_01_31_072940) do
ActiveRecord::Schema[7.1].define(version: 2024_02_03_002211) do
create_table "employer_records", force: :cascade do |t|
t.integer "participant_id", null: false
t.integer "employer_id", null: false
t.date "start_date"
t.date "end_date"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["employer_id"], name: "index_employer_records_on_employer_id"
t.index ["participant_id"], name: "index_employer_records_on_participant_id"
end
create_table "employers", force: :cascade do |t|
t.string "first_name"
t.string "phone"
@ -63,6 +74,8 @@ ActiveRecord::Schema[7.1].define(version: 2024_01_31_072940) do
t.string "city"
t.string "state"
t.string "zip"
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"
@ -144,6 +157,8 @@ ActiveRecord::Schema[7.1].define(version: 2024_01_31_072940) do
t.string "zip"
end
add_foreign_key "employer_records", "employers"
add_foreign_key "employer_records", "participants"
add_foreign_key "employments", "participants"
add_foreign_key "employments", "workers"
add_foreign_key "participants", "employers"

View File

@ -0,0 +1,7 @@
require "test_helper"
class EmployerRecordsControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end

13
test/fixtures/employer_records.yml vendored Normal file
View File

@ -0,0 +1,13 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
participant: one
employer: one
start_date: 2024-02-02
end_date: 2024-02-02
two:
participant: two
employer: two
start_date: 2024-02-02
end_date: 2024-02-02

View File

@ -0,0 +1,7 @@
require "test_helper"
class EmployerRecordTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end