Class: Rapidfire::Question

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.inherited(child) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'app/models/rapidfire/question.rb', line 12

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

  super
end

Instance Method Details

#rulesObject



22
23
24
# File 'app/models/rapidfire/question.rb', line 22

def rules
  validation_rules.present? ? validation_rules.symbolize_keys : {}
end

#type_can_changeObject



64
65
66
67
68
# File 'app/models/rapidfire/question.rb', line 64

def type_can_change
  if type_changed? && answers.any?
    errors.add(:type, "cannot change after answers have been added")
  end
end

#validate_answer(answer) ⇒ Object

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/rapidfire/question.rb', line 32

def validate_answer(answer)
  if rules[:presence] == "1"
    case self
    when Rapidfire::Questions::File
      if Rails::VERSION::MAJOR >= 6
        answer.validates_presence_of :file
      else
        if !answer.file.attached?
          answer.errors.add(:file, :blank)
        end
      end
    when Rapidfire::Questions::MultiFile
      if Rails::VERSION::MAJOR >= 6
        answer.validates_presence_of :files
      else
        if !answer.files.attached?
          answer.errors.add(:files, :blank)
        end
      end
    else
      answer.validates_presence_of :answer_text
    end
  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

#validation_rules=(val) ⇒ Object



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

def validation_rules=(val)
  super(val.stringify_keys)
end