Sha256: fc55b7a3a89a7c9981e559bbd2483e7339f9e1cb07f2cdfde198dd3e463fbfe6

Contents?: true

Size: 1.16 KB

Versions: 6

Compression:

Stored size: 1.16 KB

Contents

class ValidationCondition < ActiveRecord::Base

  # Constants
  OPERATORS = %w(== != < > <= >= =~)

  # Associations
  belongs_to :validation
  
  # Scopes
  
  # Validations
  validates_numericality_of :validation_id #, :question_id, :answer_id
  validates_presence_of :operator, :rule_key
  validates_inclusion_of :operator, :in => OPERATORS
  validates_uniqueness_of :rule_key, :scope => :validation_id
  
  acts_as_response # includes "as" instance method
  
  # Class methods
  def self.operators
    OPERATORS
  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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
surveyor-0.14.4 app/models/validation_condition.rb
surveyor-0.14.3 app/models/validation_condition.rb
surveyor-0.14.2 app/models/validation_condition.rb
surveyor-0.14.1 app/models/validation_condition.rb
surveyor-0.14.0 app/models/validation_condition.rb
surveyor-0.13.0 app/models/validation_condition.rb