Lots of updates mainly in how pages are displayed. Also edited participant database to allow for simulataneous employer creation, made the TIN field not mandatory etc

This commit is contained in:
Ben 2024-01-19 23:38:15 -06:00
parent 9662776c73
commit 2de94bd706
10 changed files with 188 additions and 59 deletions

View File

@ -44,6 +44,19 @@ class EmployersController < ApplicationController
redirect_to employers_url, notice: 'Employer was successfully destroyed.' redirect_to employers_url, notice: 'Employer was successfully destroyed.'
end end
# GET /employers/search
# This action responds to the auto-complete AJAX requests
def search
if params[:term].present?
@employers = Employer.where("name LIKE ?", "%#{params[:term]}%")
else
@employers = Employer.none
end
# Respond with a JSON array of employer names and IDs
render json: @employers.map { |e| { label: e.name, value: e.id } }
end
private private
def set_employer def set_employer

View File

@ -14,11 +14,34 @@ class ParticipantsController < ApplicationController
def create def create
@participant = Participant.new(participant_params) @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 if @participant.save
redirect_to @participant, notice: 'Participant was successfully created.' redirect_to @participant, notice: 'Participant was successfully created.'
else else
raise ActiveRecord::Rollback, "Participant could not be saved"
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 render :new
end 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
render :new
end end
def edit def edit
@ -38,6 +61,7 @@ class ParticipantsController < ApplicationController
end end
private private
def set_participant def set_participant
@participant = Participant.find(params[:id]) @participant = Participant.find(params[:id])
end end
@ -45,5 +69,16 @@ class ParticipantsController < ApplicationController
def participant_params def participant_params
params.require(:participant).permit(:name, :address, :phone, :email, :mci, :dob, :ssn, :gender, :employer_id) params.require(:participant).permit(:name, :address, :phone, :email, :mci, :dob, :ssn, :gender, :employer_id)
end end
end
def employer_params_from_participant(participant)
{
name: participant.name,
address: participant.address,
phone: participant.phone,
email: participant.email,
dob: participant.dob, # Mapping Date of Birth
ssn: participant.ssn, # Mapping SSN
gender: participant.gender # Mapping Gender
}
end
end

View File

@ -1,5 +1,10 @@
class Participant < ApplicationRecord class Participant < ApplicationRecord
belongs_to :employer # This makes the association to Employer optional
belongs_to :employer, optional: true
belongs_to :worker, optional: true
# Other associations
has_and_belongs_to_many :programs has_and_belongs_to_many :programs
has_many :workers has_many :workers
end end

View File

@ -10,10 +10,10 @@
<th>Address</th> <th>Address</th>
<th>Phone</th> <th>Phone</th>
<th>Email</th> <th>Email</th>
<th>TIN</th>
<th>DOB</th> <th>DOB</th>
<th>SSN</th>
<th>Gender</th>
<th>Actions</th> <th>Actions</th>
</tr> </tr>
</thead> </thead>
@ -24,14 +24,13 @@
<td><%= employer.address %></td> <td><%= employer.address %></td>
<td><%= employer.phone %></td> <td><%= employer.phone %></td>
<td><%= employer.email %></td> <td><%= employer.email %></td>
<td><%= employer.tin %></td>
<td><%= employer.dob %></td> <td><%= employer.dob %></td>
<td><%= employer.ssn %></td>
<td><%= employer.gender %></td>
<td> <td>
<%= link_to 'Show', employer, class: 'btn btn-sm btn-info' %> <%= link_to 'Show', employer, class: 'btn btn-sm btn-info' %>
<%= link_to 'Edit', edit_employer_path(employer), class: 'btn btn-sm btn-warning' %> <%= link_to 'Edit', edit_employer_path(employer), class: 'btn btn-sm btn-warning' %>
<%= link_to 'Destroy', @employer, method: :delete, data: { confirm: 'Are you sure?', turbo: 'false' }, class: 'btn btn-sm btn-danger' %>
</td> </td>
</tr> </tr>
<% end %> <% end %>

View File

