Class: Effective::Response

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

Instance Method Summary collapse

Instance Method Details

#category_partialObject



102
103
104
# File 'app/models/effective/response.rb', line 102

def category_partial
  question&.category_partial
end

#completed?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'app/models/effective/response.rb', line 106

def completed?
  responsable.responsable_completed?
end

#correct?Boolean

Returns:

  • (Boolean)


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
# File 'app/models/effective/response.rb', line 110

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

#responseObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/effective/response.rb', line 82

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 response_options.first if question.choose_one?
  return response_options.first if question.select_up_to_1?
  return response_options if question.question_option?

  raise('unknown response for unexpected question category')
end

#to_sObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/models/effective/response.rb', line 61

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