Class: Decidim::Surveys::Admin::CreateSurvey

Inherits:
Command
  • Object
show all
Defined in:
decidim-surveys/app/commands/decidim/surveys/admin/create_survey.rb

Overview

Command that gets called whenever a component’s survey has to be created. It usually happens as a callback when the component itself is created.

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(component, form) ⇒ CreateSurvey

Returns a new instance of CreateSurvey.



9
10
11
12
# File 'decidim-surveys/app/commands/decidim/surveys/admin/create_survey.rb', line 9

def initialize(component, form)
  @component = component
  @form = form
end

Dynamic Method Handling

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

Instance Attribute Details

#formObject (readonly)

Returns the value of attribute form.



14
15
16
# File 'decidim-surveys/app/commands/decidim/surveys/admin/create_survey.rb', line 14

def form
  @form
end

Instance Method Details

#callObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'decidim-surveys/app/commands/decidim/surveys/admin/create_survey.rb', line 16

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

  @survey = Survey.new(
    component: @component,
    questionnaire: Decidim::Forms::Questionnaire.new(
      title: form.title,
      description: form.description,
      tos: form.tos
    )
  )

  @survey.save ? broadcast(:ok, @survey) : broadcast(:invalid)
end