@ -30,6 +30,17 @@
<%= form.email_field :email, id: 'email-field', class: 'form-control', required: true, placeholder: 'Make sure to include @ sign' %> <%= form.email_field :email, id: 'email-field', class: 'form-control', required: true, placeholder: 'Make sure to include @ sign' %>
</div> </div>
<div class="mb-3">
<%= form.label :is_employer, 'Is this participant also an employer?', class: 'form-label' %>
<div>
<%= radio_button_tag 'is_employer', 'yes', false, class: 'form-check-input' %>
<%= label_tag 'is_employer_yes', 'Yes', class: 'form-check-label' %>
<%= radio_button_tag 'is_employer', 'no', true, class: 'form-check-input' %>
<%= label_tag 'is_employer_no', 'No', class: 'form-check-label' %>
</div>
</div>
<div class="mb-3"> <div class="mb-3">
<%= form.label :mci, 'MCI', class: 'form-label' %> <%= form.label :mci, 'MCI', class: 'form-label' %>
<%= form.text_field :mci, class: 'form-control', placeholder: 'Required for Participant' %> <%= form.text_field :mci, class: 'form-control', placeholder: 'Required for Participant' %>
@ -138,3 +149,30 @@
<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() {
$('#employer-autocomplete').autocomplete({
source: function(request, response) {
$.ajax({
url: '/employers/search', // Rails endpoint for searching employers
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
$('#employer-autocomplete').val(ui.item.label); // ui.item.label should contain the name of the employer
// You might want to set a hidden field with employer ID if needed
return false;
}
});
});
</script>

View File

@ -23,9 +23,9 @@
<td><%= participant.address %></td> <td><%= participant.address %></td>
<td><%= participant.phone %></td> <td><%= participant.phone %></td>
<td><%= participant.email %></td> <td><%= participant.email %></td>
<td><%= participant.program %></td> <td><%= participant.programs.map(&:name).join(", ") %></td>
<td><%= participant.mci %></td> <td><%= participant.mci %></td>
<td><%= participant.dob.strftime('%Y-%m-%d') if participant.dob %></td> <td><%= participant.dob.strftime('%B, %d, %Y') if participant.dob %></td>
<td><%= participant.ssn %></td> <td><%= participant.ssn %></td>
<td><%= participant.gender %></td> <td><%= participant.gender %></td>
<td><%= link_to 'Show', participant, class: 'btn btn-sm btn-info' %></td> <td><%= link_to 'Show', participant, class: 'btn btn-sm btn-info' %></td>

View File

@ -15,6 +15,37 @@
<%= @participant.address %> <%= @participant.address %>
</p> </p>
<p class="card-text">
<strong>Phone:</strong>
<%= @participant.phone %>
</p>
<p class="card-text">
<strong>Email:</strong>
<%= @participant.email %>
</p>
<p class="card-text">
<strong>MCI:</strong>
<%= @participant.mci %>
</p>
<p class="card-text">
<strong>DOB:</strong>
<%= @participant.dob.strftime('%B, %d, %Y') if @participant.dob.present? %>
</p>
<p class="card-text">
<strong>SSN:</strong>
<%= @participant.ssn %>
</p>
<p class="card-text">
<strong>Gender:</strong>
<%= @participant.gender %>
</p>
<!-- Repeat this pattern for other attributes like phone, email, etc. --> <!-- Repeat this pattern for other attributes like phone, email, etc. -->
</div> </div>

View File

@ -10,9 +10,12 @@ Rails.application.routes.draw do
end end
resources :participants resources :participants
resources :employers
resources :workers resources :workers
resources :vendors resources :vendors
resources :employers do
get 'search', on: :collection
end

View File

@ -0,0 +1,5 @@
class ChangeWorkerIdToBeOptionalInParticipants < ActiveRecord::Migration[6.0]
def change
change_column_null :participants, :worker_id, true
end
end

4
db/schema.rb generated
View File

@ -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_17_025159) do ActiveRecord::Schema[7.1].define(version: 2024_01_20_051441) 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"
@ -41,7 +41,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_01_17_025159) do
t.integer "employer_id", null: false t.integer "employer_id", null: false
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.integer "worker_id", null: false t.integer "worker_id"
t.index ["employer_id"], name: "index_participants_on_employer_id" t.index ["employer_id"], name: "index_participants_on_employer_id"
t.index ["worker_id"], name: "index_participants_on_worker_id" t.index ["worker_id"], name: "index_participants_on_worker_id"
end end