Class: Decidim::Votings::Admin::UpdateBallotStyle

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

Overview

A command with the business logic to update the ballot style

Instance Method Summary collapse

Constructor Details

#initialize(form, ballot_style) ⇒ UpdateBallotStyle

Returns a new instance of UpdateBallotStyle.



8
9
10
11
# File 'app/commands/decidim/votings/admin/update_ballot_style.rb', line 8

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

Instance Method Details

#callObject

Executes the command. Broadcast this events:

  • :ok when everything is valid

  • :invalid when the form wasn’t valid and couldn’t proceed

Returns nothing.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/commands/decidim/votings/admin/update_ballot_style.rb', line 18

def call
  return broadcast(:invalid) unless form.valid?

  begin
    update_ballot_style!
  rescue ActiveRecord::RecordNotUnique
    form.errors.add(:code, :taken)
    return broadcast(:invalid)
  end

  transaction do
    destroy_removed_ballot_style_questions!
    create_added_ballot_style_questions!
  end

  broadcast(:ok)
end