1 class ElectionController < ApplicationController
2 model :raw_voter_list, :voter, :vote, :candidate
5 before_filter :login_required
7 ## general methods for dealing with elections
8 ####################################################################
11 render :action => 'list'
15 @election_pages, @elections = paginate :elections, :per_page => 10
18 ## methods for displaying, creating,
19 ## and manipulating election overview data
20 ####################################################################
23 @election = Election.find(params[:id])
27 @election = Election.new
31 @election = Election.find(params[:id])
35 @election = Election.new(params[:election])
37 flash[:notice] = 'Election was successfully created.'
38 redirect_to :action => 'new_candidates', :id => @election.id
40 render :action => 'new'
45 @election = Election.find(params[:id])
46 if @election.update_attributes(params[:election])
47 flash[:notice] = 'Election was successfully updated.'
48 redirect_to :action => 'show', :id => @election
50 render :action => 'edit'
55 election = Election.find(params[:id]).destroy
56 redirect_to :action => 'list'
59 # methods fod display, adding, deleting, and manipulating candidate
60 # information for elections
61 ####################################################################
63 @election = Election.find( params[:id] )
67 election = Election.find( params[:id] )
68 @candidate = Candidate.new
69 @candidate.name = params[:newcandidate]
71 election.candidates << @candidate
72 render :partial => 'candidate_line'
76 candidate = Candidate.find( params[:id] )
81 @election = Election.find( params[:id] )
84 ## methods for displaying, adding, deleting, and manipulating voters
85 ## for a particular election
86 ####################################################################
88 @election = Election.find( params[:id] )
89 if params.has_key?[:raw_voter_list]
90 process_incoming_voters( params[:raw_voter_list] )
92 @raw_voter_list = RawVoterList.new
97 @election = Election.find( params[:id] )
98 if params.has_key?( :raw_voter_list )
99 process_incoming_voters( params[:raw_voter_list] )
102 @raw_voter_list = RawVoterList.new
106 voter = Voter.find( params[:id] )
110 ## methods for computing and printing results
111 ####################################################################
113 @election = Election.find( params[:id] )
116 @election.voters.each do |voter|
117 if voter.vote and voter.vote.confirmed?
118 votes << voter.vote.rankings.sort.collect {|vote| vote.candidate_id}
122 @voteobj = CloneproofSSDVote.new(votes)
123 @resultobj = @voteobj.result
124 @winners = @resultobj.winners
126 @candidates_by_id = {}
127 @election.candidates.each {|cand| @candidates_by_id[cand.id] = cand}
136 @election.voters. each do |voter|
137 if voter.vote and voter.vote.confirmed?
138 @voter_list << voter.email
139 @vote_list << voter.vote
144 @vote_list.sort! { |a,b| a.token <=> b.token }
148 ####################################################################
151 def process_incoming_voters(raw_voter_list)
152 incoming_voters = RawVoterList.new( raw_voter_list )
154 unless incoming_voters.entries.empty?
155 incoming_voters.each do |new_voter|
157 if incoming_voters.email == 0
158 new_voter.contacted = 1
159 elsif incoming_voters.email == 1
160 email_voter( new_voter )
161 new_voter.contacted = 1
163 new_voter.contacted = 0
166 # the new voter should be in good shape. save add to the election
168 @election.voters << new_voter
172 # reset the next time to have a the same default value for emailing
173 @raw_voter_list = RawVoterList.new
174 @raw_voter_list.email = incoming_voters.email