Class: ValidationCondition

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/validation_condition.rb

Constant Summary collapse

OPERATORS =

Constants

%w(== != < > <= >= =~)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.operatorsObject

Class methods



23
24
25
# File 'app/models/validation_condition.rb', line 23

def self.operators
  OPERATORS
end

Instance Method Details

#is_valid?(response) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/validation_condition.rb', line 32

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

#to_hash(response) ⇒ Object

Instance Methods



28
29
30
# File 'app/models/validation_condition.rb', line 28

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