Updated Worker Show page to include Linked Participants
This commit is contained in:
parent
9066a968eb
commit
1265d885ec
|
@ -15,8 +15,6 @@ class ParticipantsController < ApplicationController
|
||||||
@service_contract = ServiceContract.new # Initialize a new Service Contract object
|
@service_contract = ServiceContract.new # Initialize a new Service Contract object
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def new
|
def new
|
||||||
@participant = Participant.new
|
@participant = Participant.new
|
||||||
end
|
end
|
||||||
|
@ -59,6 +57,18 @@ class ParticipantsController < ApplicationController
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def search
|
||||||
|
if params[:term].present?
|
||||||
|
@participants = Participant.where("first_name LIKE ? OR last_name LIKE ?", "%#{params[:term]}%", "%#{params[:term]}%")
|
||||||
|
else
|
||||||
|
@participants = Participant.none
|
||||||
|
end
|
||||||
|
|
||||||
|
respond_to do |format|
|
||||||
|
format.json { render json: @participants.map { |participant| { label: participant.full_name, value: participant.id } } }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
@participant.destroy
|
@participant.destroy
|
||||||
redirect_to participants_url, notice: 'Participant was successfully destroyed.'
|
redirect_to participants_url, notice: 'Participant was successfully destroyed.'
|
||||||
|
|
|
@ -4,9 +4,12 @@ class WorkersController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
@worker = Worker.find(params[:id])
|
@worker = Worker.includes(:participants).find(params[:id])
|
||||||
|
@employments = @worker.employments
|
||||||
|
@employment = Employment.new # Initialize a new Employment instance
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def new
|
def new
|
||||||
@worker = Worker.new
|
@worker = Worker.new
|
||||||
end
|
end
|
||||||
|
@ -51,6 +54,24 @@ class WorkersController < ApplicationController
|
||||||
render json: @workers.map { |worker| { label: "#{worker.first_name} #{worker.last_name}", value: worker.id } }
|
render json: @workers.map { |worker| { label: "#{worker.first_name} #{worker.last_name}", value: worker.id } }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def link_participant
|
||||||
|
@worker = Worker.find(params[:id])
|
||||||
|
|
||||||
|
# Assuming you're submitting the participant ID in your form
|
||||||
|
participant_id = params[:employment][:participant_id]
|
||||||
|
start_date = params[:employment][:start_date]
|
||||||
|
end_date = params[:employment][:end_date]
|
||||||
|
|
||||||
|
# Create the employment relationship
|
||||||
|
@employment = @worker.employments.build(participant_id: participant_id, start_date: start_date, end_date: end_date)
|
||||||
|
|
||||||
|
if @employment.save
|
||||||
|
redirect_to @worker, notice: 'Participant was successfully linked to the worker.'
|
||||||
|
else
|
||||||
|
render 'show', alert: 'Unable to link participant.'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def worker_params
|
def worker_params
|
||||||
params.require(:worker).permit(
|
params.require(:worker).permit(
|
||||||
|
|
|
@ -9,6 +9,9 @@ class Participant < ApplicationRecord
|
||||||
# Validations
|
# Validations
|
||||||
validates :first_name, presence: true
|
validates :first_name, presence: true
|
||||||
validates :last_name, presence: true
|
validates :last_name, presence: true
|
||||||
|
def full_name
|
||||||
|
"#{first_name} #{last_name}"
|
||||||
|
end
|
||||||
|
|
||||||
# Other associations
|
# Other associations
|
||||||
has_and_belongs_to_many :programs
|
has_and_belongs_to_many :programs
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
class Worker < ApplicationRecord
|
class Worker < ApplicationRecord
|
||||||
# One-to-many relationship with Participants
|
# One-to-many relationship with Employments
|
||||||
has_many :participants, through: :employments
|
|
||||||
has_many :employments
|
has_many :employments
|
||||||
|
|
||||||
|
# Many-to-many relationship with Participants through Employments
|
||||||
|
has_many :participants, through: :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
|
||||||
|
|
||||||
|
|
|
@ -43,3 +43,93 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
<div class="row mt-4">
|
||||||
|
<div class="col-md-6">
|
||||||
|
<h2 class="mt-4">Linked Participants</h2>
|
||||||
|
<% if @employments.present? %>
|
||||||
|
<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>
|
||||||
|
<% @employments.each do |employment| %>
|
||||||
|
<tr>
|
||||||
|
<% participant = employment.participant %>
|
||||||
|
<td><%= participant ? "#{participant.first_name} #{participant.last_name}" : 'No Participant Assigned' %></td>
|
||||||
|
<td><%= employment.start_date.strftime('%B %d, %Y') if employment.start_date.present? %></td>
|
||||||
|
<td><%= employment.end_date.strftime('%B %d, %Y') if employment.end_date.present? %></td>
|
||||||
|
<td>
|
||||||
|
<%= link_to 'View', participant_path(participant), class: 'btn btn-sm btn-secondary' %>
|
||||||
|
<%= link_to 'Edit', edit_employment_path(employment), class: 'btn btn-sm btn-info' %>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<% end %>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<% else %>
|
||||||
|
<p>No employments available.</p>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
<%= form_with(model: [@worker, @employment], url: link_participant_worker_path(@worker), method: :post, class: 'row g-3') do |form| %>
|
||||||
|
<div class="col-md-6">
|
||||||
|
<%= form.label :participant_name, "Add New Participant", class: 'form-label' %>
|
||||||
|
<%= text_field_tag :participant_name, nil, id: 'participant-autocomplete', class: 'form-control', placeholder: 'Start typing participant name...' %>
|
||||||
|
<%= hidden_field_tag 'employment[participant_id]', nil, id: 'selected-participant-id' %>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="col-md-3">
|
||||||
|
<%= form.label :start_date, class: 'form-label' %>
|
||||||
|
<%= form.date_field :start_date, class: 'form-control' %>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="col-md-3">
|
||||||
|
<%= form.label :end_date, class: 'form-label' %>
|
||||||
|
<%= form.date_field :end_date, class: 'form-control' %>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="col-12">
|
||||||
|
<%= form.submit "Link Participant", class: 'btn btn-dark' %>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
|
|
||||||
|
<%# JavaScript for Participant Autocomplete %>
|
||||||
|
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
|
||||||
|
<script src="https://code.jquery.com/ui/1.13.0/jquery-ui.min.js"></script>
|
||||||
|
<link rel="stylesheet" href="//code.jquery.com/ui/1.13.0/themes/base/jquery-ui.css">
|
||||||
|
|
||||||
|
<script>
|
||||||
|
$(document).ready(function() {
|
||||||
|
$('#participant-autocomplete').autocomplete({
|
||||||
|
source: function(request, response) {
|
||||||
|
$.ajax({
|
||||||
|
url: '/participants/search', // Updated endpoint for searching participants
|
||||||
|
dataType: "json",
|
||||||
|
data: { term: request.term },
|
||||||
|
success: function(data) {
|
||||||
|
response(data);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
minLength: 2, // Minimum characters to trigger the search
|
||||||
|
select: function(event, ui) {
|
||||||
|
// Function to handle selection
|
||||||
|
$('#participant-autocomplete').val(ui.item.label); // ui.item.label should contain the name of the participant
|
||||||
|
$('#selected-participant-id').val(ui.item.value); // Set the participant ID in the hidden field
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -10,16 +10,25 @@ Rails.application.routes.draw do
|
||||||
end
|
end
|
||||||
|
|
||||||
resources :participants do
|
resources :participants do
|
||||||
|
collection do
|
||||||
|
get 'search' # Define search on the collection, not on a member
|
||||||
|
end
|
||||||
|
|
||||||
member do
|
member do
|
||||||
post 'link_worker'
|
post 'link_worker' # Other member routes
|
||||||
post 'link_vendor'
|
post 'link_vendor' #Add this line to link a vendor
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
resources :workers do
|
resources :workers do
|
||||||
|
member do
|
||||||
|
post 'link_participant'
|
||||||
|
end
|
||||||
get 'search', on: :collection
|
get 'search', on: :collection
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
resources :vendors do
|
resources :vendors do
|
||||||
collection do
|
collection do
|
||||||
get 'search'
|
get 'search'
|
||||||
|
|
Loading…
Reference in New Issue