Sha256: 6124cffda87ea0e9f74a9ff4146641f73b7ad6ac73445bae3ecb56d62426eb38

Contents?: true

Size: 1.98 KB

Versions: 9

Compression:

Stored size: 1.98 KB

Contents

module Surveyor
  module Models
    module ValidationConditionMethods
      def self.included(base)
        # Associations
        base.send :belongs_to, :validation

        # Scopes
        @@validations_already_included ||= nil
        unless @@validations_already_included
          # 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 => :validation_id
          # this causes issues with building and saving
          # base.send :validates_numericality_of, :validation_id #, :question_id, :answer_id
          
          @@validations_already_included = true
        end
        
        base.send :include, Surveyor::ActsAsResponse # includes "as" instance method
        
        # Whitelisting attributes
        base.send :attr_accessible, :validation, :validation_id, :rule_key, :operator, :question_id, :answer_id, :datetime_value, :integer_value, :float_value, :unit, :text_value, :string_value, :response_other, :regexp
        
        # Class methods
        base.instance_eval do
          def operators
            Surveyor::Common::OPERATORS
          end
        end
      end

      # Instance Methods
      def to_hash(response)
        {rule_key.to_sym => (response.nil? ? false : self.is_valid?(response))}
      end

      def is_valid?(response)
        klass = response.answer.response_class
        compare_to = Response.find_by_question_id_and_answer_id(self.question_id, self.answer_id) || self
        case self.operator
        when "==", "<", ">", "<=", ">="
          response.as(klass).send(self.operator, compare_to.as(klass))
        when "!="
          !(response.as(klass) == compare_to.as(klass))
        when "=~"
          return false if compare_to != self
          !(response.as(klass).to_s =~ Regexp.new(self.regexp || "")).nil?
        else
          false
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
hssc_surveyor-1.4.3.pre lib/surveyor/models/validation_condition_methods.rb
hssc_surveyor-1.4.2.pre lib/surveyor/models/validation_condition_methods.rb
hssc_surveyor-1.4.1.pre lib/surveyor/models/validation_condition_methods.rb
surveyor-1.4.0 lib/surveyor/models/validation_condition_methods.rb
surveyor-1.3.0 lib/surveyor/models/validation_condition_methods.rb
surveyor-1.2.0 lib/surveyor/models/validation_condition_methods.rb
surveyor-1.1.0 lib/surveyor/models/validation_condition_methods.rb
surveyor-1.0.1 lib/surveyor/models/validation_condition_methods.rb
surveyor-1.0.0 lib/surveyor/models/validation_condition_methods.rb