2016-08-01 09:50:01 -05:00
|
|
|
class AnswerFormatValidator < ActiveModel::EachValidator
|
|
|
|
def validate_each(record, attribute, value)
|
|
|
|
send(record.question.input_type, record, attribute, value)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def text record, attribute, value
|
2016-09-01 10:37:49 -05:00
|
|
|
clean_val = value.to_s.strip.delete("\r")
|
2016-08-01 16:27:17 -05:00
|
|
|
return if clean_val.length.between?(1, 1000)
|
2016-08-01 09:50:01 -05:00
|
|
|
|
2016-08-01 16:27:17 -05:00
|
|
|
if clean_val.blank?
|
2016-08-01 09:50:01 -05:00
|
|
|
record.errors[attribute] << (options[:message] || "Please enter an answer.")
|
|
|
|
end
|
|
|
|
|
2016-08-01 16:27:17 -05:00
|
|
|
if clean_val.length > 1000
|
2016-08-04 20:01:17 -05:00
|
|
|
record.errors[attribute] << (options[:message] || "The character limit for this answer is 1000.")
|
2016-08-01 09:50:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def radio record, attribute, value
|
2016-08-01 16:27:17 -05:00
|
|
|
return unless value.to_s.strip.blank?
|
2016-08-01 09:50:01 -05:00
|
|
|
|
|
|
|
record.errors[attribute] << (options[:message] || "Please select an answer.")
|
|
|
|
end
|
|
|
|
|
|
|
|
def checkbox record, attribute, value
|
2016-08-01 16:27:17 -05:00
|
|
|
return unless value.nil? || Array(value).join.blank?
|
2016-08-01 09:50:01 -05:00
|
|
|
|
|
|
|
record.errors[attribute] << (options[:message] || "Please select an answer.")
|
|
|
|
end
|
|
|
|
|
|
|
|
def live_code record, attribute, value
|
2016-08-23 21:54:53 -05:00
|
|
|
seed = (Question.find_by(id: record.question_id) || Question.new).input_options
|
|
|
|
|
|
|
|
return unless value.nil? || value.values.join.blank? || !match_seed?(value, seed)
|
2016-08-01 09:50:01 -05:00
|
|
|
|
2016-08-04 20:01:17 -05:00
|
|
|
record.errors[attribute] << (options[:message] || live_code_error_message(value))
|
|
|
|
end
|
|
|
|
|
2016-08-31 16:59:25 -05:00
|
|
|
def with_other record, attribute, value
|
|
|
|
return if value.present? && with_other_check(value)
|
|
|
|
|
|
|
|
record.errors[attribute] << (options[:message] || "Please select or provide an answer.")
|
|
|
|
end
|
|
|
|
alias radio_other with_other
|
|
|
|
alias checkbox_other with_other
|
|
|
|
|
2016-08-04 20:01:17 -05:00
|
|
|
#################################
|
|
|
|
|
2016-08-31 16:59:25 -05:00
|
|
|
def with_other_check value
|
|
|
|
return false unless value.respond_to? :keys
|
|
|
|
return false if Array(value[:options]).join.blank?
|
|
|
|
return false if value[:options].include?('other') && value[:other].to_s.blank?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2016-08-04 20:01:17 -05:00
|
|
|
def live_code_error_message value
|
|
|
|
if value.present? && value.keys.count == 1
|
|
|
|
return "Please check that you will come back to complete the code example."
|
|
|
|
end
|
2016-08-11 17:44:30 -05:00
|
|
|
"You must write comments or code in one of the textareas to progress."
|
2016-08-01 09:50:01 -05:00
|
|
|
end
|
2016-08-23 21:54:53 -05:00
|
|
|
|
|
|
|
def match_seed? value, seed
|
|
|
|
s_value = value.stringify_keys
|
|
|
|
s_seed = seed.stringify_keys
|
|
|
|
keys = s_value.merge(s_seed).keys.uniq
|
|
|
|
|
|
|
|
matches = keys.inject([]) do |memo, k|
|
|
|
|
memo << (s_value[k].to_s.strip == s_seed[k].to_s.strip)
|
|
|
|
end
|
|
|
|
|
|
|
|
matches.include? false
|
|
|
|
end
|
2016-08-01 09:50:01 -05:00
|
|
|
end
|