Skip to content
This repository has been archived by the owner on Feb 6, 2019. It is now read-only.

Commit

Permalink
Organization #visits
Browse files Browse the repository at this point in the history
  • Loading branch information
gsterndale committed Nov 22, 2011
1 parent 203dfaa commit 5d94297
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app/models/organization.rb
@@ -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
20 changes: 20 additions & 0 deletions spec/models/organization_spec.rb
@@ -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

0 comments on commit 5d94297

Please sign in to comment.