Skip to content

Commit

Permalink
Merge pull request #3 from whitesmith/feature/properly_parse_validati…
Browse files Browse the repository at this point in the history
…on_conditions

[Feature] Properly parse Validation Conditions
  • Loading branch information
jsantos authored Jun 27, 2022
2 parents 38ff3b3 + f90d17e commit 2419451
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/surveyor/models/validation_condition_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions lib/surveyor/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -219,6 +234,7 @@ def clear(context)
:bad_references => [],
:duplicate_references => [],
:dependency_conditions => [],
:validation_conditions => [],
:questions_with_correct_answers => {},
:default_mandatory => false
})
Expand Down Expand Up @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion lib/surveyor/permitted_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/surveyor/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Surveyor
VERSION = "2.1.1"
VERSION = "2.1.2"
end

0 comments on commit 2419451

Please sign in to comment.