Class: Decidim::Elections::VotesController

Inherits:
ApplicationController show all
Includes:
UsesVotesBooth, FormFactory
Defined in:
app/controllers/decidim/elections/votes_controller.rb

Overview

Provides access to election resources so that users can participate Election.where(component: current_component).published.lections.

Instance Method Summary collapse

Methods included from UsesVotesBooth

#receipt

Instance Method Details

#castObject

Casts the votes that have been saved in the session and redirects to the receipt page



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/decidim/elections/votes_controller.rb', line 41

def cast
  enforce_permission_to(:create, :vote, election:)

  CastVotes.call(election, votes_buffer, voter_uid) do
    on(:ok) do
      votes_buffer.clear
      session[:voter_uid] = voter_uid
      session_attributes.clear
      redirect_to receipt_election_votes_path(election), notice: t("votes.cast.success", scope: "decidim.elections")
    end

    on(:invalid) do
      redirect_to confirm_election_votes_path(election), alert: I18n.t("votes.cast.invalid", scope: "decidim.elections")
    end
  end
end

#confirmObject

Shows the confirmation page with the votes that will be cast



36
37
38
# File 'app/controllers/decidim/elections/votes_controller.rb', line 36

def confirm
  enforce_permission_to(:create, :vote, election:)
end

#showObject

Show the voting form for the given question



15
16
17
# File 'app/controllers/decidim/elections/votes_controller.rb', line 15

def show
  enforce_permission_to(:create, :vote, election:)
end

#updateObject

Saves the vote for the current question and redirect to the next question



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/decidim/elections/votes_controller.rb', line 20

def update
  enforce_permission_to(:create, :vote, election:)

  response_ids = Array(params.dig(:response, question.id.to_s)).compact

  if question.max_choices.present? && response_ids.size > question.max_choices
    flash.now[:alert] = t("votes.question.max_choices_exceeded", scope: "decidim.elections", max: question.max_choices)
    render :show
    return
  end

  votes_buffer[question.id.to_s] = params.dig(:response, question.id.to_s)
  redirect_to next_vote_step_path
end