Class: Decidim::Forms::AnswerQuestionnaire

Inherits:
Command
  • Object
show all
Includes:
MultipleAttachmentsMethods
Defined in:
decidim-forms/app/commands/decidim/forms/answer_questionnaire.rb

Overview

This command is executed when the user answers a Questionnaire.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Command

call, #evaluate, #method_missing, #respond_to_missing?, #transaction, #with_events

Constructor Details

#initialize(form, current_user, questionnaire) ⇒ AnswerQuestionnaire

Initializes a AnswerQuestionnaire Command.

form - The form from which to get the data. questionnaire - The current instance of the questionnaire to be answered.



13
14
15
16
17
# File 'decidim-forms/app/commands/decidim/forms/answer_questionnaire.rb', line 13

def initialize(form, current_user, questionnaire)
  @form = form
  @current_user = current_user
  @questionnaire = questionnaire
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Decidim::Command

Instance Attribute Details

#current_userObject (readonly)

Returns the value of attribute current_user.



37
38
39
# File 'decidim-forms/app/commands/decidim/forms/answer_questionnaire.rb', line 37

def current_user
  @current_user
end

#formObject (readonly)

Returns the value of attribute form.



37
38
39
# File 'decidim-forms/app/commands/decidim/forms/answer_questionnaire.rb', line 37

def form
  @form
end

#questionnaireObject (readonly)

Returns the value of attribute questionnaire.



37
38
39
# File 'decidim-forms/app/commands/decidim/forms/answer_questionnaire.rb', line 37

def questionnaire
  @questionnaire
end

Instance Method Details

#callObject

Answers a questionnaire if it is valid

Broadcasts :ok if successful, :invalid otherwise.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'decidim-forms/app/commands/decidim/forms/answer_questionnaire.rb', line 22

def call
  return broadcast(:invalid) if @form.invalid? || user_already_answered?

  with_events do
    answer_questionnaire
  end

  if @errors
    reset_form_attachments
    broadcast(:invalid)
  else
    broadcast(:ok)
  end
end