1 class ElectionController < ApplicationController
2 model :raw_voter_list, :voter, :vote, :candidate
4 before_filter :login_required
6 ## general methods for dealing with elections
7 ####################################################################
10 render :action => 'list'
14 @election_pages, @elections = paginate :elections, :per_page => 10
17 ## methods for displaying, creating,
18 ## and manipulating election overview data
19 ####################################################################
22 @election = Election.find(params[:id])
26 @election = Election.new
30 @election = Election.find(params[:id])
34 @election = Election.new(params[:election])
36 flash[:notice] = 'Election was successfully created.'
37 redirect_to :action => 'new_candidates', :id => @election.id
39 render :action => 'new'
44 @election = Election.find(params[:id])
45 if @election.update_attributes(params[:election])
46 flash[:notice] = 'Election was successfully updated.'
47 redirect_to :action => 'show', :id => @election
49 render :action => 'edit'
54 election = Election.find(params[:id]).destroy
55 redirect_to :action => 'list'
58 # methods fod display, adding, deleting, and manipulating candidate
59 # information for elections
60 ####################################################################
62 @election = Election.find( params[:id] )
66 election = Election.find( params[:id] )
67 @candidate = Candidate.new
68 @candidate.name = params[:newcandidate]
70 election.candidates << @candidate
71 render :partial => 'candidate_line'
75 candidate = Candidate.find( params[:id] )
80 @election = Election.find( params[:id] )
83 ## methods for displaying, adding, deleting, and manipulating voters
84 ## for a particular election
85 ####################################################################
87 @election = Election.find( params[:id] )
88 if params.has_key?[:raw_voter_list]
89 process_incoming_voters( params[:raw_voter_list] )
91 @raw_voter_list = RawVoterList.new
96 @election = Election.find( params[:id] )
97 if params.has_key?( :raw_voter_list )
98 process_incoming_voters( params[:raw_voter_list] )
101 @raw_voter_list = RawVoterList.new
105 voter = Voter.find( params[:id] )
109 ## methods for computing and printing results
110 ####################################################################
112 @election = Election.find( params[:id] )
115 @election.voters.each do |voter|
116 if voter.vote and voter.vote.confirmed?
117 votes << voter.vote.rankings.sort.collect {|vote| vote.candidate_id}
121 @voteobj = CloneproofSSDVote.new(votes)
122 @resultobj = @voteobj.result
123 @winners = @resultobj.winners
125 @candidates_by_id = {}
126 @election.candidates.each {|cand| @candidates_by_id[cand.id] = cand}
135 @election.voters. each do |voter|
136 if voter.vote and voter.vote.confirmed?
137 @voter_list << voter.email
138 @vote_list << voter.vote
143 @vote_list.sort! { |a,b| a.token <=> b.token }
147 ####################################################################
150 def process_incoming_voters(raw_voter_list)
151 incoming_voters = RawVoterList.new( raw_voter_list )
153 unless incoming_voters.entries.empty?
154 incoming_voters.each do |new_voter|
156 if incoming_voters.email == 0
157 new_voter.contacted = 1
158 elsif incoming_voters.email == 1
159 email_voter( new_voter )
160 new_voter.contacted = 1
162 new_voter.contacted = 0
165 # the new voter should be in good shape. save add to the election
167 @election.voters << new_voter
171 # reset the next time to have a the same default value for emailing
172 @raw_voter_list = RawVoterList.new
173 @raw_voter_list.email = incoming_voters.email