Updated all models so on creating entries the system will autopopulate all corresponding forms depending on the type entered. Including participants who are also employers properly populating both.

This commit is contained in:
Ben 2024-04-02 18:19:58 -05:00
parent 65df59d3e6
commit 8343c3ae8a
5 changed files with 93 additions and 20 deletions

View File

@ -14,27 +14,23 @@ class EmployersController < ApplicationController
end
# GET /employers/new
def new
@employer = Employer.new
end
# POST /employers
def create
@employer = Employer.new(employer_params)
if @employer.save
assign_role_specific_forms_to(@employer)
redirect_to @employer, notice: 'Employer was successfully created.'
else
render :new
end
end
# GET /employers/:id/edit
def edit
end
# PATCH/PUT /employers/:id
def update
if @employer.update(employer_params)
redirect_to @employer, notice: 'Employer was successfully updated.'
@ -43,7 +39,6 @@ class EmployersController < ApplicationController
end
end
# DELETE /employers/:id
def destroy
@employer = Employer.find_by(id: params[:id])
if @employer
@ -54,10 +49,6 @@ class EmployersController < ApplicationController
end
end
# GET /employers/search
# This action responds to the auto-complete AJAX requests
def search
if params[:term].present?
@employers = Employer.where(
@ -127,4 +118,16 @@ class EmployersController < ApplicationController
params.require(:employer_record).permit(:employer_id, :start_date, :end_date)
end
def assign_role_specific_forms_to(employer)
employer_role = FormRole.find_by(name: 'Employer')
# Fetch forms associated with the "Employer" role
forms_for_employer = employer_role.forms
forms_for_employer.each do |form|
# Create an onboarding item for each form for the newly created employer
employer.onboarding_items.create(form: form)
end
end
end

View File

@ -15,12 +15,14 @@ class VendorsController < ApplicationController
def create
@vendor = Vendor.new(vendor_params)
if @vendor.save
redirect_to @vendor
assign_role_specific_forms_to(@vendor)
redirect_to @vendor, notice: 'Vendor was successfully created.'
else
render :new
end
end
def edit
@vendor = Vendor.find(params[:id])
end
@ -69,5 +71,17 @@ class VendorsController < ApplicationController
)
end
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
end

View File

@ -18,12 +18,14 @@ class WorkersController < ApplicationController
def create
@worker = Worker.new(worker_params)
if @worker.save
redirect_to @worker
assign_role_specific_forms_to(@worker)
redirect_to @worker, notice: 'Worker was successfully created.'
else
render :new
end
end
def edit
@worker = Worker.find(params[:id])
end
@ -88,6 +90,17 @@ class WorkersController < ApplicationController
)
end
def assign_role_specific_forms_to(worker)
worker_role = FormRole.find_by(name: 'Worker')
# Fetch forms associated with the "Worker" role
forms_for_worker = worker_role.forms
forms_for_worker.each do |form|
# Create an onboarding item for each form for the newly created worker
worker.onboarding_items.create(form: form)
end
end
end

View File

@ -31,15 +31,37 @@ class Participant < ApplicationRecord
end
def create_onboarding_items_for_forms
# Ensure there's a 'Role' model with an association set up between Forms and Roles.
participant_role = Role.find_by(name: 'Participant')
return unless participant_role
create_participant_onboarding_items
create_employer_onboarding_items_if_applicable
end
# Fetch all forms associated with the 'Participant' role.
forms = Form.joins(:roles).where(roles: { id: participant_role.id })
def create_participant_onboarding_items
participant_role = FormRole.find_by(name: 'Participant')
forms = Form.joins(:form_roles).where(form_roles: { id: participant_role.id })
forms.each do |form|
self.onboarding_items.find_or_create_by(form: form)
onboarding_items.find_or_create_by(form: form)
end
end
def create_employer_onboarding_items_if_applicable
# Assuming there is a method or indicator to check if the participant is also an employer
return unless self.is_also_employer?
employer_role = FormRole.find_by(name: 'Employer')
forms = Form.joins(:form_roles).where(form_roles: { id: employer_role.id })
forms.each do |form|
# Here you need to decide how you want to associate the forms with the employer record
# If the employer record is separate from the participant, you may need something like:
self.employer.onboarding_items.find_or_create_by(form: form) if self.employer.present?
# If the participant itself acts as an employer, then you might directly create onboarding items as done for participant forms
end
end
# A method to determine if the participant should also have employer forms
def is_also_employer?
# Implement logic to determine if the participant is also an employer
# This might involve checking if the participant has an associated employer record
# or any other logic specific to your application
self.employer.present? # Example logic, adjust as needed
end
end

View File

@ -0,0 +1,21 @@
namespace :workers do
desc "Populate missing onboarding forms for all workers"
task populate_missing_forms: :environment do
# Fetch all forms that should be available to workers
forms_to_assign = Form.worker # Adjust this based on your criteria for which forms to assign
# Iterate over each worker
Worker.find_each do |worker|
forms_to_assign.each do |form|
# Check if the worker already has this form assigned
unless worker.onboarding_items.exists?(form: form)
# If not, create an OnboardingItem for the worker and this form
OnboardingItem.create!(owner: worker, form: form)
end
end
end
puts "Missing onboarding forms have been successfully populated for all workers."
end
end