Linked Vendors to Participants via Service Contracts
This commit is contained in:
parent
778afeec08
commit
405125df6c
|
@ -7,13 +7,15 @@ class ParticipantsController < ApplicationController
|
|||
|
||||
|
||||
def show
|
||||
@participant = Participant.includes(:employments).find(params[:id])
|
||||
@participant = Participant.includes(:employments, :service_contracts).find(params[:id])
|
||||
@workers = @participant.workers # Fetch associated workers
|
||||
@employment = Employment.new # Initialize a new Employment object
|
||||
@participant = Participant.includes(employments: :worker).find(params[:id])
|
||||
@employments = @participant.employments.includes(:worker).order('workers.last_name')
|
||||
@service_contracts = @participant.service_contracts.includes(:vendor) # Fetch associated service contracts
|
||||
@employment = Employment.new # Initialize a new Employment object
|
||||
@service_contract = ServiceContract.new # Initialize a new Service Contract object
|
||||
end
|
||||
|
||||
|
||||
|
||||
def new
|
||||
@participant = Participant.new
|
||||
|
@ -74,6 +76,25 @@ class ParticipantsController < ApplicationController
|
|||
render 'show'
|
||||
end
|
||||
end
|
||||
|
||||
def link_vendor
|
||||
@participant = Participant.find(params[:id])
|
||||
@service_contract = @participant.service_contracts.new(service_contract_params)
|
||||
|
||||
if @service_contract.save
|
||||
redirect_to @participant, notice: 'Vendor was successfully linked.'
|
||||
else
|
||||
flash[:alert] = @service_contract.errors.full_messages.to_sentence
|
||||
render 'show'
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def service_contract_params
|
||||
# Define your service contract parameters here
|
||||
end
|
||||
|
||||
|
||||
|
||||
private
|
||||
|
@ -122,5 +143,10 @@ class ParticipantsController < ApplicationController
|
|||
def employment_params
|
||||
params.require(:employment).permit(:worker_id, :start_date, :end_date)
|
||||
end
|
||||
|
||||
def service_contract_params
|
||||
params.require(:service_contract).permit(:vendor_id, :start_date, :end_date)
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
class ServiceContractsController < ApplicationController
|
||||
before_action :set_service_contract, only: [:edit, :update, :destroy]
|
||||
|
||||
def new
|
||||
@service_contract = ServiceContract.new
|
||||
end
|
||||
|
||||
def create
|
||||
@service_contract = ServiceContract.new(service_contract_params)
|
||||
if @service_contract.save
|
||||
redirect_to participant_path(@service_contract.participant), notice: 'Service contract was successfully created.'
|
||||
else
|
||||
render :new
|
||||
end
|
||||
end
|
||||
|
||||
def edit
|
||||
end
|
||||
|
||||
def update
|
||||
if @service_contract.update(service_contract_params)
|
||||
redirect_to participant_path(@service_contract.participant), notice: 'Service contract was successfully updated.'
|
||||
else
|
||||
render :edit
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@service_contract.destroy
|
||||
redirect_to participant_path(@service_contract.participant), notice: 'Service contract was successfully destroyed.'
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_service_contract
|
||||
@service_contract = ServiceContract.find(params[:id])
|
||||
end
|
||||
|
||||
def service_contract_params
|
||||
params.require(:service_contract).permit(:start_date, :end_date, :participant_id, :vendor_id)
|
||||
end
|
||||
end
|
||||
|
|
@ -38,6 +38,18 @@ class VendorsController < ApplicationController
|
|||
@vendor.destroy
|
||||
redirect_to vendors_path
|
||||
end
|
||||
|
||||
def search
|
||||
if params[:term].present?
|
||||
@vendors = Vendor.where("name LIKE ?", "%#{params[:term]}%")
|
||||
else
|
||||
@vendors = Vendor.none
|
||||
end
|
||||
|
||||
# Respond with a JSON array of vendor names and IDs
|
||||
render json: @vendors.map { |vendor| { label: vendor.name, value: vendor.id } }
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
def vendor_params
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
module ServiceContractsHelper
|
||||
end
|
|
@ -3,6 +3,8 @@ class Participant < ApplicationRecord
|
|||
belongs_to :employer, optional: true
|
||||
has_many :employments
|
||||
has_many :workers, through: :employments
|
||||
has_many :service_contracts
|
||||
has_many :vendors, through: :service_contracts
|
||||
|
||||
# Validations
|
||||
validates :first_name, presence: true
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
class ServiceContract < ApplicationRecord
|
||||
belongs_to :participant
|
||||
belongs_to :vendor
|
||||
end
|
|
@ -1,5 +1,6 @@
|
|||
class Vendor < ApplicationRecord
|
||||
# Many-to-many relationships
|
||||
has_and_belongs_to_many :participants
|
||||
has_and_belongs_to_many :employers
|
||||
has_many :service_contracts
|
||||
has_many :participants, through: :service_contracts
|
||||
end
|
||||
|
|
|
@ -150,3 +150,86 @@ $(document).ready(function() {
|
|||
});
|
||||
</script>
|
||||
|
||||
|
||||
<h2 class="mt-4">Linked Vendors</h2>
|
||||
<% if @service_contracts.present? %>
|
||||
<table class="table table-striped">
|
||||
<thead class="table-light">
|
||||
<tr>
|
||||
<th>Vendor Name</th>
|
||||
<th>Start Date</th>
|
||||
<th>End Date</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<% @service_contracts.each do |contract| %>
|
||||
<tr>
|
||||
<td><%= contract.vendor&.name || 'No Vendor Assigned' %></td>
|
||||
<td><%= contract.start_date.strftime('%B %d, %Y') if contract.start_date.present? %></td>
|
||||
<td><%= contract.end_date.strftime('%B %d, %Y') if contract.end_date.present? %></td>
|
||||
<td>
|
||||
<%= link_to vendor_path(contract.vendor), class: 'btn btn-sm btn-secondary' do %>
|
||||
<i class="bi bi-eye"></i> <!-- Eyeball icon for 'Show' -->
|
||||
<% end %>
|
||||
<%= link_to edit_service_contract_path(contract), class: 'btn btn-sm btn-info' do %>
|
||||
<i class="bi bi-pencil-fill" style="color: white;"></i> <!-- Pencil icon for 'Edit' with white color -->
|
||||
<% end %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
<% else %>
|
||||
<p>No service contracts available.</p>
|
||||
<% end %>
|
||||
|
||||
<br>
|
||||
|
||||
<%= form_with(model: [@participant, @service_contract], url: link_vendor_participant_path(@participant), method: :post, class: 'row g-3') do |form| %>
|
||||
<div class="col-md-6">
|
||||
<%= form.label :vendor_name, "Add New Vendor", class: 'form-label' %>
|
||||
<%= text_field_tag :vendor_name, nil, id: 'vendor-autocomplete', class: 'form-control', placeholder: 'Start typing vendor name...' %>
|
||||
<%= hidden_field_tag 'service_contract[vendor_id]', nil, id: 'selected-vendor-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 Vendor", class: 'btn btn-dark' %>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<%# JavaScript for Vendor Autocomplete %>
|
||||
<script>
|
||||
$(document).ready(function() {
|
||||
$('#vendor-autocomplete').autocomplete({
|
||||
source: function(request, response) {
|
||||
$.ajax({
|
||||
url: '/vendors/search', // Endpoint for searching vendors
|
||||
dataType: "json",
|
||||
data: { term: request.term },
|
||||
success: function(data) {
|
||||
response(data);
|
||||
}
|
||||
});
|
||||
},
|
||||
minLength: 2,
|
||||
select: function(event, ui) {
|
||||
$('#vendor-autocomplete').val(ui.item.label);
|
||||
$('#selected-vendor-id').val(ui.item.value);
|
||||
return false;
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
<div class="container mt-5">
|
||||
<div class="row justify-content-center">
|
||||
<div class="col-md-6">
|
||||
<h1 class="mb-4 text-center">Edit Service Contract</h1>
|
||||
|
||||
<%= form_with(model: @service_contract, url: service_contract_path(@service_contract), method: :patch, local: true, html: { class: 'needs-validation', novalidate: true }) do |form| %>
|
||||
<% if @service_contract.errors.any? %>
|
||||
<div id="error_explanation" class="alert alert-danger" role="alert">
|
||||
<h4><%= pluralize(@service_contract.errors.count, "error") %> prohibited this service contract from being saved:</h4>
|
||||
<ul>
|
||||
<% @service_contract.errors.full_messages.each do |message| %>
|
||||
<li><%= message %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div class="mb-3">
|
||||
<%= form.label :start_date, class: 'form-label' %>
|
||||
<%= form.date_field :start_date, class: 'form-control' %>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<%= form.label :end_date, class: 'form-label' %>
|
||||
<%= form.date_field :end_date, class: 'form-control' %>
|
||||
</div>
|
||||
|
||||
<div class="actions">
|
||||
<%= form.submit "Update Service Contract", class: 'btn btn-dark' %>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div class="mt-3 d-flex justify-content-between">
|
||||
<%= link_to 'Back to Participant', participant_path(@service_contract.participant), class: 'btn btn-secondary' %>
|
||||
<%= link_to 'Delete Service Contract', service_contract_path(@service_contract), method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-danger' %>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
|
@ -12,6 +12,7 @@ Rails.application.routes.draw do
|
|||
resources :participants do
|
||||
member do
|
||||
post 'link_worker'
|
||||
post 'link_vendor'
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -19,8 +20,15 @@ Rails.application.routes.draw do
|
|||
get 'search', on: :collection
|
||||
end
|
||||
|
||||
resources :vendors
|
||||
resources :vendors do
|
||||
collection do
|
||||
get 'search'
|
||||
end
|
||||
end
|
||||
|
||||
resources :employments, only: [:edit, :update, :destroy]
|
||||
resources :service_contracts
|
||||
|
||||
resources :employers do
|
||||
get 'search', on: :collection
|
||||
end
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
class CreateServiceContracts < ActiveRecord::Migration[7.1]
|
||||
def change
|
||||
create_table :service_contracts do |t|
|
||||
t.references :participant, null: false, foreign_key: true
|
||||
t.references :vendor, 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_30_051346) do
|
||||
ActiveRecord::Schema[7.1].define(version: 2024_01_31_072940) do
|
||||
create_table "employers", force: :cascade do |t|
|
||||
t.string "first_name"
|
||||
t.string "phone"
|
||||
|
@ -86,6 +86,17 @@ ActiveRecord::Schema[7.1].define(version: 2024_01_30_051346) do
|
|||
t.datetime "updated_at", null: false
|
||||
end
|
||||
|
||||
create_table "service_contracts", force: :cascade do |t|
|
||||
t.integer "participant_id", null: false
|
||||
t.integer "vendor_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_service_contracts_on_participant_id"
|
||||
t.index ["vendor_id"], name: "index_service_contracts_on_vendor_id"
|
||||
end
|
||||
|
||||
create_table "users", force: :cascade do |t|
|
||||
t.string "email", default: "", null: false
|
||||
t.string "encrypted_password", default: "", null: false
|
||||
|
@ -137,4 +148,6 @@ ActiveRecord::Schema[7.1].define(version: 2024_01_30_051346) do
|
|||
add_foreign_key "employments", "workers"
|
||||
add_foreign_key "participants", "employers"
|
||||
add_foreign_key "participants", "workers"
|
||||
add_foreign_key "service_contracts", "participants"
|
||||
add_foreign_key "service_contracts", "vendors"
|
||||
end
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
require "test_helper"
|
||||
|
||||
class ServiceContractsControllerTest < ActionDispatch::IntegrationTest
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
|
@ -0,0 +1,13 @@
|
|||
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||
|
||||
one:
|
||||
participant: one
|
||||
vendor: one
|
||||
start_date: 2024-01-31
|
||||
end_date: 2024-01-31
|
||||
|
||||
two:
|
||||
participant: two
|
||||
vendor: two
|
||||
start_date: 2024-01-31
|
||||
end_date: 2024-01-31
|
|
@ -0,0 +1,7 @@
|
|||
require "test_helper"
|
||||
|
||||
class ServiceContractTest < ActiveSupport::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
Loading…
Reference in New Issue