Module: Surveyor::Models::DependencyConditionMethods

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::ForbiddenAttributesProtection, ActiveModel::Validations, ActsAsResponse
Included in:
DependencyCondition
Defined in:
lib/surveyor/models/dependency_condition_methods.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Methods included from ActsAsResponse

#as

Instance Method Details

#to_hash(response_set) ⇒ Object

Instance methods



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/surveyor/models/dependency_condition_methods.rb', line 30

def to_hash(response_set)
  # all responses to associated question
  responses = question.blank? ? [] : response_set.responses.where("responses.answer_id in (?)", question.answer_ids)
  if self.operator.match /^count(>|>=|<|<=|==|!=)\d+$/
    op, i = self.operator.scan(/^count(>|>=|<|<=|==|!=)(\d+)$/).flatten
    # logger.warn({rule_key.to_sym => responses.count.send(op, i.to_i)})
    return {rule_key.to_sym => (op == "!=" ? !responses.count.send("==", i.to_i) : responses.count.send(op, i.to_i))}
  elsif operator == "!=" and (responses.blank? or responses.none?{|r| r.answer.id == self.answer.id})
    # logger.warn( {rule_key.to_sym => true})
    return {rule_key.to_sym => true}
  elsif response = responses.to_a.detect{|r| r.answer.id == self.answer.id}
    klass = response.answer.response_class
    klass = "answer" if self.as(klass).nil? # it should compare answer ids when the dependency condition *_value is nil
    case self.operator
    when "==", "<", ">", "<=", ">="
      # logger.warn( {rule_key.to_sym => response.as(klass).send(self.operator, self.as(klass))})
      return {rule_key.to_sym => !response.as(klass).nil? && response.as(klass).send(self.operator, self.as(klass))}
    when "!="
      # logger.warn( {rule_key.to_sym => !response.as(klass).send("==", self.as(klass))})
      return {rule_key.to_sym => !response.as(klass).send("==", self.as(klass))}
    end
  end
  # logger.warn({rule_key.to_sym => false})
  {rule_key.to_sym => false}
end