Module: EnrollmentValidations

Included in:
Enrollment
Defined in:
app/models/enrollment_validations.rb

Overview

Simply extracted some code to clean up model. I’d like to do this to all of the really big classes but let’s see how this goes first.

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
# File 'app/models/enrollment_validations.rb', line 7

def self.included(base)
# Must delay the calls to these ActiveRecord methods
# or it will raise many "undefined method"s.
base.class_eval do

  validates_presence_of   :project_id
  validates_presence_of   :project, :if => :project_id
  validates_uniqueness_of :project_id, :scope => [:study_subject_id], :allow_blank => true

  validates_length_of :notes, :maximum => 65000, :allow_blank => true

  validates_presence_of :ineligible_reason_id,
    :message => 'required if is_eligible is No',
    :if => :is_not_eligible?
  validates_absence_of :ineligible_reason_id,
    :message => 'not allowed unless is_eligible is No',
    :unless => :is_not_eligible?
  validates_presence_of :ineligible_reason, :if => :ineligible_reason_id

  validates_presence_of :ineligible_reason_specify,
    :message => 'required if ineligible reason is Other',
    :if => :ineligible_reason_is_other?
  validates_absence_of :ineligible_reason_specify,
    :message => 'not allowed unless is_eligible is No',
    :unless => :is_not_eligible?

  validates_presence_of :reason_not_chosen,
    :message => 'requires if is_chosen is No',
    :if => :is_not_chosen?
  validates_absence_of :reason_not_chosen,
    :message => 'not allowed unless is_chosen is No',
    :unless => :is_not_chosen?

  validates_presence_of :refusal_reason_id,
    :message => "required if consented is No",
    :if => :not_consented?
  validates_absence_of :refusal_reason_id,
    :message => "not allowed unless consented is No",
    :unless => :not_consented?
  validates_presence_of :refusal_reason, :if => :refusal_reason_id

# validates_presence_of :consented_on,
#   :message => 'date is required when adding consent information',
#   :if => :consented?
  validates_presence_of :consented_on,
    :message => 'date is required when adding consent information',
    :unless => :consent_unknown?
#   :if => :not_consented?
  validates_absence_of :consented_on,
    :message => "not allowed if consented is blank or Don't Know",
    :if => :consent_unknown?
  validates_past_date_for :consented_on

  validates_presence_of :other_refusal_reason,
    :message => "required if refusal reason is Other",
    :if => :refusal_reason_is_other?
  validates_absence_of :other_refusal_reason,
    :message => "not allowed unless consented is No",
    :unless => :not_consented?

  validates_presence_of :terminated_reason,
    :message => "required if terminated participation is Yes",
    :if => :terminated_participation?
  validates_absence_of :terminated_reason,
    :message => "not allowed unless terminated participation is Yes",
    :unless => :terminated_participation?

  validates_presence_of :completed_on,
    :message => "required if is_complete is Yes",
    :if => :is_complete?
  validates_absence_of :completed_on,
    :message => "not allowed unless is_complete is Yes",
    :unless => :is_complete?
  validates_past_date_for :completed_on

  validates_absence_of :document_version_id,
    :message => "not allowed if consented is blank or Don't Know",
    :if => :consent_unknown?
  validates_presence_of :document_version, :if => :document_version_id
  
  validates_complete_date_for :consented_on, :allow_nil => true
  validates_complete_date_for :completed_on, :allow_nil => true

  validates_length_of :recruitment_priority,      :maximum => 250, :allow_blank => true
  validates_length_of :ineligible_reason_specify, :maximum => 250, :allow_blank => true
  validates_length_of :other_refusal_reason,      :maximum => 250, :allow_blank => true
  validates_length_of :reason_not_chosen,         :maximum => 250, :allow_blank => true
  validates_length_of :terminated_reason,         :maximum => 250, :allow_blank => true
  validates_length_of :reason_closed,             :maximum => 250, :allow_blank => true


  validates_inclusion_of :consented, :is_eligible,
    :is_chosen, :is_complete, :terminated_participation,
    :is_candidate,
      :in => YNDK.valid_values, :allow_nil => true

  validates_inclusion_of :use_smp_future_rsrch,
    :use_smp_future_cancer_rsrch, :use_smp_future_other_rsrch,
    :share_smp_with_others, :contact_for_related_study,
    :provide_saliva_smp, :receive_study_findings,
      :in => ADNA.valid_values, :allow_nil => true


#
# NOTE: BEWARE of POSSIBLE strings in these comparisons.
#   Rails SHOULD actually convert the incoming 
#   params (which are strings) to integers or nil.
#

  #  Return boolean of comparison
  #  true only if is_eligible == 2
  def is_not_eligible?
#   is_eligible.to_i == YNDK[:no]  #2
    is_eligible == YNDK[:no]  #2
  end

  #  Return boolean of comparison
  #  true only if is_chosen == 2
  def is_not_chosen?
#   is_chosen.to_i == YNDK[:no]  #2
    is_chosen == YNDK[:no]  #2
  end

  #  Return boolean of comparison
  #  true only if consented == 1
# no longer used, I believe
# def consented?
#   consented == 1
# end

  #  Return boolean of comparison
  #  true only if consented == 2
  def not_consented?
#   consented.to_i == YNDK[:no]  #2
    consented == YNDK[:no]  #2
  end

  #  Return boolean of comparison
  #  true only if consented == nil or 999
  def consent_unknown?
#   [nil,999,'','999'].include?(consented) # not 1 or 2
    [nil,999].include?(consented) # not 1 or 2
  end

  #  Return boolean of comparison
  #  true only if terminated_participation == 1
  def terminated_participation?
#   terminated_participation.to_i == YNDK[:yes]  #  1
    terminated_participation == YNDK[:yes]  #  1
  end

  #  Return boolean of comparison
  #  true only if is_complete == 1
  def is_complete?
#   is_complete.to_i == YNDK[:yes] # 1
    is_complete == YNDK[:yes] # 1
  end

end # class_eval
end