1 class ElectionController < ApplicationController
2 require_dependency "raw_voter_list"
3 require_dependency "voter"
4 require_dependency "vote"
5 require_dependency "candidate"
8 ## methods for displaying, creating,
9 ## and manipulating election overview data
10 ####################################################################
13 redirect_to :action => 'general_information'
16 def general_information
17 @sidebar_content = render_to_string :partial => 'progress',
18 :locals => { :page => 'overview' }
19 @election = Election.new
20 render :action => 'general_information'
24 @election = Election.new(params[:election])
27 @election.user = session[:user]
28 @election.anonymous = 1
29 @election.startdate = Time.now
32 flash[:notice] = 'Election was successfully created.'
33 redirect_to :action => 'edit_candidates', :id => @election.id
35 render :action => 'general_information'
39 # add filter to verify that the person working on or looking at
40 # something is the owner
42 @election = Election.find(params[:id])
46 @election = Election.find(params[:id])
50 @election = Election.find(params[:id])
51 if @election.update_attributes(params[:election])
52 flash[:notice] = 'Election was successfully updated.'
53 redirect_to :action => 'show', :id => @election
55 render :action => 'edit'
60 @election = Election.find(params[:id])
61 @election.voters.each do |voter|
67 redirect_to :action => 'show', :id => @election.id
70 # methods fod display, adding, deleting, and manipulating candidate
71 # information for elections
72 ####################################################################
74 @sidebar_content = render_to_string :partial => 'progress',
75 :locals => { :page => 'candidates' }
76 @election = Election.find( params[:id] )
80 @election = Election.find(params[:id])
81 @candidate = Candidate.new(params[:candidate])
82 @election.candidates << @candidate
85 @candidate = Candidate.new
86 redirect_to :action => 'edit_candidates', :id => @election.id
88 render :action => 'edit_candidates', :id => @election.id
93 candidate = Candidate.find( params[:id] )
97 def lessinfo_candidate
99 @current_candidate = Candidate.find( params[:id] )
100 render :partial => 'candidate_line'
103 def moreinfo_candidate
105 @current_candidate = Candidate.find( params[:id] )
106 render :partial => 'candidate_line'
110 @candidate = Candidate.find( params[:id] )
111 @election = @candidate.election
115 @candidate = Candidate.find(params[:id])
116 @election = @candidate.election
118 if @candidate.update_attributes(params[:candidate])
119 redirect_to :action => 'edit_candidates', :id => @candidate.election.id
121 render :action => 'edit_candidate'
125 def candidate_picture
126 candidate = Candidate.find( params[:id] )
127 send_data( candidate.picture.data,
128 :filename => candidate.picture.filename,
129 :type => candidate.picture.filetype,
130 :disposition => 'inline' )
133 ## methods for displaying, adding, deleting, and manipulating voters
134 ## for a particular election
135 ####################################################################
141 @election = Election.find( params[:id] )
142 if params.has_key?( :raw_voter_list )
143 process_incoming_voters( params[:raw_voter_list] )
145 @raw_voter_list = RawVoterList.new
149 voter = Voter.find( params[:id] )
154 voter_array= FullVoter.find(:all, :conditions => ["email = ?", params[:email]])
155 voter_array.delete_if {|voter| voter.election.active == 0}
156 unless voter_array.empty?
157 VoterNotify.deliver_reminder(voter_array)
161 ## methods for computing and printing results
162 ####################################################################
164 @election = Election.find( params[:id] )
167 @election.voters.each do |voter|
168 if voter.vote and voter.vote.confirmed?
169 votes << voter.vote.rankings.sort.collect {|vote| vote.candidate_id}
173 @voteobj = CloneproofSSDVote.new(votes)
174 @resultobj = @voteobj.result
175 @winners = @resultobj.winners
177 @candidates_by_id = {}
178 @election.candidates.each {|cand| @candidates_by_id[cand.id] = cand}
187 @election.voters. each do |voter|
188 if voter.vote and voter.vote.confirmed?
189 @voter_list << voter.email
190 @vote_list << voter.vote
195 @vote_list.sort! { |a,b| a.token <=> b.token }
199 ####################################################################
202 def process_incoming_voters(raw_voter_list)
203 incoming_voters = RawVoterList.new( raw_voter_list )
205 unless incoming_voters.entries.empty?
206 incoming_voters.each do |new_voter|
207 new_voter.email.strip! # There's a trailing \r on all but the last in
210 # the new voter should be in good shape. save add to the election
211 @election.voters << new_voter
214 # TODO: Can we do some kind of AJAX error message for the voter being invalid?
219 # reset the next time to have a the same default value for emailing
220 @raw_voter_list = RawVoterList.new
221 @raw_voter_list.email = incoming_voters.email
224 def email_voter(voter=nil)
226 VoterNotify.deliver_votestart(voter)