31 lines
887 B
Ruby
31 lines
887 B
Ruby
class Form < ApplicationRecord
|
|
# Assuming each form can have many onboarding items associated with different owners.
|
|
has_many :onboarding_items
|
|
has_many :forms_roles
|
|
has_many :form_roles, through: :forms_roles
|
|
accepts_nested_attributes_for :forms_roles, allow_destroy: true
|
|
|
|
validates :name, presence: true
|
|
# Include more validations as necessary
|
|
|
|
after_save :update_onboarding_items
|
|
|
|
private
|
|
|
|
def form_role_ids=(ids)
|
|
self.form_roles = ids.reject(&:blank?).map { |id| Role.find(id) }
|
|
end
|
|
|
|
|
|
|
|
def update_onboarding_items
|
|
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
|
|
end
|
|
end
|
|
end |