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|
65 email = VoterNotify.deliver_votestart(voter)
66 #render(:text => "<pre>" + email.encoded + "</pre>")
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 lessinfo_candidate
101 @show_details = false
102 @current_candidate = Candidate.find( params[:id] )
103 render :partial => 'candidate_line'
106 def moreinfo_candidate
108 @current_candidate = Candidate.find( params[:id] )
109 render :partial => 'candidate_line'
113 @candidate = Candidate.find( params[:id] )
114 @election = @candidate.election
118 @candidate = Candidate.find(params[:id])
119 @election = @candidate.election
121 if @candidate.update_attributes(params[:candidate])
122 redirect_to :action => 'edit_candidates', :id => @candidate.election.id
124 render :action => 'edit_candidate'
128 def candidate_picture
129 candidate = Candidate.find( params[:id] )
130 send_data( candidate.picture.data,
131 :filename => candidate.picture.filename,
132 :type => candidate.picture.filetype,
133 :disposition => 'inline' )
136 ## methods for displaying, adding, deleting, and manipulating voters
137 ## for a particular election
138 ####################################################################
144 @election = Election.find( params[:id] )
145 if params.has_key?( :raw_voter_list )
146 process_incoming_voters( params[:raw_voter_list] )
148 @raw_voter_list = RawVoterList.new
152 voter = Voter.find( params[:id] )
156 ## methods for computing and printing results
157 ####################################################################
159 @election = Election.find( params[:id] )
162 @election.voters.each do |voter|
163 if voter.vote and voter.vote.confirmed?
164 votes << voter.vote.rankings.sort.collect {|vote| vote.candidate_id}
168 @voteobj = CloneproofSSDVote.new(votes)
169 @resultobj = @voteobj.result
170 @winners = @resultobj.winners
172 @candidates_by_id = {}
173 @election.candidates.each {|cand| @candidates_by_id[cand.id] = cand}
182 @election.voters. each do |voter|
183 if voter.vote and voter.vote.confirmed?
184 @voter_list << voter.email
185 @vote_list << voter.vote
190 @vote_list.sort! { |a,b| a.token <=> b.token }
194 ####################################################################
197 def process_incoming_voters(raw_voter_list)
198 incoming_voters = RawVoterList.new( raw_voter_list )
200 unless incoming_voters.entries.empty?
201 incoming_voters.each do |new_voter|
202 new_voter.email.strip! # There's a trailing \r on all but the last in
204 if incoming_voters.email == 0
205 new_voter.contacted = 1
206 elsif incoming_voters.email == 1
207 email_voter( new_voter )
208 new_voter.contacted = 1
210 new_voter.contacted = 0
213 # the new voter should be in good shape. save add to the election
214 @election.voters << new_voter
217 # TODO: Can we do some kind of AJAX error message for the voter being invalid?
222 # reset the next time to have a the same default value for emailing
223 @raw_voter_list = RawVoterList.new
224 @raw_voter_list.email = incoming_voters.email
227 def email_voter(email=nil)