Class: Decidim::Initiatives::VoteInitiative

Inherits:
Rectify::Command
  • Object
show all
Defined in:
decidim-initiatives/app/commands/decidim/initiatives/vote_initiative.rb

Overview

A command with all the business logic when a user or organization votes an initiative.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(form) ⇒ VoteInitiative

Public: Initializes the command.

form - A form object with the params.


10
11
12
# File 'decidim-initiatives/app/commands/decidim/initiatives/vote_initiative.rb', line 10

def initialize(form)
  @form = form
end

Instance Attribute Details

#votesObject (readonly)

Returns the value of attribute votes


38
39
40
# File 'decidim-initiatives/app/commands/decidim/initiatives/vote_initiative.rb', line 38

def votes
  @votes
end

Instance Method Details

#callObject

Executes the command. Broadcasts these events:

  • :ok when everything is valid, together with the proposal vote.

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

Returns nothing.


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'decidim-initiatives/app/commands/decidim/initiatives/vote_initiative.rb', line 20

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

  percentage_before = initiative.percentage

  Initiative.transaction do
    create_votes
  end

  percentage_after = initiative.reload.percentage

  send_notification
  notify_percentage_change(percentage_before, percentage_after)
  notify_support_threshold_reached(percentage_before, percentage_after)

  broadcast(:ok, votes)
end