Class: Decidim::Elections::VotesController

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

Overview

Exposes the elections resources so users can participate on them

Instance Method Summary collapse

Methods included from HasVoteFlow

#ballot_questions, #can_preview?, #default_vote_flow, #preview_mode?, #vote_flow

Instance Method Details

#createObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/decidim/elections/votes_controller.rb', line 25

def create
  vote_flow.voter_from_token(params.require(:vote).permit(:voter_token, :voter_id))
  return unless valid_voter_token?
  return unless vote_allowed?

  return redirect_to election_vote_path(election, id: params[:vote][:encrypted_data_hash], token: vote_flow.voter_id_token) if preview_mode?

  @form = form(Voter::VoteForm).from_params(params, election: election, user: vote_flow.user, email: vote_flow.email)
  Voter::CastVote.call(@form) do
    on(:ok) do |vote|
      redirect_to election_vote_path(election, id: vote.encrypted_vote_hash, token: vote_flow.voter_id_token)
    end
    on(:invalid) do
      flash[:alert] = I18n.t("votes.create.error", scope: "decidim.elections")
      redirect_to exit_path
    end
  end
end

#newObject



17
18
19
20
21
22
23
# File 'app/controllers/decidim/elections/votes_controller.rb', line 17

def new
  vote_flow.(params)
  return unless vote_allowed?

  @form = form(Voter::VoteForm).from_params({ voter_token: voter_token, voter_id: voter_id },
                                            election: election, user: vote_flow.user)
end

#showObject



44
45
46
# File 'app/controllers/decidim/elections/votes_controller.rb', line 44

def show
  enforce_permission_to :view, :election, election: election
end

#updateObject



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/decidim/elections/votes_controller.rb', line 48

def update
  enforce_permission_to :view, :election, election: election

  Voter::UpdateVoteStatus.call(vote) do
    on(:ok) do
      redirect_to election_vote_path(election, id: vote.encrypted_vote_hash, token: vote_flow.voter_id_token(vote.voter_id))
    end
    on(:invalid) do
      flash[:alert] = I18n.t("votes.update.error", scope: "decidim.elections")
      redirect_to exit_path
    end
  end
end

#verifyObject



62
63
64
65
66
# File 'app/controllers/decidim/elections/votes_controller.rb', line 62

def verify
  enforce_permission_to :view, :election, election: election

  @form = form(Voter::VerifyVoteForm).instance(election: election)
end