Module: Quby::Answers::Services::AnswerValidations

Defined in:
lib/quby/answers/services/answer_validations.rb

Instance Method Summary collapse

Instance Method Details

#calculated_attributesObject



31
32
33
# File 'lib/quby/answers/services/answer_validations.rb', line 31

def calculated_attributes
  @calculated_attributes ||= AttributeCalculator.new(questionnaire, self)
end

#cleanup_inputObject



11
12
13
14
15
16
17
18
19
20
21
22
23
# 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) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
72
73
74
# 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) ⇒ Object



25
26
27
28
29
# 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_lookupObject



47
48
49
# File 'lib/quby/answers/services/answer_validations.rb', line 47

def depends_on_lookup
  calculated_attributes.depends_on_lookup
end

#hidden_questionsObject



35
36
37
# File 'lib/quby/answers/services/answer_validations.rb', line 35

def hidden_questions
  calculated_attributes.hidden
end

#parent_option_is_not_selected(question) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# 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_groupsObject



43
44
45
# File 'lib/quby/answers/services/answer_validations.rb', line 43

def question_groups
  calculated_attributes.groups
end

#shown_questionsObject



39
40
41
# File 'lib/quby/answers/services/answer_validations.rb', line 39

def shown_questions
  calculated_attributes.shown
end

#skip_validation?(answer, question) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
# 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_answersObject



82
83
84
# File 'lib/quby/answers/services/answer_validations.rb', line 82

def validate_answers
  AnswerValidator.new(questionnaire, self).validate
end