Class: Rapidfire::Question

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.inherited(child) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'app/models/rapidfire/question.rb', line 15

def self.inherited(child)
  child.instance_eval do
    def model_name
      Question.model_name
    end
  end

  super
end

Instance Method Details

#rulesObject



25
26
27
# File 'app/models/rapidfire/question.rb', line 25

def rules
  validation_rules || {}
end

#validate_answer(answer) ⇒ Object

answer will delegate its validation to question, and question will inturn add validations on answer on the fly!



31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/rapidfire/question.rb', line 31

def validate_answer(answer)
  if rules[:presence] == "1"
    answer.validates_presence_of :answer_text
  end

  if rules[:minimum].present? || rules[:maximum].present?
    min_max = { minimum: rules[:minimum].to_i }
    min_max[:maximum] = rules[:maximum].to_i if rules[:maximum].present?

    answer.validates_length_of :answer_text, min_max
  end
end