2018-12-06 17:49:50 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class RegionalOffice < ApplicationRecord
|
2019-03-25 20:56:31 -04:00
|
|
|
################
|
|
|
|
# Associations #
|
|
|
|
################
|
|
|
|
|
2019-06-23 11:59:44 -04:00
|
|
|
belongs_to :federal_subject
|
2018-12-06 17:49:50 -05:00
|
|
|
|
2019-07-20 07:56:26 -04:00
|
|
|
has_many :all_relationships,
|
|
|
|
class_name: 'Relationship',
|
|
|
|
inverse_of: :regional_office
|
2019-07-20 06:46:49 -04:00
|
|
|
|
2019-07-20 07:38:12 -04:00
|
|
|
has_many :current_relationships,
|
|
|
|
lambda {
|
|
|
|
select('DISTINCT ON (relationships.person_id) *')
|
|
|
|
.order(person_id: :asc, from_date: :desc)
|
|
|
|
},
|
|
|
|
class_name: 'Relationship',
|
|
|
|
inverse_of: :regional_office
|
|
|
|
|
2019-07-20 07:46:46 -04:00
|
|
|
has_many :current_supporter_relationships,
|
|
|
|
lambda {
|
|
|
|
select('DISTINCT ON (relationships.person_id) *')
|
|
|
|
.where(status: :supporter)
|
|
|
|
.order(person_id: :asc, from_date: :desc)
|
|
|
|
},
|
2019-07-20 06:46:49 -04:00
|
|
|
class_name: 'Relationship',
|
|
|
|
inverse_of: :regional_office
|
|
|
|
|
2019-07-20 07:46:46 -04:00
|
|
|
has_many :current_member_relationships,
|
|
|
|
lambda {
|
|
|
|
select('DISTINCT ON (relationships.person_id) *')
|
|
|
|
.where(status: :member)
|
|
|
|
.order(person_id: :asc, from_date: :desc)
|
|
|
|
},
|
2019-07-20 06:46:49 -04:00
|
|
|
class_name: 'Relationship',
|
|
|
|
inverse_of: :regional_office
|
2019-04-28 09:06:22 -04:00
|
|
|
|
2019-07-25 21:50:57 -04:00
|
|
|
has_many :current_regional_manager_relationships,
|
2019-07-20 07:46:46 -04:00
|
|
|
lambda {
|
|
|
|
select('DISTINCT ON (relationships.person_id) *')
|
2019-07-25 21:50:57 -04:00
|
|
|
.where(status: :member, role: :regional_manager)
|
2019-07-20 07:46:46 -04:00
|
|
|
.order(person_id: :asc, from_date: :desc)
|
|
|
|
},
|
2019-07-20 06:58:39 -04:00
|
|
|
class_name: 'Relationship',
|
|
|
|
inverse_of: :regional_office
|
|
|
|
|
2019-07-25 21:53:21 -04:00
|
|
|
has_many :current_regional_supervisor_relationships,
|
2019-07-20 07:46:46 -04:00
|
|
|
lambda {
|
|
|
|
select('DISTINCT ON (relationships.person_id) *')
|
2019-07-25 21:53:21 -04:00
|
|
|
.where(status: :member, role: :regional_supervisor)
|
2019-07-20 07:46:46 -04:00
|
|
|
.order(person_id: :asc, from_date: :desc)
|
|
|
|
},
|
2019-07-20 06:58:39 -04:00
|
|
|
class_name: 'Relationship',
|
|
|
|
inverse_of: :regional_office
|
|
|
|
|
2019-07-20 07:56:26 -04:00
|
|
|
has_many :all_people,
|
|
|
|
class_name: 'Person',
|
2019-07-20 08:01:24 -04:00
|
|
|
inverse_of: :current_regional_office,
|
2019-07-20 07:56:26 -04:00
|
|
|
through: :all_relationships,
|
2019-07-20 06:25:23 -04:00
|
|
|
source: :person
|
2019-07-20 05:53:51 -04:00
|
|
|
|
2019-07-20 07:38:12 -04:00
|
|
|
has_many :current_people,
|
|
|
|
class_name: 'Person',
|
2019-07-20 08:01:24 -04:00
|
|
|
inverse_of: :current_regional_office,
|
2019-07-20 07:38:12 -04:00
|
|
|
through: :current_relationships,
|
|
|
|
source: :person
|
|
|
|
|
2019-07-20 07:46:46 -04:00
|
|
|
has_many :current_supporter_people,
|
2019-07-20 06:46:49 -04:00
|
|
|
class_name: 'Person',
|
2019-07-20 08:01:24 -04:00
|
|
|
inverse_of: :current_regional_office,
|
2019-07-20 07:46:46 -04:00
|
|
|
through: :current_supporter_relationships,
|
2019-07-20 06:46:49 -04:00
|
|
|
source: :person
|
|
|
|
|
2019-07-20 07:46:46 -04:00
|
|
|
has_many :current_member_people,
|
2019-07-20 06:46:49 -04:00
|
|
|
class_name: 'Person',
|
2019-07-20 08:01:24 -04:00
|
|
|
inverse_of: :current_regional_office,
|
2019-07-20 07:46:46 -04:00
|
|
|
through: :current_member_relationships,
|
2019-07-20 06:46:49 -04:00
|
|
|
source: :person
|
|
|
|
|
2019-07-25 21:50:57 -04:00
|
|
|
has_many :current_regional_manager_people,
|
2019-07-20 06:58:39 -04:00
|
|
|
class_name: 'Person',
|
2019-07-20 08:01:24 -04:00
|
|
|
inverse_of: :current_regional_office,
|
2019-07-25 21:50:57 -04:00
|
|
|
through: :current_regional_manager_relationships,
|
2019-07-20 06:58:39 -04:00
|
|
|
source: :person
|
|
|
|
|
2019-07-25 21:53:21 -04:00
|
|
|
has_many :current_regional_supervisor_people,
|
2019-07-20 06:58:39 -04:00
|
|
|
class_name: 'Person',
|
2019-07-20 08:01:24 -04:00
|
|
|
inverse_of: :current_regional_office,
|
2019-07-25 21:53:21 -04:00
|
|
|
through: :current_regional_supervisor_relationships,
|
2019-07-20 06:58:39 -04:00
|
|
|
source: :person
|
|
|
|
|
2019-03-25 20:56:31 -04:00
|
|
|
###############
|
|
|
|
# Validations #
|
|
|
|
###############
|
|
|
|
|
2019-06-23 11:59:44 -04:00
|
|
|
validates :federal_subject, uniqueness: true
|
2018-12-06 17:49:50 -05:00
|
|
|
end
|