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 @sidebar_content = render_to_string :partial => 'progress',
47 :locals => { :page => 'review' }
49 @election = Election.find(params[:id])
53 @election = Election.find(params[:id])
54 if @election.update_attributes(params[:election])
55 flash[:notice] = 'Election was successfully updated.'
56 redirect_to :action => 'show', :id => @election
58 render :action => 'edit'
63 @election = Election.find(params[:id])
64 @election.voters.each do |voter|
70 redirect_to :action => 'show', :id => @election.id
73 # methods fod display, adding, deleting, and manipulating candidate
74 # information for elections
75 ####################################################################
77 @sidebar_content = render_to_string :partial => 'progress',
78 :locals => { :page => 'candidates' }
79 @election = Election.find( params[:id] )
83 @election = Election.find(params[:id])
84 @candidate = Candidate.new(params[:candidate])
85 @election.candidates << @candidate
88 @candidate = Candidate.new
89 redirect_to :action => 'edit_candidates', :id => @election.id
91 render :action => 'edit_candidates', :id => @election.id
96 candidate = Candidate.find( params[:id] )
100 def candidate_picture
101 candidate = Candidate.find( params[:id] )
102 send_data( candidate.picture.data,
103 :filename => candidate.picture.filename,
104 :type => candidate.picture.filetype,
105 :disposition => 'inline' )
108 ## methods for displaying, adding, deleting, and manipulating voters
109 ## for a particular election
110 ####################################################################
112 redirect_to :action => 'edit_voters', :id => params[:id]
116 @sidebar_content = render_to_string :partial => 'progress',
117 :locals => { :page => 'voters' }
119 @election = Election.find( params[:id] )
120 if params.has_key?( :raw_voter_list )
121 process_incoming_voters( params[:raw_voter_list] )
123 @raw_voter_list = RawVoterList.new
127 voter = Voter.find( params[:id] )
131 ## methods for computing and printing results
132 ####################################################################
134 @election = Election.find( params[:id] )
137 @election.voters.each do |voter|
138 if voter.vote and voter.vote.confirmed?
139 votes << voter.vote.rankings.sort.collect {|vote| vote.candidate_id}
143 @voteobj = CloneproofSSDVote.new(votes)
144 @resultobj = @voteobj.result
145 @winners = @resultobj.winners
147 @candidates_by_id = {}
148 @election.candidates.each {|cand| @candidates_by_id[cand.id] = cand}
157 @election.voters. each do |voter|
158 if voter.vote and voter.vote.confirmed?
159 @voter_list << voter.email
160 @vote_list << voter.vote
165 @vote_list.sort! { |a,b| a.token <=> b.token }
169 ####################################################################
172 def process_incoming_voters(raw_voter_list)
173 incoming_voters = RawVoterList.new( raw_voter_list )
175 unless incoming_voters.entries.empty?
176 incoming_voters.each do |new_voter|
177 new_voter.email.strip! # There's a trailing \r on all but the last in
180 # the new voter should be in good shape. save add to the election
181 @election.voters << new_voter
184 # TODO: Can we do some kind of AJAX error message for the voter being invalid?
189 # reset the next time to have a the same default value for emailing
190 @raw_voter_list = RawVoterList.new
191 @raw_voter_list.email = incoming_voters.email
194 def email_voter(voter=nil)
196 VoterNotify.deliver_votestart(voter)