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/regional_office.rb

150 lines
4.6 KiB
Ruby
Raw Normal View History

2018-12-06 17:49:50 -05:00
# frozen_string_literal: true
2019-08-17 21:18:13 -04:00
class RegionalOffice < ApplicationRecord # rubocop:disable Metrics/ClassLength
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
has_many :all_relationships,
class_name: 'Relationship',
inverse_of: :regional_office
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
has_many :current_supporter_relationships,
lambda {
select('DISTINCT ON (relationships.person_id) *')
2019-07-25 23:58:39 -04:00
.supporters
.order(person_id: :asc, from_date: :desc)
},
class_name: 'Relationship',
inverse_of: :regional_office
has_many :current_member_relationships,
lambda {
select('DISTINCT ON (relationships.person_id) *')
2019-07-25 23:58:39 -04:00
.members
.order(person_id: :asc, from_date: :desc)
},
class_name: 'Relationship',
inverse_of: :regional_office
has_many :current_regional_manager_relationships,
lambda {
select('DISTINCT ON (relationships.person_id) *')
2019-07-25 23:58:39 -04:00
.regional_managers
.order(person_id: :asc, from_date: :desc)
},
class_name: 'Relationship',
inverse_of: :regional_office
has_many :current_regional_supervisor_relationships,
lambda {
select('DISTINCT ON (relationships.person_id) *')
2019-07-25 23:58:39 -04:00
.regional_supervisors
.order(person_id: :asc, from_date: :desc)
},
class_name: 'Relationship',
inverse_of: :regional_office
2019-08-17 21:17:40 -04:00
has_one :current_regional_secretary_relationship,
lambda {
select('DISTINCT ON (relationships.person_id) *')
.regional_secretaries
.order(person_id: :asc, from_date: :desc)
},
class_name: 'Relationship',
inverse_of: :regional_office
has_many :all_people,
class_name: 'Person',
inverse_of: :current_regional_office,
through: :all_relationships,
source: :person
has_many :current_people,
class_name: 'Person',
inverse_of: :current_regional_office,
through: :current_relationships,
source: :person
has_many :current_supporter_people,
class_name: 'Person',
inverse_of: :current_regional_office,
through: :current_supporter_relationships,
source: :person
has_many :current_member_people,
class_name: 'Person',
inverse_of: :current_regional_office,
through: :current_member_relationships,
source: :person
has_many :current_regional_manager_people,
class_name: 'Person',
inverse_of: :current_regional_office,
through: :current_regional_manager_relationships,
source: :person
has_many :current_regional_supervisor_people,
class_name: 'Person',
inverse_of: :current_regional_office,
through: :current_regional_supervisor_relationships,
source: :person
2019-08-17 21:17:40 -04:00
has_one :current_regional_secretary_person,
class_name: 'Person',
inverse_of: :current_regional_office,
through: :current_regional_secretary_relationship,
source: :person
2019-08-17 20:59:07 -04:00
has_many :all_accounts,
class_name: 'Account',
through: :all_people,
source: :account
has_many :current_accounts,
class_name: 'Account',
through: :current_people,
source: :account
has_many :current_supporter_accounts,
class_name: 'Account',
through: :current_supporter_people,
source: :account
has_many :current_member_accounts,
class_name: 'Account',
through: :current_member_people,
source: :account
has_many :current_regional_manager_accounts,
class_name: 'Account',
through: :current_regional_manager_people,
source: :account
has_many :current_regional_supervisor_accounts,
class_name: 'Account',
through: :current_regional_supervisor_people,
source: :account
2019-08-17 21:17:40 -04:00
has_one :current_regional_secretary_account,
class_name: 'Account',
through: :current_regional_secretary_person,
source: :account
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