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>")
64 # methods fod display, adding, deleting, and manipulating candidate
65 # information for elections
66 ####################################################################
68 @election = Election.find( params[:id] )
72 @election = Election.find(params[:id])
73 @candidate = Candidate.new(params[:candidate])
76 @election.candidates << @candidate
77 @candidate = Candidate.new
78 redirect_to :action => 'edit_candidates', :id => @election.id
80 render :action => 'edit_candidates', :id => @election.id
85 candidate = Candidate.find( params[:id] )
89 def lessinfo_candidate
91 @candidate = Candidate.find( params[:id] )
92 render :partial => 'candidate_line'
95 def moreinfo_candidate
97 @candidate = Candidate.find( params[:id] )
98 render :partial => 'candidate_line'
102 @candidate = Candidate.find( params[:id] )
103 @election = @candidate.election
107 @candidate = Candidate.find(params[:id])
108 @election = @candidate.election
110 if @candidate.update_attributes(params[:candidate])
111 redirect_to :action => 'edit_candidates', :id => @candidate.election.id
113 render :action => 'edit_candidate'
117 def candidate_picture
118 candidate = Candidate.find( params[:id] )
119 send_data( candidate.picture_data,
120 :filename => candidate.picture_filename,
121 :type => candidate.picture_type,
122 :disposition => 'inline' )
125 ## methods for displaying, adding, deleting, and manipulating voters
126 ## for a particular election
127 ####################################################################
129 @election = Election.find( params[:id] )
130 if params.has_key?[:raw_voter_list]
131 process_incoming_voters( params[:raw_voter_list] )
133 @raw_voter_list = RawVoterList.new
138 @election = Election.find( params[:id] )
139 if params.has_key?( :raw_voter_list )
140 process_incoming_voters( params[:raw_voter_list] )
143 @raw_voter_list = RawVoterList.new
147 voter = Voter.find( params[:id] )
151 ## methods for computing and printing results
152 ####################################################################
154 @election = Election.find( params[:id] )
157 @election.voters.each do |voter|
158 if voter.vote and voter.vote.confirmed?
159 votes << voter.vote.rankings.sort.collect {|vote| vote.candidate_id}
163 @voteobj = CloneproofSSDVote.new(votes)
164 @resultobj = @voteobj.result
165 @winners = @resultobj.winners
167 @candidates_by_id = {}
168 @election.candidates.each {|cand| @candidates_by_id[cand.id] = cand}
177 @election.voters. each do |voter|
178 if voter.vote and voter.vote.confirmed?
179 @voter_list << voter.email
180 @vote_list << voter.vote
185 @vote_list.sort! { |a,b| a.token <=> b.token }
189 ####################################################################
192 def process_incoming_voters(raw_voter_list)
193 incoming_voters = RawVoterList.new( raw_voter_list )
195 unless incoming_voters.entries.empty?
196 incoming_voters.each do |new_voter|
198 if incoming_voters.email == 0
199 new_voter.contacted = 1
200 elsif incoming_voters.email == 1
201 email_voter( new_voter )
202 new_voter.contacted = 1
204 new_voter.contacted = 0
207 # the new voter should be in good shape. save add to the election
209 @election.voters << new_voter
213 # reset the next time to have a the same default value for emailing
214 @raw_voter_list = RawVoterList.new
215 @raw_voter_list.email = incoming_voters.email