Module: Quby::Answers::Services::AnswerValidations
- Defined in:
- lib/quby/answers/services/answer_validations.rb
Instance Method Summary collapse
- #calculated_attributes ⇒ Object
- #cleanup_input ⇒ Object
- #clear?(answer, question) ⇒ Boolean
- #clear_question(question) ⇒ Object
- #depends_on_lookup ⇒ Object
- #hidden_questions ⇒ Object
- #parent_option_is_not_selected(question) ⇒ Object
- #question_groups ⇒ Object
- #shown_questions ⇒ Object
- #validate_answers ⇒ Object
Instance Method Details
#calculated_attributes ⇒ Object
31 32 33 |
# File 'lib/quby/answers/services/answer_validations.rb', line 31 def calculated_attributes @calculated_attributes ||= AttributeCalculator.new(questionnaire, self) end |
#cleanup_input ⇒ Object
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
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_lookup ⇒ Object
47 48 49 |
# File 'lib/quby/answers/services/answer_validations.rb', line 47 def depends_on_lookup calculated_attributes.depends_on_lookup end |
#hidden_questions ⇒ Object
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_groups ⇒ Object
43 44 45 |
# File 'lib/quby/answers/services/answer_validations.rb', line 43 def question_groups calculated_attributes.groups end |
#shown_questions ⇒ Object
39 40 41 |
# File 'lib/quby/answers/services/answer_validations.rb', line 39 def shown_questions calculated_attributes.shown end |
#validate_answers ⇒ Object
76 77 78 |
# File 'lib/quby/answers/services/answer_validations.rb', line 76 def validate_answers AnswerValidator.new(questionnaire, self).validate end |