Class: Quby::Answers::Services::UpdatesAnswers

Inherits:
Object
  • Object
show all
Defined in:
lib/quby/answers/services/updates_answers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(answer) ⇒ UpdatesAnswers

Returns a new instance of UpdatesAnswers.



12
13
14
# File 'lib/quby/answers/services/updates_answers.rb', line 12

def initialize(answer)
  @answer = answer
end

Instance Attribute Details

#answerObject (readonly)

Returns the value of attribute answer.



10
11
12
# File 'lib/quby/answers/services/updates_answers.rb', line 10

def answer
  @answer
end

Instance Method Details

#on_failure(&block) ⇒ Object



58
59
60
# File 'lib/quby/answers/services/updates_answers.rb', line 58

def on_failure(&block)
  @failure_callback = block
end

#on_success(&block) ⇒ Object



54
55
56
# File 'lib/quby/answers/services/updates_answers.rb', line 54

def on_success(&block)
  @success_callback = block
end

#update(new_attributes = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/quby/answers/services/updates_answers.rb', line 16

def update(new_attributes = {})
  save_raw_params_on_error(new_attributes) do
    attribute_filter = FiltersAnswerValue.new(answer.questionnaire)
    attribute_filter.filter(new_attributes).each { |name, value| answer.send("#{name}=", value) }

    answer.extend AnswerValidations
    answer.cleanup_input
    answer.validate_answers

    if answer.errors.empty?
      if new_attributes["rendered_at"].present?
        started_at = Time.at(new_attributes["rendered_at"].to_i)
      else
        started_at = nil
      end
      answer.mark_completed(started_at)
      answer.outcome = OutcomeCalculation.new(answer).calculate
      Quby.answers.update!(answer)
      succeed!
    else
      if defined?(::Roqua::Support)
        # We know that Quby v1 does not run client-side validations when the "Toch opslaan"
        # button is clicked. So if the answer is aborted, this is likely due to the user
        # ignoring/overlooking error messages other than the requires-answer validations that the
        # "Toch opslaan" button is designed to bypass.
        #
        # Since we know this is a current shortcoming of Quby v1 that's not likely to be fixed,
        # for now there's no point in sending these client-server validation mismatches
        # to AppSignal.
        unless answer.aborted
          ::Roqua::Support::Errors.report(Quby::ValidationError.new(answer.errors.full_messages))
        end
      end
      fail!
    end
  end
end