From 35a20c346db7b96a4ac23b4367003c1901451f22 Mon Sep 17 00:00:00 2001 From: Jorge Santos Date: Mon, 27 Jun 2022 14:12:02 +0900 Subject: [PATCH 1/2] [Feature] Properly parse Validation Conditions Validation Conditions were created not to only validate the format of a specific response, but also to validate it based on a question/answer combination. Currently the parser was not mapping out the question/answer references, so we just replicated what was being done with the DependencyConditons. So now we'll be able to create specific validations like the following: ``` q_3 'Number of other people in the vehicle:', display_type: 'number_input', common_identifier: 'car_travel_party_size', is_mandatory: true a_0 ' ' q_4 'How many household members were with you?', display_type: 'number_input', common_identifier: 'hh_no_members', is_mandatory: true dependency rule: 'A' condition_A :q_3, '>', { text_value: "0", answer_reference: "0" } a_0 ' ' validation rule: 'A' condition_A "<=", { question_reference: "3", answer_reference: "0" } ``` Where the second question will include a validation condition, where its value should be bigger or equal to the first question response value. --- .../models/validation_condition_methods.rb | 4 +++- lib/surveyor/parser.rb | 22 +++++++++++++++++++ lib/surveyor/permitted_params.rb | 2 +- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/surveyor/models/validation_condition_methods.rb b/lib/surveyor/models/validation_condition_methods.rb index 3198e0d..1b8fe54 100644 --- a/lib/surveyor/models/validation_condition_methods.rb +++ b/lib/surveyor/models/validation_condition_methods.rb @@ -11,7 +11,9 @@ module ValidationConditionMethods included do # Associations belongs_to :validation - + belongs_to :question + belongs_to :answer + if defined?(::ProtectedAttributes) attr_accessible(*PermittedParams.new.validation_condition_attributes) end diff --git a/lib/surveyor/parser.rb b/lib/surveyor/parser.rb index 696ebcf..1eaaa40 100644 --- a/lib/surveyor/parser.rb +++ b/lib/surveyor/parser.rb @@ -95,6 +95,7 @@ def method_missing(missing_method, *args, &block) resolve_question_correct_answers report_lost_and_duplicate_references report_missing_default_locale + resolve_validation_condition_references Surveyor::Parser.rake_trace("", -2) if context[:survey].save Surveyor::Parser.rake_trace "Survey saved." @@ -189,6 +190,20 @@ def resolve_dependency_condition_references end end end + + def resolve_validation_condition_references + self.context[:validation_conditions].each do |vc| + if (vc.question = self.context[:question_references][vc.question_reference]).nil? + self.context[:bad_references].push "q_#{vc.question_reference}" + end + + self.context[:answer_references][vc.question_reference] ||= {} + + if !vc.answer_reference.blank? and (vc.answer = self.context[:answer_references][vc.question_reference][vc.answer_reference]).nil? + self.context[:bad_references].push "q_#{vc.question_reference}, a_#{vc.answer_reference}" + end + end + end end end @@ -219,6 +234,7 @@ def clear(context) :bad_references => [], :duplicate_references => [], :dependency_conditions => [], + :validation_conditions => [], :questions_with_correct_answers => {}, :default_mandatory => false }) @@ -506,6 +522,10 @@ def parse_and_build(context, args, original_method, reference_identifier) # ValidationCondition model module SurveyorParserValidationConditionMethods + Surveyor::ValidationCondition.instance_eval do + attr_accessor :question_reference, :answer_reference + end + def parse_and_build(context, args, original_method, reference_identifier) # clear context context.delete :validation_condition @@ -516,6 +536,8 @@ def parse_and_build(context, args, original_method, reference_identifier) operator: a0 || "==", rule_key: reference_identifier }.merge(a1 || {})).validation_condition + context[:validation].validation_conditions << context[:validation_condition] = self + context[:validation_conditions] << self end end diff --git a/lib/surveyor/permitted_params.rb b/lib/surveyor/permitted_params.rb index ababd7d..a088c60 100644 --- a/lib/surveyor/permitted_params.rb +++ b/lib/surveyor/permitted_params.rb @@ -82,7 +82,7 @@ def validation_condition strong_parameters.permit(*validation_condition_attributes) end def validation_condition_attributes - [:validation, :validation_id, :rule_key, :operator, :question_id, :answer_id, :datetime_value, :integer_value, :float_value, :unit, :text_value, :string_value, :response_other, :regexp] + [:validation, :validation_id, :rule_key, :operator, :question, :answer, :question_id, :answer_id, :datetime_value, :integer_value, :float_value, :unit, :text_value, :string_value, :response_other, :regexp, :question_reference, :answer_reference] end # response From f90d17eec463f42fc504ac669897164d0da4dd91 Mon Sep 17 00:00:00 2001 From: Jorge Santos Date: Mon, 27 Jun 2022 14:16:40 +0900 Subject: [PATCH 2/2] Bump version to 2.1.2 --- lib/surveyor/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/surveyor/version.rb b/lib/surveyor/version.rb index dd5e60e..3795ca3 100644 --- a/lib/surveyor/version.rb +++ b/lib/surveyor/version.rb @@ -1,3 +1,3 @@ module Surveyor - VERSION = "2.1.1" + VERSION = "2.1.2" end