Participant show page per person now can have linked workers within the system
This commit is contained in:
parent
97b4e89e7a
commit
9c6f980ccb
|
@ -8,42 +8,33 @@ class ParticipantsController < ApplicationController
|
|||
|
||||
|
||||
def show
|
||||
@participant = Participant.includes(:employments).find(params[:id])
|
||||
@workers = @participant.workers # Fetch associated workers
|
||||
end
|
||||
|
||||
|
||||
def new
|
||||
@participant = Participant.new
|
||||
end
|
||||
|
||||
def create
|
||||
@participant = Participant.new(participant_params)
|
||||
|
||||
begin
|
||||
Participant.transaction do
|
||||
if params[:is_employer] == 'yes'
|
||||
employer = Employer.create!(employer_params_from_participant(@participant))
|
||||
@participant.employer = employer
|
||||
end
|
||||
|
||||
if @participant.save
|
||||
redirect_to @participant, notice: 'Participant was successfully created.'
|
||||
else
|
||||
raise ActiveRecord::Rollback, "Participant could not be saved"
|
||||
end
|
||||
Participant.transaction do
|
||||
if params[:is_employer] == 'yes'
|
||||
employer = Employer.create!(employer_params_from_participant(@participant))
|
||||
@participant.employer = employer
|
||||
end
|
||||
|
||||
if @participant.save
|
||||
redirect_to @participant, notice: 'Participant was successfully created.'
|
||||
else
|
||||
raise ActiveRecord::Rollback, "Participant could not be saved"
|
||||
end
|
||||
rescue ActiveRecord::RecordInvalid => e
|
||||
# Log the error and set a flash message for the user
|
||||
Rails.logger.error("Participant creation failed: #{e.message}")
|
||||
flash.now[:alert] = "Error: #{e.message}"
|
||||
render :new
|
||||
end
|
||||
|
||||
|
||||
|
||||
rescue => e
|
||||
puts "Error: #{e.message}" # Log the error message
|
||||
flash.now[:alert] = "Error: #{e.message}" # Optionally, display the error message to the user
|
||||
rescue ActiveRecord::RecordInvalid => e
|
||||
Rails.logger.error("Participant creation failed: #{e.message}")
|
||||
flash.now[:alert] = "Error: #{e.message}"
|
||||
render :new
|
||||
|
||||
end
|
||||
|
||||
def edit
|
||||
|
@ -62,6 +53,17 @@ class ParticipantsController < ApplicationController
|
|||
redirect_to participants_url, notice: 'Participant was successfully destroyed.'
|
||||
end
|
||||
|
||||
def link_worker
|
||||
@participant = Participant.find(params[:id])
|
||||
employment = @participant.employments.new(employment_params)
|
||||
if employment.save
|
||||
redirect_to @participant, notice: 'Worker was successfully linked.'
|
||||
else
|
||||
# Assuming you have a show or edit page to render
|
||||
render 'show', alert: 'Failed to link worker.'
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_participant
|
||||
|
@ -84,4 +86,7 @@ class ParticipantsController < ApplicationController
|
|||
}
|
||||
end
|
||||
|
||||
def employment_params
|
||||
params.require(:employment).permit(:worker_id, :start_date, :end_date)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
class Employment < ApplicationRecord
|
||||
belongs_to :worker
|
||||
belongs_to :participant
|
||||
end
|
|
@ -1,12 +1,13 @@
|
|||
class Participant < ApplicationRecord
|
||||
# This makes the association to Employer optional
|
||||
# Associations
|
||||
belongs_to :employer, optional: true
|
||||
belongs_to :worker, optional: true
|
||||
has_many :employments
|
||||
has_many :workers, through: :employments
|
||||
|
||||
# Validations
|
||||
validates :first_name, presence: true
|
||||
validates :last_name, presence: true
|
||||
|
||||
# Other associations
|
||||
has_and_belongs_to_many :programs
|
||||
has_many :workers
|
||||
end
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
class Worker < ApplicationRecord
|
||||
# One-to-many relationship with Participants
|
||||
has_many :participants
|
||||
has_many :participants, through: :employments
|
||||
has_many :employments
|
||||
|
||||
# Many-to-many relationship with Employers through Participants
|
||||
has_many :employers, through: :participants
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
<% end %>
|
||||
|
||||
<div class="actions text-center">
|
||||
<%= f.submit "Log in", class: 'btn btn-primary' %>
|
||||
<%= f.submit "Log in", class: 'btn btn-dark' %>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
|
|
|
@ -59,3 +59,46 @@
|
|||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<h2>Linked Workers</h2>
|
||||
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Worker Name</th>
|
||||
<th>Start Date</th>
|
||||
<th>End Date</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<% @participant.employments.each do |employment| %>
|
||||
<tr>
|
||||
<td><%= employment.worker.name %></td>
|
||||
<td><%= employment.start_date %></td>
|
||||
<td><%= employment.end_date %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
|
||||
|
||||
<%= form_with(url: link_worker_participant_path(@participant), method: :post) do |form| %>
|
||||
<div class="field">
|
||||
<%= form.label :worker_id, "Select Worker" %>
|
||||
<%= form.collection_select :worker_id, Worker.all, :id, :name, include_blank: true %>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<%= form.label :start_date %>
|
||||
<%= form.date_field :start_date %>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<%= form.label :end_date %>
|
||||
<%= form.date_field :end_date %>
|
||||
</div>
|
||||
|
||||
<%= form.submit "Link Worker" %>
|
||||
<% end %>
|
||||
|
|
|
@ -9,7 +9,11 @@ Rails.application.routes.draw do
|
|||
root to: 'devise/sessions#new'
|
||||
end
|
||||
|
||||
resources :participants
|
||||
resources :participants do
|
||||
member do
|
||||
post 'link_worker'
|
||||
end
|
||||
end
|
||||
resources :workers
|
||||
resources :vendors
|
||||
resources :employers do
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
class CreateEmployments < ActiveRecord::Migration[7.1]
|
||||
def change
|
||||
create_table :employments do |t|
|
||||
t.references :worker, null: false, foreign_key: true
|
||||
t.references :participant, null: false, foreign_key: true
|
||||
t.date :start_date
|
||||
t.date :end_date
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
|
@ -10,7 +10,7 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema[7.1].define(version: 2024_01_23_023259) do
|
||||
ActiveRecord::Schema[7.1].define(version: 2024_01_23_042316) do
|
||||
create_table "employers", force: :cascade do |t|
|
||||
t.string "name"
|
||||
t.string "address"
|
||||
|
@ -29,6 +29,17 @@ ActiveRecord::Schema[7.1].define(version: 2024_01_23_023259) do
|
|||
t.integer "employer_id", null: false
|
||||
end
|
||||
|
||||
create_table "employments", force: :cascade do |t|
|
||||
t.integer "worker_id", null: false
|
||||
t.integer "participant_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 ["participant_id"], name: "index_employments_on_participant_id"
|
||||
t.index ["worker_id"], name: "index_employments_on_worker_id"
|
||||
end
|
||||
|
||||
create_table "participants", force: :cascade do |t|
|
||||
t.string "first_name"
|
||||
t.string "address"
|
||||
|
@ -103,6 +114,8 @@ ActiveRecord::Schema[7.1].define(version: 2024_01_23_023259) do
|
|||
t.datetime "updated_at", null: false
|
||||
end
|
||||
|
||||
add_foreign_key "employments", "participants"
|
||||
add_foreign_key "employments", "workers"
|
||||
add_foreign_key "participants", "employers"
|
||||
add_foreign_key "participants", "workers"
|
||||
end
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||
|
||||
one:
|
||||
worker: one
|
||||
participant: one
|
||||
start_date: 2024-01-22
|
||||
end_date: 2024-01-22
|
||||
|
||||
two:
|
||||
worker: two
|
||||
participant: two
|
||||
start_date: 2024-01-22
|
||||
end_date: 2024-01-22
|
|
@ -0,0 +1,7 @@
|
|||
require "test_helper"
|
||||
|
||||
class EmploymentTest < ActiveSupport::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
Loading…
Reference in New Issue