Class: StudySubject

Overview

requires

* subject_type_id

Defined Under Namespace

Classes: DuplicatesFound, NotTwoAbstracts

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StudySubjectInterviews

included

Methods included from StudySubjectHomexOutcome

included

Methods included from StudySubjectOperationalEvents

included

Methods included from StudySubjectEnrollments

included

Methods included from StudySubjectAddresses

included

Methods included from StudySubjectLanguages

included

Methods included from StudySubjectRaces

included

Methods included from StudySubjectAbstracts

included

Methods included from StudySubjectDuplicates

included

Methods included from StudySubjectIdentifier

included

Methods included from StudySubjectPii

included

Methods included from StudySubjectPatient

included

Methods included from StudySubjectValidations

included

Methods included from StudySubjectAssociations

included

Class Method Details

.find_case_by_patid(patid) ⇒ Object



203
204
205
206
207
208
209
210
# File 'app/models/study_subject.rb', line 203

def self.find_case_by_patid(patid)
	self.find(:first,	#	patid is unique so better only be 1
		:conditions => [
			'subject_type_id = ? AND patid = ?',
			subject_type_case_id, patid
		]
	)
end

.search(params = {}) ⇒ Object



145
146
147
# File 'app/models/study_subject.rb', line 145

def self.search(params={})
	StudySubjectSearch.new(params).study_subjects
end

Instance Method Details

#assign_icf_master_idObject



177
178
179
180
181
182
183
184
185
186
187
188
# File 'app/models/study_subject.rb', line 177

def assign_icf_master_id
	if icf_master_id.blank?
		next_icf_master_id = IcfMasterId.next_unused
		if next_icf_master_id
			self.update_attribute(:icf_master_id, next_icf_master_id.to_s)
			next_icf_master_id.study_subject = self
			next_icf_master_id.assigned_on   = Date.today
			next_icf_master_id.save!
		end
	end
	self
end

#childObject

Find the case or control subject with matching familyid except self.



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/models/study_subject.rb', line 53

def child
	if (subject_type_id == self.class.subject_type_mother_id) && !familyid.blank?
		self.class.find(:first,
			:include => [:subject_type],
			:conditions => [
				"study_subjects.id != ? AND subjectid = ? AND subject_type_id IN (?)", 
					id, familyid, 
					[self.class.subject_type_case_id,self.class.subject_type_control_id] ]
		)
	else
		nil
	end
end

#childid_to_sObject



216
217
218
# File 'app/models/study_subject.rb', line 216

def childid_to_s
	( is_mother? ) ? "#{child.try(:childid)} (mother)" : childid
end

#controlsObject

Find all the subjects with matching patid with subject_type Control except self. If patid is nil, this sql doesn’t work.

TODO Could fix, but this situation is unlikely.



103
104
105
106
107
108
109
110
111
# File 'app/models/study_subject.rb', line 103

def controls
	return [] unless is_case?
	self.class.find(:all, 
		:include => [:subject_type],
		:conditions => [
			"study_subjects.id != ? AND patid = ? AND subject_type_id = ?", 
				id, patid, self.class.subject_type_control_id ] 
	)
end

#create_motherObject

Create (or just return mother) a mother subject based on subject’s own data.



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
# File 'app/models/study_subject.rb', line 150

def create_mother
	return self if is_mother?
	#	The mother method will effectively find and itself.
	existing_mother = mother
	if existing_mother
		existing_mother
	else
		new_mother = self.class.new do |s|
			s.subject_type_id = self.class.subject_type_mother_id
			s.vital_status_id = VitalStatus['living'].id
			s.sex = 'F'
			s.hispanicity_id = mother_hispanicity_id
			s.first_name  = mother_first_name
			s.middle_name = mother_middle_name
			s.last_name   = mother_last_name
			s.maiden_name = mother_maiden_name

			#	protected attributes!
			s.matchingid = matchingid
			s.familyid   = familyid
		end
		new_mother.save!
		new_mother.assign_icf_master_id
		new_mother
	end
end

#familyObject

Find all the subjects with matching familyid except self.



80
81
82
83
84
85
86
87
# File 'app/models/study_subject.rb', line 80

def family
	return [] if familyid.blank?
	self.class.find(:all,
		:include => [:subject_type],
		:conditions => [
			"study_subjects.id != ? AND familyid = ?", id, familyid ]
	)
end

#icf_master_id_to_sObject



212
213
214
# File 'app/models/study_subject.rb', line 212

def icf_master_id_to_s
	( icf_master_id.blank? ) ?  "[no ID assigned]" : icf_master_id
end

#is_case?Boolean

Returns boolean of comparison true only if type is Case

Returns:

  • (Boolean)


129
130
131
# File 'app/models/study_subject.rb', line 129

def is_case?
	subject_type_id == self.class.subject_type_case_id
end

#is_control?Boolean

Returns boolean of comparison true only if type is Control

Returns:

  • (Boolean)


135
136
137
# File 'app/models/study_subject.rb', line 135

def is_control?
	subject_type_id == self.class.subject_type_control_id
end

#is_mother?Boolean

Returns boolean of comparison true only if type is Mother

Returns:

  • (Boolean)


141
142
143
# File 'app/models/study_subject.rb', line 141

def is_mother?
	subject_type_id == self.class.subject_type_mother_id
end

#matchingObject

Find all the subjects with matching matchingid except self.



90
91
92
93
94
95
96
97
98
# File 'app/models/study_subject.rb', line 90

def matching
	return [] if matchingid.blank?
	self.class.find(:all,
		:include => [:subject_type],
		:conditions => [
			"study_subjects.id != ? AND matchingid = ?", 
				id, matchingid ]
	)
end

#motherObject

Find the subject with matching familyid and subject_type of Mother.



68
69
70
71
72
73
74
75
76
77
# File 'app/models/study_subject.rb', line 68

def mother
	return nil if familyid.blank?
	self.class.find(:first,
		:include => [:subject_type],
		:conditions => { 
			:familyid        => familyid,
			:subject_type_id => self.class.subject_type_mother_id
		}
	)
end

#next_control_orderno(grouping = '6') ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
# File 'app/models/study_subject.rb', line 190

def next_control_orderno(grouping='6')
	return nil unless is_case?
	last_control = self.class.find(:first, 
		:order => 'orderno DESC', 
		:conditions => { 
			:subject_type_id   => self.class.subject_type_control_id,
			:case_control_type => grouping,
			:matchingid        => self.subjectid
		}
	)
	( last_control.try(:orderno) || 0 ) + 1
end

#rejected_controlsObject



113
114
115
116
117
118
119
120
121
# File 'app/models/study_subject.rb', line 113

def rejected_controls
	return [] unless is_case?
	CandidateControl.find(:all,
		:conditions => {
			:related_patid    => patid,
			:reject_candidate => true
		}
	)
end

#studyid_to_sObject



220
221
222
# File 'app/models/study_subject.rb', line 220

def studyid_to_s
	( is_mother? ) ? "n/a" : studyid
end

#to_sObject



123
124
125
# File 'app/models/study_subject.rb', line 123

def to_s
	[childid,'(',studyid,full_name,')'].compact.join(' ')
end