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