Class: Decidim::Elections::Admin::UpdateQuestions

Inherits:
Command
  • Object
show all
Defined in:
app/commands/decidim/elections/admin/update_questions.rb

Instance Method Summary collapse

Constructor Details

#initialize(form, election) ⇒ UpdateQuestions

Returns a new instance of UpdateQuestions.



7
8
9
10
# File 'app/commands/decidim/elections/admin/update_questions.rb', line 7

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

Instance Method Details

#callObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/commands/decidim/elections/admin/update_questions.rb', line 12

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

  current_position = @election.questions.count || 0
  @form.questions.each_with_index do |question_form, index|
    if question_form.deleted?
      delete_question(question_form)
    else
      update_question(question_form, index + current_position)
    end
  end

  broadcast(:ok)
end