Sha256: 5de7c734250780aed86bbe05fcd1e9180faf3686cf83699c64e24446e455f941

Contents?: true

Size: 1.7 KB

Versions: 3

Compression:

Stored size: 1.7 KB

Contents

module Surveyor
  module Models
    module DependencyConditionMethods
      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
        base.send :validates_presence_of, :operator, :rule_key
        base.send :validates_inclusion_of, :operator, :in => Surveyor::Common::OPERATORS
        base.send :validates_uniqueness_of, :rule_key, :scope => :dependency_id
        # this causes issues with building and saving
        # base.send :validates_numericality_of, :question_id, :answer_id, :dependency_id
        
        base.send :include, Surveyor::ActsAsResponse # includes "as" instance method

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

      # Instance methods
      def to_hash(response_set)
        response = response_set.responses.detect{|r| r.answer_id.to_i == self.answer_id.to_i} || false # eval("nil and false") => nil so return false if no response is found
        {rule_key.to_sym => (response and self.is_met?(response))}
      end

      # Checks to see if the response passed in meets the dependency condition
      def is_met?(response)
        klass = response.answer.response_class
        return case self.operator
        when "==", "<", ">", "<=", ">="
          response.as(klass).send(self.operator, self.as(klass))
        when "!="
          !(response.as(klass) == self.as(klass))
        else
          false
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
surveyor-0.16.1 lib/surveyor/models/dependency_condition_methods.rb
surveyor-0.16.0 lib/surveyor/models/dependency_condition_methods.rb
surveyor-0.15.0 lib/surveyor/models/dependency_condition_methods.rb