2024-01-17 01:44:36 -06:00
|
|
|
class VendorsController < ApplicationController
|
|
|
|
def index
|
2024-01-30 00:04:20 -06:00
|
|
|
@vendors = Vendor.order(:name).page(params[:page]).per(5) # Adjust the number per page as needed
|
2024-01-17 01:44:36 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
@vendor = Vendor.find(params[:id])
|
2024-02-05 17:06:56 -06:00
|
|
|
@sorted_participants = @vendor.service_contracts.includes(:participant).map(&:participant).sort_by(&:last_name)
|
2024-01-17 01:44:36 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
@vendor = Vendor.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@vendor = Vendor.new(vendor_params)
|
|
|
|
if @vendor.save
|
2024-04-02 18:19:58 -05:00
|
|
|
assign_role_specific_forms_to(@vendor)
|
|
|
|
redirect_to @vendor, notice: 'Vendor was successfully created.'
|
2024-01-17 01:44:36 -06:00
|
|
|
else
|
|
|
|
render :new
|
|
|
|
end
|
|
|
|
end
|
2024-04-02 18:19:58 -05:00
|
|
|
|
2024-01-17 01:44:36 -06:00
|
|
|
|
|
|
|
def edit
|
|
|
|
@vendor = Vendor.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
@vendor = Vendor.find(params[:id])
|
|
|
|
if @vendor.update(vendor_params)
|
|
|
|
redirect_to @vendor
|
|
|
|
else
|
|
|
|
render :edit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@vendor = Vendor.find(params[:id])
|
|
|
|
@vendor.destroy
|
|
|
|
redirect_to vendors_path
|
|
|
|
end
|
2024-01-31 02:19:24 -06:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2024-01-17 01:44:36 -06:00
|
|
|
|
|
|
|
private
|
2024-01-31 00:49:23 -06:00
|
|
|
def vendor_params
|
|
|
|
params.require(:vendor).permit(
|
|
|
|
:name,
|
|
|
|
:address_line_1,
|
|
|
|
:address_line_2,
|
|
|
|
:city,
|
|
|
|
:state,
|
|
|
|
:zip,
|
|
|
|
:phone,
|
|
|
|
:email,
|
|
|
|
:dba,
|
|
|
|
:tin,
|
|
|
|
:contact
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2024-04-02 18:19:58 -05:00
|
|
|
def assign_role_specific_forms_to(vendor)
|
|
|
|
vendor_role = FormRole.find_by(name: 'Vendor')
|
|
|
|
|
|
|
|
# Fetch forms associated with the "Vendor" role
|
|
|
|
forms_for_vendor = vendor_role.forms
|
|
|
|
|
|
|
|
forms_for_vendor.each do |form|
|
|
|
|
# Create an onboarding item for each form for the newly created vendor
|
|
|
|
vendor.onboarding_items.create(form: form)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-01-17 01:44:36 -06:00
|
|
|
end
|
|
|
|
|