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



48
49
50
# File 'lib/quby/answers/services/updates_answers.rb', line 48

def on_failure(&block)
  @failure_callback = block
end

#on_success(&block) ⇒ Object



44
45
46
# File 'lib/quby/answers/services/updates_answers.rb', line 44

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
# 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
        ::Roqua::Support::Errors.report(Quby::ValidationError.new(answer.errors.full_messages))
      end
      fail!
    end
  end
end