1 class ElectionController < ApplicationController
2 model :raw_voter_list, :voter, :vote, :candidate
5 before_filter :login_required
7 ## methods for displaying, creating,
8 ## and manipulating election overview data
9 ####################################################################
12 @election = Election.new
16 @election = Election.new(params[:election])
19 @election.user = session[:user]
20 @election.anonymous = 1
21 @election.startdate = Time.now
24 flash[:notice] = 'Election was successfully created.'
25 redirect_to :action => 'edit_candidates', :id => @election.id
27 render :action => 'new'
31 # add filter to verify that the person working on or looking at
32 # something is the owner
34 @election = Election.find(params[:id])
38 @election = Election.find(params[:id])
42 @election = Election.find(params[:id])
43 if @election.update_attributes(params[:election])
44 flash[:notice] = 'Election was successfully updated.'
45 redirect_to :action => 'show', :id => @election
47 render :action => 'edit'
52 @election = Election.find(params[:id])
54 @election.voters.each do |voter|
55 email = VoterNotify.create_votestart(voter)
56 render(:text => "<pre>" + email.encoded + "</pre>")
62 # methods fod display, adding, deleting, and manipulating candidate
63 # information for elections
64 ####################################################################
66 @election = Election.find( params[:id] )
70 @election = Election.find(params[:id])
71 @candidate = Candidate.new(params[:candidate])
74 @election.candidates << @candidate
75 @candidate = Candidate.new
76 redirect_to :action => 'edit_candidates', :id => @election.id
78 render :action => 'edit_candidates', :id => @election.id
83 candidate = Candidate.find( params[:id] )
87 def lessinfo_candidate
89 @candidate = Candidate.find( params[:id] )
90 render :partial => 'candidate_line'
93 def moreinfo_candidate
95 @candidate = Candidate.find( params[:id] )
96 render :partial => 'candidate_line'
100 @candidate = Candidate.find( params[:id] )
101 @election = @candidate.election
105 @candidate = Candidate.find(params[:id])
106 @election = @candidate.election
108 if @candidate.update_attributes(params[:candidate])
109 redirect_to :action => 'edit_candidates', :id => @candidate.election.id
111 render :action => 'edit_candidate'
115 def candidate_picture
116 candidate = Candidate.find( params[:id] )
117 send_data( candidate.picture_data,
118 :filename => candidate.picture_filename,
119 :type => candidate.picture_type,
120 :disposition => 'inline' )
123 ## methods for displaying, adding, deleting, and manipulating voters
124 ## for a particular election
125 ####################################################################
127 @election = Election.find( params[:id] )
128 if params.has_key?[:raw_voter_list]
129 process_incoming_voters( params[:raw_voter_list] )
131 @raw_voter_list = RawVoterList.new
136 @election = Election.find( params[:id] )
137 if params.has_key?( :raw_voter_list )
138 process_incoming_voters( params[:raw_voter_list] )
141 @raw_voter_list = RawVoterList.new
145 voter = Voter.find( params[:id] )
149 ## methods for computing and printing results
150 ####################################################################
152 @election = Election.find( params[:id] )
155 @election.voters.each do |voter|
156 if voter.vote and voter.vote.confirmed?
157 votes << voter.vote.rankings.sort.collect {|vote| vote.candidate_id}
161 @voteobj = CloneproofSSDVote.new(votes)
162 @resultobj = @voteobj.result
163 @winners = @resultobj.winners
165 @candidates_by_id = {}
166 @election.candidates.each {|cand| @candidates_by_id[cand.id] = cand}
175 @election.voters. each do |voter|
176 if voter.vote and voter.vote.confirmed?
177 @voter_list << voter.email
178 @vote_list << voter.vote
183 @vote_list.sort! { |a,b| a.token <=> b.token }
187 ####################################################################
190 def process_incoming_voters(raw_voter_list)
191 incoming_voters = RawVoterList.new( raw_voter_list )
193 unless incoming_voters.entries.empty?
194 incoming_voters.each do |new_voter|
196 if incoming_voters.email == 0
197 new_voter.contacted = 1
198 elsif incoming_voters.email == 1
199 email_voter( new_voter )
200 new_voter.contacted = 1
202 new_voter.contacted = 0
205 # the new voter should be in good shape. save add to the election
207 @election.voters << new_voter
211 # reset the next time to have a the same default value for emailing
212 @raw_voter_list = RawVoterList.new
213 @raw_voter_list.email = incoming_voters.email