2024-01-16 20:54:44 -06:00
|
|
|
class Employer < ApplicationRecord
|
2024-02-05 15:58:46 -06:00
|
|
|
# Direct association with participants
|
|
|
|
has_many :direct_participants, class_name: 'Participant'
|
2024-02-02 23:12:36 -06:00
|
|
|
|
2024-02-05 15:58:46 -06:00
|
|
|
# Association through EmployerRecord
|
|
|
|
has_many :employer_records
|
|
|
|
has_many :indirect_participants, through: :employer_records, source: :participant
|
2024-01-29 22:04:56 -06:00
|
|
|
|
2024-02-05 15:58:46 -06:00
|
|
|
# Association with Workers through direct_participants
|
|
|
|
has_many :workers, through: :direct_participants
|
|
|
|
|
|
|
|
# Other methods...
|
2024-02-05 16:18:06 -06:00
|
|
|
def full_name
|
|
|
|
"#{first_name} #{last_name}"
|
|
|
|
end
|
2024-02-06 00:24:38 -06:00
|
|
|
|
2024-02-17 02:42:12 -06:00
|
|
|
has_many :onboarding_items, as: :owner
|
|
|
|
accepts_nested_attributes_for :onboarding_items
|
2024-02-06 00:24:38 -06:00
|
|
|
|
2024-02-07 05:13:12 -06:00
|
|
|
class Employer < ApplicationRecord
|
|
|
|
# Associations and other methods as before...
|
2024-02-06 00:24:38 -06:00
|
|
|
|
2024-02-07 05:13:12 -06:00
|
|
|
validate :unique_as_participant, if: -> { ssn_changed? || new_record? }
|
|
|
|
validates :ssn, uniqueness: { allow_blank: true }, if: -> { ssn.present? }
|
|
|
|
validates :tin, uniqueness: { allow_blank: true }, if: -> { tin.present? }
|
2024-02-06 00:24:38 -06:00
|
|
|
|
2024-02-07 05:13:12 -06:00
|
|
|
private
|
|
|
|
|
|
|
|
def unique_as_participant
|
|
|
|
if Participant.exists?(ssn: ssn) && (id.blank? || Participant.where.not(id: id).exists?(ssn: ssn))
|
|
|
|
errors.add(:ssn, 'A participant with the same SSN already exists')
|
2024-02-06 00:24:38 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2024-02-17 02:42:12 -06:00
|
|
|
|
|
|
|
def create_onboarding_items_for_forms
|
|
|
|
# Ensure there's a 'Role' model with an association set up between Forms and Roles.
|
|
|
|
employer_role = Role.find_by(name: 'Employer')
|
|
|
|
return unless employer_role
|
|
|
|
|
|
|
|
# Fetch all forms associated with the 'Employer' role.
|
|
|
|
forms = Form.joins(:roles).where(roles: { id: employer_role.id })
|
|
|
|
forms.each do |form|
|
|
|
|
self.onboarding_items.find_or_create_by(form: form)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-02-07 05:13:12 -06:00
|
|
|
end
|
2024-02-17 02:42:12 -06:00
|
|
|
|
2024-02-07 05:13:12 -06:00
|
|
|
|