1
0
Fork 0

Add associations

This commit is contained in:
Alex Kotov 2019-08-18 06:17:40 +05:00
parent 6f59b236f9
commit 99479e3fd3
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
2 changed files with 55 additions and 0 deletions

View file

@ -55,6 +55,15 @@ class RegionalOffice < ApplicationRecord
class_name: 'Relationship',
inverse_of: :regional_office
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,
@ -91,6 +100,12 @@ class RegionalOffice < ApplicationRecord
through: :current_regional_supervisor_relationships,
source: :person
has_one :current_regional_secretary_person,
class_name: 'Person',
inverse_of: :current_regional_office,
through: :current_regional_secretary_relationship,
source: :person
has_many :all_accounts,
class_name: 'Account',
through: :all_people,
@ -121,6 +136,11 @@ class RegionalOffice < ApplicationRecord
through: :current_regional_supervisor_people,
source: :account
has_one :current_regional_secretary_account,
class_name: 'Account',
through: :current_regional_secretary_person,
source: :account
###############
# Validations #
###############

View file

@ -86,6 +86,20 @@ RSpec.describe RegionalOffice do
end
end
describe '#current_regional_secretary_relationship' do
it do
is_expected.to \
have_one(:current_regional_secretary_relationship)
.class_name('Relationship')
.inverse_of(:regional_office)
.dependent(:restrict_with_exception)
.conditions(status: :member,
role: :regional_manager,
regional_secretary_flag: :regional_secretary)
.order(person_id: :asc, from_date: :desc)
end
end
describe '#all_people' do
it do
is_expected.to \
@ -152,6 +166,17 @@ RSpec.describe RegionalOffice do
end
end
describe '#current_regional_secretary_person' do
it do
is_expected.to \
have_one(:current_regional_secretary_person)
.class_name('Person')
.inverse_of(:current_regional_office)
.through(:current_regional_secretary_relationship)
.source(:person)
end
end
describe '#all_accounts' do
it do
is_expected.to \
@ -211,4 +236,14 @@ RSpec.describe RegionalOffice do
.source(:account)
end
end
describe '#current_regional_secretary_account' do
it do
is_expected.to \
have_one(:current_regional_secretary_account)
.class_name('Account')
.through(:current_regional_secretary_person)
.source(:account)
end
end
end