Class: Decidim::Votings::Admin::UpdateVoting

Inherits:
Rectify::Command
  • Object
show all
Defined in:
decidim-elections/app/commands/decidim/votings/admin/update_voting.rb

Overview

A command with all the business logic when updating an existing voting in the system.

Instance Method Summary collapse

Constructor Details

#initialize(voting, form) ⇒ UpdateVoting

Public: Initializes the command.

voting - the Voting to update form - A form object with the params.


13
14
15
16
17
18
19
# File 'decidim-elections/app/commands/decidim/votings/admin/update_voting.rb', line 13

def initialize(voting, form)
  image_fields.each do |field|
    form.send("#{field}=".to_sym, voting.send(field)) if form.send(field).blank?
  end
  @voting = voting
  @form = form
end

Instance Method Details

#callObject

Executes the command. Broadcasts these events:

  • :ok when everything is valid.

  • :invalid if the form wasn't valid and we couldn't proceed.

Returns nothing.


27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'decidim-elections/app/commands/decidim/votings/admin/update_voting.rb', line 27

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

  update_voting!

  if voting.valid?
    broadcast(:ok, voting)
  else
    image_fields.each do |field|
      form.errors.add(field, voting.errors[field]) if voting.errors.include? field
    end
    broadcast(:invalid)
  end
end