Class: Decidim::Initiatives::VoteInitiative
- 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
-
#votes ⇒ Object
readonly
Returns the value of attribute votes.
Instance Method Summary collapse
-
#call ⇒ Object
Executes the command.
-
#initialize(form) ⇒ VoteInitiative
constructor
Public: Initializes the command.
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
#votes ⇒ Object (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
#call ⇒ Object
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 |