Class: Decidim::Initiatives::VoteInitiative

Inherits:
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

Methods inherited from Command

call, #evaluate, #method_missing, #respond_to_missing?, #transaction, #with_events

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

Dynamic Method Handling

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

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 was not valid and we could not 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