1 class ElectionController < ApplicationController
2 require_dependency "raw_voter_list"
3 require_dependency "voter"
4 require_dependency "vote"
5 require_dependency "candidate"
8 #before_filter :login_required
10 ## methods for displaying, creating,
11 ## and manipulating election overview data
12 ####################################################################
15 redirect_to :action => 'general_information'
18 def general_information
19 @sidebar_content = render_to_string :partial => 'progress',
20 :locals => { :page => 'overview' }
21 @election = Election.new
22 render :action => 'general_information'
26 @election = Election.new(params[:election])
29 @election.user = session[:user]
30 @election.anonymous = 1
31 @election.startdate = Time.now
34 flash[:notice] = 'Election was successfully created.'
35 redirect_to :action => 'edit_candidates', :id => @election.id
37 render :action => 'general_information'
41 # add filter to verify that the person working on or looking at
42 # something is the owner
44 @election = Election.find(params[:id])
48 @election = Election.find(params[:id])
52 @election = Election.find(params[:id])
53 if @election.update_attributes(params[:election])
54 flash[:notice] = 'Election was successfully updated.'
55 redirect_to :action => 'show', :id => @election
57 render :action => 'edit'
62 @election = Election.find(params[:id])
63 @election.voters.each do |voter|
69 redirect_to :action => 'show', :id => @election.id
72 # methods fod display, adding, deleting, and manipulating candidate
73 # information for elections
74 ####################################################################
76 @sidebar_content = render_to_string :partial => 'progress',
77 :locals => { :page => 'candidates' }
78 @election = Election.find( params[:id] )
82 @election = Election.find(params[:id])
83 @candidate = Candidate.new(params[:candidate])
84 @election.candidates << @candidate
87 @candidate = Candidate.new
88 redirect_to :action => 'edit_candidates', :id => @election.id
90 render :action => 'edit_candidates', :id => @election.id
95 candidate = Candidate.find( params[:id] )
99 def lessinfo_candidate
100 @show_details = false
101 @current_candidate = Candidate.find( params[:id] )
102 render :partial => 'candidate_line'
105 def moreinfo_candidate
107 @current_candidate = Candidate.find( params[:id] )
108 render :partial => 'candidate_line'
112 @candidate = Candidate.find( params[:id] )
113 @election = @candidate.election
117 @candidate = Candidate.find(params[:id])
118 @election = @candidate.election
120 if @candidate.update_attributes(params[:candidate])
121 redirect_to :action => 'edit_candidates', :id => @candidate.election.id
123 render :action => 'edit_candidate'
127 def candidate_picture
128 candidate = Candidate.find( params[:id] )
129 send_data( candidate.picture.data,
130 :filename => candidate.picture.filename,
131 :type => candidate.picture.filetype,
132 :disposition => 'inline' )
135 ## methods for displaying, adding, deleting, and manipulating voters
136 ## for a particular election
137 ####################################################################
143 @election = Election.find( params[:id] )
144 if params.has_key?( :raw_voter_list )
145 process_incoming_voters( params[:raw_voter_list] )
147 @raw_voter_list = RawVoterList.new
151 voter = Voter.find( params[:id] )
156 voter_array= FullVoter.find(:all, :conditions => ["email = ?", params[:email]])
157 voter_array.delete_if {|voter| voter.election.active == 0}
158 unless voter_array.empty?
159 VoterNotify.deliver_reminder(voter_array)
163 ## methods for computing and printing results
164 ####################################################################
166 @election = Election.find( params[:id] )
169 @election.voters.each do |voter|
170 if voter.vote and voter.vote.confirmed?
171 votes << voter.vote.rankings.sort.collect {|vote| vote.candidate_id}
175 @voteobj = CloneproofSSDVote.new(votes)
176 @resultobj = @voteobj.result
177 @winners = @resultobj.winners
179 @candidates_by_id = {}
180 @election.candidates.each {|cand| @candidates_by_id[cand.id] = cand}
189 @election.voters. each do |voter|
190 if voter.vote and voter.vote.confirmed?
191 @voter_list << voter.email
192 @vote_list << voter.vote
197 @vote_list.sort! { |a,b| a.token <=> b.token }
201 ####################################################################
204 def process_incoming_voters(raw_voter_list)
205 incoming_voters = RawVoterList.new( raw_voter_list )
207 unless incoming_voters.entries.empty?
208 incoming_voters.each do |new_voter|
209 new_voter.email.strip! # There's a trailing \r on all but the last in
212 # the new voter should be in good shape. save add to the election
213 @election.voters << new_voter
216 # TODO: Can we do some kind of AJAX error message for the voter being invalid?
221 # reset the next time to have a the same default value for emailing
222 @raw_voter_list = RawVoterList.new
223 @raw_voter_list.email = incoming_voters.email
226 def email_voter(voter=nil)
228 VoterNotify.deliver_votestart(voter)