Class: Decidim::Initiatives::VoteInitiative

Inherits:
Rectify::Command
  • Object
show all
Defined in:
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(initiative, current_user, group_id) ⇒ VoteInitiative

Public: Initializes the command.

initiative - A Decidim::Initiative object. current_user - The current user. group_id - Decidim user group id



12
13
14
15
16
# File 'app/commands/decidim/initiatives/vote_initiative.rb', line 12

def initialize(initiative, current_user, group_id)
  @initiative = initiative
  @current_user = current_user
  @decidim_user_group_id = group_id
end

Instance Attribute Details

#voteObject (readonly)

Returns the value of attribute vote.



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

def vote
  @vote
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.



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

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

  percentage_before = @initiative.percentage
  vote.save!
  send_notification
  percentage_after = @initiative.reload.percentage

  notify_percentage_change(percentage_before, percentage_after)

  broadcast(:ok, vote)
end