skill-assessment-app/app/validators/answer_format_validator.rb

48 lines
1.4 KiB
Ruby
Raw Normal View History

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-08-01 16:27:17 -05:00
clean_val = value.to_s.strip
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
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-01 16:27:17 -05:00
return unless value.nil? || value.values.join.blank?
2016-08-01 09:50:01 -05:00
record.errors[attribute] << (options[:message] || live_code_error_message(value))
end
#################################
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
"You must write comments or code in one of the textareas to progress."
2016-08-01 09:50:01 -05:00
end
end