module Quby::Answers::Services::AnswerValidations

Public Instance Methods

calculated_attributes() click to toggle source
# File lib/quby/answers/services/answer_validations.rb, line 31
def calculated_attributes
  @calculated_attributes ||= AttributeCalculator.new(questionnaire, self)
end
cleanup_input() click to toggle source
# File lib/quby/answers/services/answer_validations.rb, line 11
def cleanup_input
  questionnaire.questions.each do |question|
    next unless question
    next if question.hidden?

    answer = send(question.key)
    if answer && clear?(answer, question)
      clear_question(question)
    elsif answer && question.type == :textarea
      send("#{question.key}=", answer.gsub("\r\n", "\n"))
    end
  end
end
clear?(answer, question) click to toggle source
# File lib/quby/answers/services/answer_validations.rb, line 64
def clear?(answer, question)
  # rubocop:disable LineLength
  return true if question.is_a?(Questionnaires::Entities::Questions::SelectQuestion)  && answer == question.extra_data[:placeholder].to_s
  return true if question.is_a?(Questionnaires::Entities::Questions::StringQuestion)  && answer == ""
  return true if question.is_a?(Questionnaires::Entities::Questions::TextQuestion)    && answer == ""
  return true if question.is_a?(Questionnaires::Entities::Questions::IntegerQuestion) && answer == ""
  return true if question.is_a?(Questionnaires::Entities::Questions::FloatQuestion)   && answer == ""
  return true if parent_option_is_not_selected(question)
  return true if hidden_questions&.include?(question.key)
  false
end
clear_question(question) click to toggle source
# File lib/quby/answers/services/answer_validations.rb, line 25
def clear_question(question)
  question.answer_keys.each do |key|
    value[key.to_s] = nil
  end
end
depends_on_lookup() click to toggle source
# File lib/quby/answers/services/answer_validations.rb, line 47
def depends_on_lookup
  calculated_attributes.depends_on_lookup
end
hidden_questions() click to toggle source
# File lib/quby/answers/services/answer_validations.rb, line 35
def hidden_questions
  calculated_attributes.hidden
end
parent_option_is_not_selected(question) click to toggle source
# File lib/quby/answers/services/answer_validations.rb, line 51
def parent_option_is_not_selected(question)
  return false unless question.parent and question.parent_option_key

  case question.parent.type
  when :radio
    value[question.parent.key.to_s] != question.parent_option_key.to_s
  when :check_box
    value[question.parent.key.to_s]&.fetch(question.parent_option_key.to_s, nil) != 1
  else
    false
  end
end
question_groups() click to toggle source
# File lib/quby/answers/services/answer_validations.rb, line 43
def question_groups
  calculated_attributes.groups
end
shown_questions() click to toggle source
# File lib/quby/answers/services/answer_validations.rb, line 39
def shown_questions
  calculated_attributes.shown
end
skip_validation?(answer, question) click to toggle source
# File lib/quby/answers/services/answer_validations.rb, line 76
def skip_validation?(answer, question)
  return true if parent_option_is_not_selected(question)
  return true if hidden_questions&.include?(question.key)
  false
end
validate_answers() click to toggle source
# File lib/quby/answers/services/answer_validations.rb, line 82
def validate_answers
  AnswerValidator.new(questionnaire, self).validate
end