1
0
Fork 0
This repository has been archived on 2023-03-27. You can view files and clone it, but cannot push or open issues or pull requests.
lpr-partynest/app/models/relationship.rb

87 lines
2 KiB
Ruby
Raw Normal View History

2019-04-27 10:24:04 -04:00
# frozen_string_literal: true
class Relationship < ApplicationRecord
2019-07-21 22:42:04 -04:00
pg_enum :status, %i[supporter excluded member]
2019-07-25 22:09:52 -04:00
pg_enum :role, %i[
federal_manager
federal_supervisor
regional_manager
regional_supervisor
]
2019-06-04 22:33:45 -04:00
pg_enum :federal_secretary_flag, %i[federal_secretary]
pg_enum :regional_secretary_flag, %i[regional_secretary]
2019-04-27 10:24:04 -04:00
################
# Associations #
################
belongs_to :person, inverse_of: :all_relationships
2019-07-20 06:52:03 -04:00
2019-08-17 23:55:12 -04:00
belongs_to :regional_office
2019-04-28 08:48:51 -04:00
belongs_to :initiator_account,
class_name: 'Account',
inverse_of: false,
optional: true
2019-07-25 22:06:01 -04:00
##########
# Scopes #
##########
2019-07-25 23:50:30 -04:00
scope :supporters, -> { where(status: :supporter) }
scope :excluded, -> { where(status: :excluded) }
scope :members, -> { where(status: :member) }
2019-07-25 23:58:39 -04:00
scope :federal_managers, -> { members.where(role: :federal_manager) }
2019-07-25 22:06:01 -04:00
2019-07-25 23:58:39 -04:00
scope :federal_supervisors, -> { members.where(role: :federal_supervisor) }
2019-07-25 22:09:52 -04:00
2019-07-25 23:58:39 -04:00
scope :federal_secretaries, lambda {
federal_managers.where(federal_secretary_flag: :federal_secretary)
}
2019-07-25 23:58:39 -04:00
scope :regional_managers, -> { members.where(role: :regional_manager) }
2019-07-25 23:50:30 -04:00
2019-07-25 23:58:39 -04:00
scope :regional_supervisors, -> { members.where(role: :regional_supervisor) }
2019-07-25 23:50:30 -04:00
2019-07-25 23:58:39 -04:00
scope :regional_secretaries, lambda {
regional_managers.where(regional_secretary_flag: :regional_secretary)
}
2019-04-28 08:48:51 -04:00
###############
# Validations #
###############
validates :from_date, presence: true, uniqueness: { scope: :person_id }
2019-06-04 22:33:45 -04:00
validates :status, presence: true
2019-07-20 05:12:58 -04:00
validates :role, absence: { unless: :member? }
validates :federal_secretary_flag,
allow_nil: true,
absence: { unless: :federal_manager? },
uniqueness: true
validates :regional_secretary_flag,
allow_nil: true,
absence: { unless: :regional_manager? },
uniqueness: { scope: :regional_office_id }
2019-08-14 23:35:04 -04:00
###########
# Methods #
###########
def position
return 'federal_secretary' if federal_secretary?
return 'regional_secretary' if regional_secretary?
role
end
2019-04-27 10:24:04 -04:00
end