From 5d942970e22977270af25e93f6e0db375a8e039b Mon Sep 17 00:00:00 2001 From: Greg Sterndale Date: Tue, 22 Nov 2011 10:12:43 -0500 Subject: [PATCH] Organization #visits --- app/models/organization.rb | 12 ++++++++++++ spec/models/organization_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 spec/models/organization_spec.rb diff --git a/app/models/organization.rb b/app/models/organization.rb index 24ca728..3f658c8 100644 --- a/app/models/organization.rb +++ b/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 diff --git a/spec/models/organization_spec.rb b/spec/models/organization_spec.rb new file mode 100644 index 0000000..72fb7ff --- /dev/null +++ b/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