Class: Effective::Question

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

Constant Summary collapse

CATEGORIES =
[
  'Choose one',   # Radios
  'Select all that apply', # Checks
  'Select up to 1',
  'Select up to 2',
  'Select up to 3',
  'Select up to 4',
  'Select up to 5',
  'Date',         # Date Field
  'Decimal',      # Decimal Field
  'Email',        # Email Field
  'Number',       # Numeric Field
  'Percentage',   # Percentage Field
  'Price',        # Price Field
  'Long Answer',  # Text Area
  'Short Answer', # Text Field
  'Upload File'   # File field
]
WITH_OPTIONS_CATEGORIES =
[
  'Choose one',   # Radios
  'Select all that apply', # Checks
  'Select up to 1',
  'Select up to 2',
  'Select up to 3',
  'Select up to 4',
  'Select up to 5'
]
UNSUPPORTED_FOLLOW_UP_QUESTION_CATEGORIES =
['Upload File']

Instance Method Summary collapse

Instance Method Details

#answerObject



144
145
146
147
# File 'app/models/effective/question.rb', line 144

def answer
  return unless scored?
  question_option? ? answer_options : question_answer.try(:answer)
end

#answer_optionsObject



176
177
178
# File 'app/models/effective/question.rb', line 176

def answer_options
  question_options.reject(&:marked_for_destruction?).select(&:answer?)
end

#answer_to_sObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/models/effective/question.rb', line 149

def answer_to_s
  return '' unless scored?

  case category
    when 'Choose one' then "One of: #{answer_options.join(', ')}"
    when 'Select all that apply' then "All of: #{answer_options.join(', ')}"
    when 'Select up to 1' then "One of: #{answer_options.join(', ')}"
    when 'Select up to 2' then "Two of: #{answer_options.join(', ')}"
    when 'Select up to 3' then "Three of: #{answer_options.join(', ')}"
    when 'Select up to 4' then "Four of: #{answer_options.join(', ')}"
    when 'Select up to 5' then "Five of: #{answer_options.join(', ')}"
    else question_answer.try(:to_s)
  end
end

#category_partialObject



180
181
182
# File 'app/models/effective/question.rb', line 180

def category_partial
  category.to_s.parameterize.underscore
end

#question_answerObject



184
185
186
# File 'app/models/effective/question.rb', line 184

def question_answer
  question_answers.first || question_answers.build()
end

#question_option?Boolean

Returns:

  • (Boolean)


172
173
174
# File 'app/models/effective/question.rb', line 172

def question_option?
  WITH_OPTIONS_CATEGORIES.include?(category)
end

#show_if_attributeObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/models/effective/question.rb', line 127

def show_if_attribute
  return :question_option_ids if question_option?

  case category
  when 'Date' then :date
  when 'Decimal' then :decimal
  when 'Email' then :email
  when 'Number' then :number
  when 'Percentage' then :percentage
  when 'Price' then :price
  when 'Long Answer' then :long_answer
  when 'Short Answer' then :short_answer
  when 'Upload File' then :upload_file
  else :unknown
  end
end

#show_if_valueObject



164
165
166
# File 'app/models/effective/question.rb', line 164

def show_if_value
  question.try(:question_option?) ? question_option_id : follow_up_value
end

#show_if_value_to_sObject



168
169
170
# File 'app/models/effective/question.rb', line 168

def show_if_value_to_s
  (question.try(:question_option?) ? question_option : follow_up_value).to_s
end

#to_sObject



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

def to_s
  title.presence || model_name.human
end