Class: Decidim::Proposals::ProposalVotesController

Inherits:
ApplicationController show all
Includes:
ControllerHelpers, ProposalVotesHelper
Defined in:
app/controllers/decidim/proposals/proposal_votes_controller.rb

Overview

Exposes the proposal vote resource so users can vote proposals.

Instance Method Summary collapse

Methods included from ProposalVotesHelper

#can_accumulate_votes_beyond_threshold?, #current_user_can_vote?, #remaining_votes_count_for, #threshold_per_proposal, #threshold_per_proposal_enabled?, #vote_limit, #vote_limit_enabled?, #votes_blocked?, #votes_enabled?

Methods inherited from ApplicationController

#proposal_limit, #proposal_limit_reached?, #proposals

Instance Method Details

#createObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/controllers/decidim/proposals/proposal_votes_controller.rb', line 14

def create
  enforce_permission_to(:vote, :proposal, proposal:)
  @from_proposals_list = params[:from_proposals_list] == "true"

  VoteProposal.call(proposal, current_user) do
    on(:ok) do
      proposal.reload

      proposals = ProposalVote.where(
        author: current_user,
        proposal: Proposal.where(component: current_component)
      ).map(&:proposal)

      expose(proposals:)
      render :update_buttons_and_counters
    end

    on(:invalid) do
      render json: { error: I18n.t("proposal_votes.create.error", scope: "decidim.proposals") }, status: :unprocessable_entity
    end
  end
end

#destroyObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/decidim/proposals/proposal_votes_controller.rb', line 37

def destroy
  enforce_permission_to(:unvote, :proposal, proposal:)
  @from_proposals_list = params[:from_proposals_list] == "true"

  UnvoteProposal.call(proposal, current_user) do
    on(:ok) do
      proposal.reload

      proposals = ProposalVote.where(
        author: current_user,
        proposal: Proposal.where(component: current_component)
      ).map(&:proposal)

      expose(proposals: proposals + [proposal])
      render :update_buttons_and_counters
    end
  end
end