2024-02-15 17:23:54 -06:00
|
|
|
class Form < ApplicationRecord
|
2024-02-16 23:36:48 -06:00
|
|
|
# Assuming each form can have many onboarding items associated with different owners.
|
|
|
|
has_many :onboarding_items
|
2024-02-17 02:42:12 -06:00
|
|
|
has_many :forms_roles
|
|
|
|
has_many :roles, through: :forms_roles
|
|
|
|
accepts_nested_attributes_for :forms_roles, allow_destroy: true
|
2024-02-16 23:36:48 -06:00
|
|
|
|
|
|
|
validates :name, presence: true
|
|
|
|
validates :role, inclusion: { in: %w[Participant Worker Vendor Employer] }
|
|
|
|
# Include more validations as necessary
|
|
|
|
|
|
|
|
after_save :update_onboarding_items
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def update_onboarding_items
|
2024-02-17 02:42:12 -06:00
|
|
|
roles.each do |role|
|
|
|
|
# Assuming `role.name` is 'Participant', 'Worker', 'Vendor', or 'Employer'
|
|
|
|
owner_class = role.name.constantize
|
|
|
|
|
|
|
|
owner_class.find_each do |owner|
|
|
|
|
OnboardingItem.find_or_create_by(owner: owner, form: self)
|
|
|
|
end
|
2024-02-16 23:36:48 -06:00
|
|
|
end
|
|
|
|
end
|
2024-02-17 02:42:12 -06:00
|
|
|
end
|