Class: Patient

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/patient.rb

Overview

This is the central record representing patient data.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) c32_modules

Hash of all the C32 modules, returned as Symbols, keyed against the active_record association names used to represent them in the patient model. Used to look up which association to access for a particular component module's data.



61
62
63
64
65
66
67
68
69
70
# File 'app/models/patient.rb', line 61

def self.c32_modules
  c32_associations = reflect_on_all_associations.select do |reflection|
    extends = reflection.options[:extend]
    extends == HasC32ComponentExtension::C32Component || extends.try(:include?, HasC32ComponentExtension::C32Component)
  end
  return c32_associations.inject({}) do |hash,reflection|
    hash[reflection.c32_component_module_name] = reflection.name
    hash 
  end
end

+ (Object) find_by_patient_identifier(id)

find the patient template from the XDS id these lookup values are neither indexed nor guaranteed to be unique. XXX



308
309
310
311
312
313
314
315
316
317
# File 'app/models/patient.rb', line 308

def self.find_by_patient_identifier( id )

  split_id = id.split('^^^')
  patient_id = PatientIdentifier.find( :first, 
      :conditions => {  :patient_identifier => split_id.first, 
                        :affinity_domain => split_id.second }
   ).try( :patient_id )
   
   Patient.find( patient_id ) unless patient_id.nil?
end

Instance Method Details

- (Patient) clone

Create a deep copy of the given patient record.

Returns:



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/patient.rb', line 98

def clone
  copy = super
  transaction do
    copy.save!

    %w[
      registration_information support information_source advance_directive
    ].each do |assoc|
      copy.send("#{assoc}\=", send(assoc).clone) if send(assoc)
    end

    %w[
      patient_identifiers languages providers medications allergies conditions
      all_results immunizations encounters procedures medical_equipments insurance_providers
    ].each do |assoc|
      send(assoc).each do |item|
        copy.send(assoc) << item.clone
      end
    end

  end
  copy
end

- (Boolean) editable_by?(owner)

Returns:

  • (Boolean)


37
38
39
# File 'app/models/patient.rb', line 37

def editable_by? owner
  user == owner
end

- (String) generate_unique_id

Generate an OID using the time and the patient's ActiveRecord ID.

Returns:



75
76
77
# File 'app/models/patient.rb', line 75

def generate_unique_id
  "1.3.6.1.4.1.21367.2009.5.14.#{id}.#{Time.now.to_i}"
end

- (Object) insurance_provider_guarantors

these are used in the insurance_provider_* controllers



42
43
44
# File 'app/models/patient.rb', line 42

def insurance_provider_guarantors
  InsuranceProviderGuarantor.by_patient(self)
end

- (Object) insurance_provider_patients



45
46
47
# File 'app/models/patient.rb', line 45

def insurance_provider_patients
  InsuranceProviderPatient.by_patient(self)
end

- (Object) insurance_provider_subscribers



48
49
50
# File 'app/models/patient.rb', line 48

def insurance_provider_subscribers
  InsuranceProviderSubscriber.by_patient(self)
end

- (Object) patient_identifier



79
80
81
# File 'app/models/patient.rb', line 79

def patient_identifier
  registration_information.try(:affinity_domain_identifier)
end

- (Object) randomize

Populate a patient record with random values. This does not generate semantically valid C32 documents.



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'app/models/patient.rb', line 224

def randomize()
  self.registration_information ||= RegistrationInformation.new

  registration_information.randomize(self)
  self.name = registration_information.full_name

  # if patient is female, 10% chance patient is pregnant
  if self.registration_information.gender.code == 'F'
    random_pregnant = rand(10) 
    if random_pregnant > 8
      self.pregnant = true 
    else
      self.pregnant = false
    end
  else
    self.pregnant = nil
  end 

  @provider = Provider.new
  @provider.randomize(self.registration_information)
  self.providers << @provider

  @insurance = InsuranceProvider.new
  @insurance.randomize(self.registration_information)
  self.insurance_providers << @insurance

  @allergy = Allergy.new
  @allergy.randomize(self.registration_information.date_of_birth)
  self.allergies << @allergy

  @condition = Condition.new
  @condition.randomize(self.registration_information.date_of_birth)
  self.conditions << @condition

  @language = Language.new
  @language.randomize()
  self.languages << @language

  @immunization = Immunization.new
  @immunization.randomize(self.registration_information.date_of_birth)
  self.immunizations << @immunization

  @medication = Medication.new
  @medication.randomize()
  self.medications << @medication

  self.support = Support.new
  self.support.randomize(self.registration_information.date_of_birth)
  
  self.information_source = InformationSource.new
  self.information_source.randomize()

  @result = Result.new
  @result.randomize()
  self.results << @result

  @vital_sign = VitalSign.new
  @vital_sign.randomize()
  self.vital_signs << @vital_sign

  self.advance_directive = AdvanceDirective.new
  self.advance_directive.randomize(self.registration_information.date_of_birth)

  @encounter = Encounter.new
  @encounter.randomize(self.registration_information.date_of_birth)
  self.encounters << @encounter

  @procedure = Procedure.new
  @procedure.randomize(self.registration_information.date_of_birth)
  self.procedures << @procedure

  @medical_equipment = MedicalEquipment.new
  @medical_equipment.randomize(self.registration_information.date_of_birth)
  self.medical_equipments << @medical_equipment

  @patient_identifier = PatientIdentifier.new
  @patient_identifier.randomize()
  self.patient_identifiers << @patient_identifier

  self
