Class: Quby::Answers::Entities::Answer
- Inherits:
-
Object
- Object
- Quby::Answers::Entities::Answer
- Extended by:
- ActiveModel::Naming, ActiveModel::Translation
- Defined in:
- lib/quby/answers/entities/answer.rb
Instance Attribute Summary collapse
- #_id ⇒ String
-
#aborted ⇒ Object
Returns the value of attribute aborted.
- #actions ⇒ Object
- #active ⇒ Boolean
- #completed_at ⇒ Time
- #completion ⇒ Object
- #created_at ⇒ Time
- #dsl_last_update ⇒ Time
-
#extra_failed_validations ⇒ Object
Returns the value of attribute extra_failed_validations.
-
#extra_question_values ⇒ Object
For setting raw content values and failed validations.
- #flags ⇒ Hash<String, Boolean>
-
#import_notes ⇒ Hash
For answers that are imported from external sources.
- #outcome ⇒ Outcome
-
#outcome_generated_at ⇒ Object
Returns the value of attribute outcome_generated_at.
- #patient ⇒ Hash
- #patient_id ⇒ String
- #questionnaire_id ⇒ String
- #questionnaire_key ⇒ String
-
#raw_params ⇒ Hash
The raw form data (for recovery purposes).
- #scores ⇒ Object
- #started_at ⇒ Time
- #test ⇒ Boolean
- #textvars ⇒ Hash<String, String>
- #token ⇒ String
- #updated_at ⇒ Time
-
#value ⇒ Hash
The filtered and transformed form data.
Instance Method Summary collapse
- #action ⇒ Object
- #all_blank? ⇒ Boolean
- #as_json(options = {}) ⇒ Object
- #attributes ⇒ Object
- #completed? ⇒ Boolean
- #enhance_by_dsl ⇒ Object
- #errors ⇒ Object
- #id ⇒ Object
-
#initialize(_id: nil, questionnaire_id: nil, questionnaire_key: nil, raw_params: nil, value: nil, patient_id: nil, patient: nil, token: nil, active: true, test: false, created_at: nil, updated_at: nil, started_at: nil, completed_at: nil, outcome: nil, outcome_generated_at: nil, scores: nil, actions: nil, completion: nil, dsl_last_update: nil, import_notes: nil, flags: nil, textvars: nil) ⇒ Answer
constructor
A new instance of Answer.
- #mark_completed(start_time) ⇒ Object
-
#questionnaire ⇒ Object
Faux belongs_to :questionnaire.
- #to_param ⇒ Object
- #url_params(options = {}) ⇒ Object
- #valid? ⇒ Boolean
- #value_by_regular_values ⇒ Object
- #value_by_values ⇒ Object
Constructor Details
#initialize(_id: nil, questionnaire_id: nil, questionnaire_key: nil, raw_params: nil, value: nil, patient_id: nil, patient: nil, token: nil, active: true, test: false, created_at: nil, updated_at: nil, started_at: nil, completed_at: nil, outcome: nil, outcome_generated_at: nil, scores: nil, actions: nil, completion: nil, dsl_last_update: nil, import_notes: nil, flags: nil, textvars: nil) ⇒ Answer
Returns a new instance of Answer.
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 |
# File 'lib/quby/answers/entities/answer.rb', line 85 def initialize(_id: nil, questionnaire_id: nil, questionnaire_key: nil, raw_params: nil, value: nil, patient_id: nil, patient: nil, token: nil, active: true, test: false, created_at: nil, updated_at: nil, started_at: nil, completed_at: nil, outcome: nil, outcome_generated_at: nil, scores: nil, actions: nil, completion: nil, dsl_last_update: nil, import_notes: nil, flags: nil, textvars: nil) self._id = _id self.questionnaire_id = questionnaire_id self.questionnaire_key = questionnaire_key self.raw_params = raw_params || {} self.value = value || {} self.patient_id = patient_id self.patient = patient || {} self.token = token self.active = active self.test = test self.created_at = created_at self.updated_at = updated_at self.started_at = started_at self.completed_at = completed_at self.outcome_generated_at = outcome_generated_at self.scores = scores || {} self.actions = actions || {} self.completion = completion || {} self.dsl_last_update = dsl_last_update self.import_notes = import_notes || {} self.flags = flags self.textvars = textvars end |
Instance Attribute Details
#_id ⇒ String
15 16 17 |
# File 'lib/quby/answers/entities/answer.rb', line 15 def _id @_id end |
#aborted ⇒ Object
Returns the value of attribute aborted.
79 80 81 |
# File 'lib/quby/answers/entities/answer.rb', line 79 def aborted @aborted end |
#actions ⇒ Object
272 273 274 |
# File 'lib/quby/answers/entities/answer.rb', line 272 def actions outcome.actions end |
#active ⇒ Boolean
41 42 43 |
# File 'lib/quby/answers/entities/answer.rb', line 41 def active @active end |
#completed_at ⇒ Time
56 57 58 |
# File 'lib/quby/answers/entities/answer.rb', line 56 def completed_at @completed_at end |
#completion ⇒ Object
276 277 278 |
# File 'lib/quby/answers/entities/answer.rb', line 276 def completion outcome.completion end |
#created_at ⇒ Time
47 48 49 |
# File 'lib/quby/answers/entities/answer.rb', line 47 def created_at @created_at end |
#dsl_last_update ⇒ Time
62 63 64 |
# File 'lib/quby/answers/entities/answer.rb', line 62 def dsl_last_update @dsl_last_update end |
#extra_failed_validations ⇒ Object
Returns the value of attribute extra_failed_validations.
83 84 85 |
# File 'lib/quby/answers/entities/answer.rb', line 83 def extra_failed_validations @extra_failed_validations end |
#extra_question_values ⇒ Object
For setting raw content values and failed validations
82 83 84 |
# File 'lib/quby/answers/entities/answer.rb', line 82 def extra_question_values @extra_question_values end |
#flags ⇒ Hash<String, Boolean>
69 70 71 |
# File 'lib/quby/answers/entities/answer.rb', line 69 def flags @flags end |
#import_notes ⇒ Hash
For answers that are imported from external sources
66 67 68 |
# File 'lib/quby/answers/entities/answer.rb', line 66 def import_notes @import_notes end |
#outcome ⇒ Outcome
59 60 61 |
# File 'lib/quby/answers/entities/answer.rb', line 59 def outcome @outcome end |
#outcome_generated_at ⇒ Object
Returns the value of attribute outcome_generated_at.
74 75 76 |
# File 'lib/quby/answers/entities/answer.rb', line 74 def outcome_generated_at @outcome_generated_at end |
#patient ⇒ Hash
35 36 37 |
# File 'lib/quby/answers/entities/answer.rb', line 35 def patient @patient end |
#patient_id ⇒ String
32 33 34 |
# File 'lib/quby/answers/entities/answer.rb', line 32 def patient_id @patient_id end |
#questionnaire_id ⇒ String
18 19 20 |
# File 'lib/quby/answers/entities/answer.rb', line 18 def questionnaire_id @questionnaire_id end |
#questionnaire_key ⇒ String
21 22 23 |
# File 'lib/quby/answers/entities/answer.rb', line 21 def questionnaire_key @questionnaire_key end |
#raw_params ⇒ Hash
The raw form data (for recovery purposes)
25 26 27 |
# File 'lib/quby/answers/entities/answer.rb', line 25 def raw_params @raw_params end |
#scores ⇒ Object
268 269 270 |
# File 'lib/quby/answers/entities/answer.rb', line 268 def scores outcome.scores end |
#started_at ⇒ Time
53 54 55 |
# File 'lib/quby/answers/entities/answer.rb', line 53 def started_at @started_at end |
#test ⇒ Boolean
44 45 46 |
# File 'lib/quby/answers/entities/answer.rb', line 44 def test @test end |
#textvars ⇒ Hash<String, String>
72 73 74 |
# File 'lib/quby/answers/entities/answer.rb', line 72 def textvars @textvars end |
#token ⇒ String
38 39 40 |
# File 'lib/quby/answers/entities/answer.rb', line 38 def token @token end |
#updated_at ⇒ Time
50 51 52 |
# File 'lib/quby/answers/entities/answer.rb', line 50 def updated_at @updated_at end |
#value ⇒ Hash
The filtered and transformed form data
29 30 31 |
# File 'lib/quby/answers/entities/answer.rb', line 29 def value @value end |
Instance Method Details
#action ⇒ Object
280 281 282 |
# File 'lib/quby/answers/entities/answer.rb', line 280 def action outcome.action end |
#all_blank? ⇒ Boolean
307 308 309 310 311 312 |
# File 'lib/quby/answers/entities/answer.rb', line 307 def all_blank? questionnaire.questions.reduce(true) do |all_blank, question| next all_blank unless question all_blank and send(question.key).blank? end end |
#as_json(options = {}) ⇒ Object
294 295 296 297 298 299 300 301 |
# File 'lib/quby/answers/entities/answer.rb', line 294 def as_json( = {}) attributes.merge( id: id, value_by_values: value_by_values, scores: scores, is_completed: self.completed? ? true : false ) end |
#attributes ⇒ Object
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 |
# File 'lib/quby/answers/entities/answer.rb', line 128 def attributes HashWithIndifferentAccess.new({ _id: _id, questionnaire_id: questionnaire_id, questionnaire_key: questionnaire_key, raw_params: raw_params, value: value, patient_id: patient_id, patient: patient, token: token, active: active, test: test, created_at: created_at, updated_at: updated_at, started_at: started_at, completed_at: completed_at, outcome_generated_at: outcome_generated_at, scores: scores, actions: actions, completion: completion, dsl_last_update: dsl_last_update, import_notes: import_notes, flags: flags, textvars: textvars }) end |
#completed? ⇒ Boolean
303 304 305 |
# File 'lib/quby/answers/entities/answer.rb', line 303 def completed? !all_blank? && valid? end |
#enhance_by_dsl ⇒ Object
175 176 177 |
# File 'lib/quby/answers/entities/answer.rb', line 175 def enhance_by_dsl DSL.enhance(self) end |
#errors ⇒ Object
155 156 157 |
# File 'lib/quby/answers/entities/answer.rb', line 155 def errors @errors ||= ActiveModel::Errors.new(self) end |
#id ⇒ Object
116 117 118 |
# File 'lib/quby/answers/entities/answer.rb', line 116 def id _id end |
#mark_completed(start_time) ⇒ Object
168 169 170 171 172 173 |
# File 'lib/quby/answers/entities/answer.rb', line 168 def mark_completed(start_time) if completed? || @aborted self.started_at = start_time if started_at.blank? self.completed_at = Time.now if completed_at.blank? end end |
#questionnaire ⇒ Object
Faux belongs_to :questionnaire
164 165 166 |
# File 'lib/quby/answers/entities/answer.rb', line 164 def questionnaire Quby.questionnaires.find(questionnaire_key) end |
#to_param ⇒ Object
124 125 126 |
# File 'lib/quby/answers/entities/answer.rb', line 124 def to_param id end |
#url_params(options = {}) ⇒ Object
314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/quby/answers/entities/answer.rb', line 314 def url_params( = {}) = Time.now.getgm.strftime("%Y-%m-%dT%H:%M:%S+00:00") plain_token = [Quby::Settings.shared_secret, token, ].join('|') # double slash removed from return_url (it's either this or removing the # final slash in Settings.application_url) .merge( display_mode: [:display_mode] || "paged", token: token, timestamp: , hmac: Digest::SHA1.hexdigest(plain_token) ) end |
#valid? ⇒ Boolean
159 160 161 |
# File 'lib/quby/answers/entities/answer.rb', line 159 def valid? errors.empty? end |
#value_by_regular_values ⇒ Object
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 |
# File 'lib/quby/answers/entities/answer.rb', line 230 def value_by_regular_values result = {} if value result = value.dup value.each do |key, answer| question = questionnaire.questions.find { |q| q&.key.to_s == key.to_s } next unless question if question.type == :radio || question.type == :scale || question.type == :select option = question..find { |o| o.key.to_s == value[key].to_s } result[key] = option.value if option elsif question.type == :integer result[key] = answer.to_i if answer elsif question.type == :float result[key] = answer.to_f if answer end end end result rescue Exception => e if defined? Roqua::Support::Errors Roqua::Support::Errors.report e, root_path: Rails.root.to_s end raise e if Rails.env.development? || Rails.env.test? Rails.logger.error "RESCUED #{e.} \n #{e.backtrace.join('\n')}" {} end |
#value_by_values ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/quby/answers/entities/answer.rb', line 206 def value_by_values result = {} if value result = value.dup value.each_key do |key| question = questionnaire.questions.find { |q| q&.key.to_s == key.to_s } if question and (question.type == :radio || question.type == :scale || question.type == :select) option = question..find { |o| o.key.to_s == value[key].to_s } if option result[key] = option.value.to_s end end end end result rescue Exception => e if defined? Roqua::Support::Errors Roqua::Support::Errors.report e, root_path: Rails.root.to_s end raise e if Rails.env.development? || Rails.env.test? Rails.logger.error "RESCUED #{e.} \n #{e.backtrace.join('\n')}" {} end |