skill-assessment-app/app/validators/answer_format_validator.rb
Mark Moser fc784ffcb1 completes #17 - live-coder plus text input type
Squashed commit of the following:

commit d41cbf66eb2a9ee705ab60bb156eed95881fa193
Author: Mark Moser <mark.moser@perficient.com>
Date:   Thu Aug 4 19:58:20 2016 -0500

    live-coder-text validations

commit 866bfeb863516a8656bc26b10f967d0b9b8d8505
Author: Mark Moser <mark.moser@perficient.com>
Date:   Thu Aug 4 11:57:57 2016 -0500

    getting things green again and rebasing develop

commit 28a23200f291e30c690b71e9bcae3e1a69eb3093
Author: Derek Montgomery <montgomerygraphics@gmail.com>
Date:   Thu Aug 4 10:14:23 2016 -0500

    Progress on live coder text field
2016-08-04 20:01:17 -05:00

73 lines
2.5 KiB
Ruby

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
clean_val = value.to_s.strip
return if clean_val.length.between?(1, 1000)
if clean_val.blank?
record.errors[attribute] << (options[:message] || "Please enter an answer.")
end
if clean_val.length > 1000
record.errors[attribute] << (options[:message] || "The character limit for this answer is 1000.")
end
end
def radio record, attribute, value
return unless value.to_s.strip.blank?
record.errors[attribute] << (options[:message] || "Please select an answer.")
end
def checkbox record, attribute, value
return unless value.nil? || Array(value).join.blank?
record.errors[attribute] << (options[:message] || "Please select an answer.")
end
def live_code record, attribute, value
return unless value.nil? || value.values.join.blank?
record.errors[attribute] << (options[:message] || live_code_error_message(value))
end
def live_code_text record, attribute, value
return if value.present? && live_code_text_value_check(value)
record.errors[attribute] << (options[:message] || live_code_text_error_message(value))
end
#################################
def live_code_text_error_message value
return 'You must write code in one of the above textareas to progress.' if value.nil?
code_present = value.values_at(:html, :css, :js).join.present?
reason_present = value[:text].present?
later_present = value.keys.count == 1
return 'Please check that you will come back to complete the code example.' if later_present
return 'You must write code in one of the above textareas to progress.' unless code_present
return 'You must provide an answer in the reason field.' unless reason_present
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 code in one of the above textareas to progress."
end
def live_code_text_value_check value
later_present = value[:later].present? && value.keys.count == 1
code_present = value.values_at(:html, :css, :js).join.present?
reason_present = value[:text].present?
later_present || (code_present && reason_present)
end
end