This repository has been archived by the owner on Feb 6, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
203dfaa
commit 5d94297
Showing
2 changed files
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,15 @@ | ||
class Organization < ActiveRecord::Base | ||
has_many :users | ||
|
||
# Sample SQL generated: | ||
# SELECT visits.* | ||
# FROM "users" | ||
# INNER JOIN "patients" ON "patients"."user_id" = "users"."id" | ||
# INNER JOIN "patient_authorizations" ON "patient_authorizations"."patient_id" = "patients"."id" | ||
# INNER JOIN "visits" ON "visits"."patient_authorization_id" = "patient_authorizations"."id" | ||
# WHERE "users"."organization_id" = 321 | ||
def visits | ||
# Please pardon the glaring LoD violation! | ||
self.users.joins(:patients => :visits).select('visits.*').map(&:visits).flatten | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require 'spec_helper' | ||
|
||
describe Organization, "#visits" do | ||
let(:an_organization) { Organization.create! } | ||
let(:associated_user) { an_organization.users.create! } | ||
let(:associated_patient) { associated_user.patients.create! } | ||
let(:associated_patient_authorization) { associated_patient.patient_authorizations.create! } | ||
let(:associated_visit) { associated_patient_authorization.visits.create! } | ||
|
||
let(:another_organization) { Organization.create! } | ||
let(:unassociated_user) { another_organization.users.create! } | ||
let(:unassociated_patient) { unassociated_user.patients.create! } | ||
let(:unassociated_patient_authorization) { unassociated_patient.patient_authorizations.create! } | ||
let(:unassociated_visit) { unassociated_patient_authorization.visits.create! } | ||
|
||
subject { an_organization } | ||
|
||
its(:visits) { should include associated_visit } | ||
its(:visits) { should_not include unassociated_visit } | ||
end |