Class: Decidim::Forms::ResponseQuestionnaire

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

Overview

This command is executed when the user responds 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, questionnaire, allow_editing_responses: false) ⇒ ResponseQuestionnaire

Initializes a ResponseQuestionnaire Command.

form - The form from which to get the data. questionnaire - The current instance of the questionnaire to be responded. allow_editing_responses - Flag that ensures a form can or cannot be editable after the questionnaire’s responses have been provided.



15
16
17
18
19
# File 'decidim-forms/app/commands/decidim/forms/response_questionnaire.rb', line 15

def initialize(form, questionnaire, allow_editing_responses: false)
  @form = form
  @questionnaire = questionnaire
  @allow_editing_responses = allow_editing_responses
end

Dynamic Method Handling

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

Instance Attribute Details

#allow_editing_responsesObject (readonly)

Returns the value of attribute allow_editing_responses.



40
41
42
# File 'decidim-forms/app/commands/decidim/forms/response_questionnaire.rb', line 40

def allow_editing_responses
  @allow_editing_responses
end

#formObject (readonly)

Returns the value of attribute form.



40
41
42
# File 'decidim-forms/app/commands/decidim/forms/response_questionnaire.rb', line 40

def form
  @form
end

#questionnaireObject (readonly)

Returns the value of attribute questionnaire.



40
41
42
# File 'decidim-forms/app/commands/decidim/forms/response_questionnaire.rb', line 40

def questionnaire
  @questionnaire
end

Instance Method Details

#callObject

Responds a questionnaire if it is valid

Broadcasts :ok if successful, :invalid otherwise.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'decidim-forms/app/commands/decidim/forms/response_questionnaire.rb', line 24

def call
  return broadcast(:invalid) if @form.invalid? || (user_already_responded? && !allow_editing_responses)

  with_events do
    clear_responses! if allow_editing_responses
    response_questionnaire
  end

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