Module: Surveyor::Models::DependencyConditionMethods

Included in:
DependencyCondition
Defined in:
lib/surveyor/models/dependency_condition_methods.rb

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) included(base)



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/surveyor/models/dependency_condition_methods.rb', line 4

def self.included(base)
  # Associations
  base.send :belongs_to, :answer
  base.send :belongs_to, :dependency
  base.send :belongs_to, :dependent_question, :foreign_key => :question_id, :class_name => :question
  base.send :belongs_to, :question
  
  @@validations_already_included ||= nil
  unless @@validations_already_included
    # Validations
    base.send :validates_presence_of, :operator, :rule_key
    base.send :validate, :validates_operator
    base.send :validates_uniqueness_of, :rule_key, :scope => :dependency_id
    # this causes issues with building and saving
    # base.send :validates_numericality_of, :question_id, :dependency_id
    
    @@validations_already_included = true
  end
          
  base.send :include, Surveyor::ActsAsResponse # includes "as" instance method

  # Class methods
  base.instance_eval do
    def operators
      Surveyor::Common::OPERATORS
    end
  end
end

Instance Method Details

- (Boolean) is_met?(responses)

Checks to see if the responses passed in meet the dependency condition

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/surveyor/models/dependency_condition_methods.rb', line 43

def is_met?(responses)
  # response to associated answer if available, or first response
  response = if self.answer_id
               responses.detect do |r| 
                 r.answer == self.answer
               end 
             end || responses.first
  klass = response.answer.response_class
  klass = "answer" if self.as(klass).nil?
  return case self.operator
  when "==", "<", ">", "<=", ">="
    response.as(klass).send(self.operator, self.as(klass))
  when "!="
    !(response.as(klass) == self.as(klass))
  when /^count[<>=]{1,2}\d+$/
    op, i = self.operator.scan(/^count([<>!=]{1,2})(\d+)$/).flatten
    responses.count.send(op, i.to_i)
  when /^count!=\d+$/
    !(responses.count == self.operator.scan(/\d+/).first.to_i)
  else
    false
  end
end

- (Object) to_hash(response_set)

Instance methods



34
35
36
37
38
39
40
# File 'lib/surveyor/models/dependency_condition_methods.rb', line 34

def to_hash(response_set)
  # all responses to associated question
  responses = response_set.responses.select do |r| 
    question && question.answers.include?(r.answer)
  end
  {rule_key.to_sym => (!responses.empty? and self.is_met?(responses))}
end