Module: Surveyor::Models::ValidationConditionMethods
- Included in:
- ValidationCondition
- Defined in:
- lib/surveyor/models/validation_condition_methods.rb
Class Method Summary collapse
Instance Method Summary collapse
- #is_valid?(response) ⇒ Boolean
-
#to_hash(response) ⇒ Object
Instance Methods.
Class Method Details
.included(base) ⇒ Object
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 32 |
# File 'lib/surveyor/models/validation_condition_methods.rb', line 4 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 Method Details
#is_valid?(response) ⇒ Boolean
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/surveyor/models/validation_condition_methods.rb', line 39 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
35 36 37 |
# File 'lib/surveyor/models/validation_condition_methods.rb', line 35 def to_hash(response) {rule_key.to_sym => (response.nil? ? false : self.is_valid?(response))} end |