end

- (Hash) source_patient_info

Generate a hash containing source_patient_info for use in XDS metadata.

Returns:

  • (Hash)

    patient info



86
87
88
89
90
91
92
93
# File 'app/models/patient.rb', line 86

def source_patient_info
  spi = {}
  spi[:name] = name
  spi[:gender] = registration_information.try(:gender).try(:code)
  spi[:date_of_birth] = registration_information.try(:date_of_birth).try(:to_s,:brief)
  spi[:source_patient_identifier] = patient_identifier
  spi
end

- (Builder::XmlMarkup) to_c32(xml = nil)

Build a C32 XML document representing the patient.

Returns:

  • (Builder::XmlMarkup)

    C32 XML representation of patient data



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'app/models/patient.rb', line 125

def to_c32(xml = nil)
  xml ||= Builder::XmlMarkup.new(:indent => 2)

  xml.ClinicalDocument("xsi:schemaLocation" => "urn:hl7-org:v3 http://xreg2.nist.gov:8080/hitspValidation/schema/cdar2c32/infrastructure/cda/C32_CDA.xsd",
                       "xmlns" => "urn:hl7-org:v3",
                       "xmlns:sdtc" => "urn:hl7-org:sdtc",
                       "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance") do
    xml.realmCode( "code" => "US" ) #C32 2.4
    xml.typeId("root" => "2.16.840.1.113883.1.3",
               "extension" => "POCD_HD000040")
    xml.templateId("root" => "2.16.840.1.113883.3.27.1776",
                   "assigningAuthorityName" => "CDA/R2")
    xml.templateId("root" => "2.16.840.1.113883.10.20.1", 
                   "assigningAuthorityName" => "CCD")
    xml.templateId("root" => "2.16.840.1.113883.3.88.11.32.1", 
                   "assigningAuthorityName" => "HITSP/C32")
    xml.templateId("root" => "2.16.840.1.113883.10.20.3",
                   "assigningAuthorityName" => "CCD")
    xml.templateId("root" => "1.3.6.1.4.1.19376.1.5.3.1.1.1")
    xml.id("root" => "2.16.840.1.113883.3.72", 
           "extension" => "Laika C32 Test", 
           "assigningAuthorityName" => "Laika: An Open Source EHR Testing Framework projectlaika.org")
    xml.code("code" => "34133-9", 
             "displayName" => "Summarization of patient data", 
             "codeSystem" => "2.16.840.1.113883.6.1", 
             "codeSystemName" => "LOINC")
             
    xml.title(name)

    if registration_information.try(:document_timestamp)
      xml.effectiveTime("value" => c32_timestamp(registration_information.document_timestamp))
    else
      xml.effectiveTime("value" => c32_timestamp(updated_at))
    end
    xml.confidentialityCode
    xml.languageCode("code" => "en-US")

    # Start Person (Registation) Information
    xml.recordTarget do
      xml.patientRole do
        registration_information.try(:to_c32, xml)
      end
    end
    # End Person (Registation) Information

    information_source.try(:to_c32, xml)

    xml.custodian do
      xml.assignedCustodian do
        xml.representedCustodianOrganization do
          xml.id
          xml.name
          xml.telecom
          xml.addr
        end
      end
    end

    if support && support.contact_type && support.contact_type.code != "GUARD"
      support.to_c32(xml)
    end

    providers.to_c32(xml)

    # Start CCD/C32 Modules
    xml.component do
      xml.structuredBody do
        
        pregnancy_c32(xml)

        conditions.to_c32(xml)

        allergies.to_c32(xml)

        insurance_providers.to_c32(xml)

        medications.to_c32(xml)

        advance_directive.try(:to_c32, xml)

        vital_signs.to_c32(xml)

        results.to_c32(xml)

        immunizations.to_c32(xml)

        encounters.to_c32(xml)

        procedures.to_c32(xml)

        medical_equipments.to_c32(xml)
      end
    end
    # END CCD/C32 Modules
  end
end