Class: Effective::Response
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Effective::Response
- Defined in:
- app/models/effective/response.rb
Instance Method Summary collapse
- #category_partial ⇒ Object
- #completed? ⇒ Boolean
- #correct? ⇒ Boolean
- #log_changes? ⇒ Boolean
- #response ⇒ Object
- #to_s ⇒ Object
Instance Method Details
#category_partial ⇒ Object
104 105 106 |
# File 'app/models/effective/response.rb', line 104 def category_partial question&.category_partial end |
#completed? ⇒ Boolean
114 115 116 |
# File 'app/models/effective/response.rb', line 114 def completed? responsable.responsable_completed? end |
#correct? ⇒ Boolean
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 |
# File 'app/models/effective/response.rb', line 118 def correct? return unless question.present? && question.scored? return false unless response.present? if question.question_option? # For option-based questions, check if selected options match answer options answers = question.answer.map(&:id) selected = Array(response).map(&:question_option_id) if question.select_all_that_apply? return selected.sort == answers.sort else # For choose_one or select_up_to_X, all selected must be correct return selected.present? && (selected - answers).blank? end end # For non-option questions, check against question_answer answer = question.question_answer return false unless answer.present? case answer.operation when 'Equal to' if response.is_a?(String) && answer.answer.is_a?(String) response.downcase.strip == answer.answer.downcase.strip else response == answer.answer end when 'Within range' answer.answer.cover?(response) when 'Less than' response < answer.answer when 'Less than or equal to' response <= answer.answer when 'Greater than' response > answer.answer when 'Greater than or equal to' response >= answer.answer when 'Contains' response.to_s.downcase.include?(answer.answer.to_s.downcase) when 'Does not contain' !response.to_s.downcase.include?(answer.answer.to_s.downcase) else false end end |
#log_changes? ⇒ Boolean
108 109 110 111 112 |
# File 'app/models/effective/response.rb', line 108 def log_changes? return responsable.log_changes? if responsable.respond_to?(:log_changes?) true end |
#response ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'app/models/effective/response.rb', line 84 def response return nil unless question.present? return date if question.date? return email if question.email? return number if question.number? return percentage if question.percentage? return price if question.price? return decimal if question.decimal? return long_answer if question.long_answer? return short_answer if question.short_answer? return upload_file if question.upload_file? return .first if question.choose_one? return .first if question.select_up_to_1? return if question.question_option? raise('unknown response for unexpected question category') end |
#to_s ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'app/models/effective/response.rb', line 63 def to_s return '' unless question.present? return '' unless response.present? case question.category when 'Date' response.strftime('%Y-%m-%d') when 'Price' "$#{'%0.2f' % (response / 100.0)}" when 'Percentage' precision = (response % 1000).zero? ? 0 : 3 "#{format("%.#{precision}f", response.to_f / 1000)}%" when 'Upload File' response.filename.to_s when 'Select all that apply', 'Select up to 2', 'Select up to 3', 'Select up to 4', 'Select up to 5' Array(response).map(&:to_s).join(', ') else response.to_s end end |