diff --git a/app/controllers/employers_controller.rb b/app/controllers/employers_controller.rb index 5e68121..7c65d7f 100644 --- a/app/controllers/employers_controller.rb +++ b/app/controllers/employers_controller.rb @@ -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 diff --git a/app/controllers/vendors_controller.rb b/app/controllers/vendors_controller.rb index 8fcc026..cbecc88 100644 --- a/app/controllers/vendors_controller.rb +++ b/app/controllers/vendors_controller.rb @@ -15,11 +15,13 @@ 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]) @@ -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 \ No newline at end of file diff --git a/app/controllers/workers_controller.rb b/app/controllers/workers_controller.rb index 8d12c0c..e3fde05 100644 --- a/app/controllers/workers_controller.rb +++ b/app/controllers/workers_controller.rb @@ -18,11 +18,13 @@ 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]) @@ -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 \ No newline at end of file diff --git a/app/models/participant.rb b/app/models/participant.rb index c0ac085..f30d4e1 100644 --- a/app/models/participant.rb +++ b/app/models/participant.rb @@ -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 - - # Fetch all forms associated with the 'Participant' role. - forms = Form.joins(:roles).where(roles: { id: participant_role.id }) + create_participant_onboarding_items + create_employer_onboarding_items_if_applicable + end + + 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 \ No newline at end of file diff --git a/lib/tasks/populate_forms.rake b/lib/tasks/populate_forms.rake new file mode 100644 index 0000000..c9a8571 --- /dev/null +++ b/lib/tasks/populate_forms.rake @@ -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 + \ No newline at end of file