Class: Decidim::Challenges::SurveyChallenge

Inherits:
Decidim::Command
  • Object
show all
Defined in:
app/commands/decidim/challenges/survey_challenge.rb

Overview

This command is executed when the user answer a survey.

Instance Method Summary collapse

Constructor Details

#initialize(challenge, user, survey_form) ⇒ SurveyChallenge

Initializes a SurveyChallenge Command.

challenge - The current instance of the challenge to be answer the survey. user - The user answering the survey. survey_form - A form object with params; can be a questionnaire.



12
13
14
15
16
17
# File 'app/commands/decidim/challenges/survey_challenge.rb', line 12

def initialize(challenge, user, survey_form)
  super()
  @challenge = challenge
  @user = user
  @survey_form = survey_form
end

Instance Method Details

#callObject

Creates a challenge survey.

Broadcasts :ok if successful, :invalid otherwise.



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

def call
  return broadcast(:invalid) unless can_answer_survey?
  return broadcast(:invalid_form) unless survey_form.valid?

  challenge.with_lock do
    answer_questionnaire
    create_survey
  end

  broadcast(:ok)
rescue ActiveRecord::Rollback
  form.errors.add(:base, :invalid)
  broadcast(:invalid)
end