Participant show page per person now can have linked workers within the system
This commit is contained in:
parent
97b4e89e7a
commit
9c6f980ccb
|
@ -8,7 +8,10 @@ class ParticipantsController < ApplicationController
|
||||||
|
|
||||||
|
|
||||||
def show
|
def show
|
||||||
|
@participant = Participant.includes(:employments).find(params[:id])
|
||||||
|
@workers = @participant.workers # Fetch associated workers
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def new
|
def new
|
||||||
@participant = Participant.new
|
@participant = Participant.new
|
||||||
|
@ -16,34 +19,22 @@ class ParticipantsController < ApplicationController
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@participant = Participant.new(participant_params)
|
@participant = Participant.new(participant_params)
|
||||||
|
Participant.transaction do
|
||||||
begin
|
if params[:is_employer] == 'yes'
|
||||||
Participant.transaction do
|
employer = Employer.create!(employer_params_from_participant(@participant))
|
||||||
if params[:is_employer] == 'yes'
|
@participant.employer = employer
|
||||||
employer = Employer.create!(employer_params_from_participant(@participant))
|
end
|
||||||
@participant.employer = employer
|
|
||||||
end
|
if @participant.save
|
||||||
|
redirect_to @participant, notice: 'Participant was successfully created.'
|
||||||
if @participant.save
|
else
|
||||||
redirect_to @participant, notice: 'Participant was successfully created.'
|
raise ActiveRecord::Rollback, "Participant could not be saved"
|
||||||
else
|
|
||||||
raise ActiveRecord::Rollback, "Participant could not be saved"
|
|
||||||
end
|
|
||||||
end
|
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
|
end
|
||||||
|
rescue ActiveRecord::RecordInvalid => e
|
||||||
|
Rails.logger.error("Participant creation failed: #{e.message}")
|
||||||
|
flash.now[:alert] = "Error: #{e.message}"
|
||||||
rescue => e
|
|
||||||
puts "Error: #{e.message}" # Log the error message
|
|
||||||
flash.now[:alert] = "Error: #{e.message}" # Optionally, display the error message to the user
|
|
||||||
render :new
|
render :new
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def edit
|
def edit
|
||||||
|
@ -62,6 +53,17 @@ class ParticipantsController < ApplicationController
|
||||||
redirect_to participants_url, notice: 'Participant was successfully destroyed.'
|
redirect_to participants_url, notice: 'Participant was successfully destroyed.'
|
||||||
end
|
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
|
private
|
||||||
|
|
||||||
def set_participant
|
def set_participant
|
||||||
|
@ -83,5 +85,8 @@ class ParticipantsController < ApplicationController
|
||||||
gender: participant.gender # Mapping Gender
|
gender: participant.gender # Mapping Gender
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def employment_params
|
||||||
|
params.require(:employment).permit(:worker_id, :start_date, :end_date)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
class Employment < ApplicationRecord
|
||||||
|
belongs_to :worker
|
||||||
|
belongs_to :participant
|
||||||
|
end
|
|
@ -1,12 +1,13 @@
|
||||||
class Participant < ApplicationRecord
|
class Participant < ApplicationRecord
|
||||||
# This makes the association to Employer optional
|
# Associations
|
||||||
belongs_to :employer, optional: true
|
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 :first_name, presence: true
|
||||||
validates :last_name, presence: true
|
validates :last_name, presence: true
|
||||||
|
|
||||||
# Other associations
|
# Other associations
|
||||||
has_and_belongs_to_many :programs
|
has_and_belongs_to_many :programs
|
||||||
has_many :workers
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
class Worker < ApplicationRecord
|
class Worker < ApplicationRecord
|
||||||
# One-to-many relationship with Participants
|
# 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
|
# Many-to-many relationship with Employers through Participants
|
||||||
has_many :employers, through: :participants
|
has_many :employers, through: :participants
|
||||||
|
|
|
@ -22,7 +22,7 @@
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
<div class="actions text-center">
|
<div class="actions text-center">
|
||||||
<%= f.submit "Log in", class: 'btn btn-primary' %>
|
<%= f.submit "Log in", class: 'btn btn-dark' %>
|
||||||
</div>
|
</div>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
|
|
|
@ -59,3 +59,46 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</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'
|
root to: 'devise/sessions#new'
|
||||||
end
|
end
|
||||||
|
|
||||||
resources :participants
|
resources :participants do
|
||||||
|
member do
|
||||||
|
post 'link_worker'
|
||||||
|
end
|
||||||
|
end
|
||||||
resources :workers
|
resources :workers
|
||||||
resources :vendors
|
resources :vendors
|
||||||
resources :employers do
|
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.
|
# 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|
|
create_table "employers", force: :cascade do |t|
|
||||||
t.string "name"
|
t.string "name"
|
||||||
t.string "address"
|
t.string "address"
|
||||||
|
@ -29,6 +29,17 @@ ActiveRecord::Schema[7.1].define(version: 2024_01_23_023259) do
|
||||||
t.integer "employer_id", null: false
|
t.integer "employer_id", null: false
|
||||||
end
|
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|
|
create_table "participants", force: :cascade do |t|
|
||||||
t.string "first_name"
|
t.string "first_name"
|
||||||
t.string "address"
|
t.string "address"
|
||||||
|
@ -103,6 +114,8 @@ ActiveRecord::Schema[7.1].define(version: 2024_01_23_023259) do
|
||||||
t.datetime "updated_at", null: false
|
t.datetime "updated_at", null: false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
add_foreign_key "employments", "participants"
|
||||||
|
add_foreign_key "employments", "workers"
|
||||||
add_foreign_key "participants", "employers"
|
add_foreign_key "participants", "employers"
|
||||||
add_foreign_key "participants", "workers"
|
add_foreign_key "participants", "workers"
|
||||||
end
|
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