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.deliver_votestart(voter)
56 #render(:text => "<pre>" + email.encoded + "</pre>")
60 redirect_to :action => 'show', :id => @election.id
63 # methods fod display, adding, deleting, and manipulating candidate
64 # information for elections
65 ####################################################################
67 @election = Election.find( params[:id] )
71 @election = Election.find(params[:id])
72 @candidate = Candidate.new(params[:candidate])
75 @election.candidates << @candidate
76 @candidate = Candidate.new
77 redirect_to :action => 'edit_candidates', :id => @election.id
79 render :action => 'edit_candidates', :id => @election.id
84 candidate = Candidate.find( params[:id] )
88 def lessinfo_candidate
90 @candidate = Candidate.find( params[:id] )
91 render :partial => 'candidate_line'
94 def moreinfo_candidate
96 @candidate = Candidate.find( params[:id] )
97 render :partial => 'candidate_line'
101 @candidate = Candidate.find( params[:id] )
102 @election = @candidate.election
106 @candidate = Candidate.find(params[:id])
107 @election = @candidate.election
109 if @candidate.update_attributes(params[:candidate])
110 redirect_to :action => 'edit_candidates', :id => @candidate.election.id
112 render :action => 'edit_candidate'
116 def candidate_picture
117 candidate = Candidate.find( params[:id] )
118 send_data( candidate.picture_data,
119 :filename => candidate.picture_filename,
120 :type => candidate.picture_type,
121 :disposition => 'inline' )
124 ## methods for displaying, adding, deleting, and manipulating voters
125 ## for a particular election
126 ####################################################################
128 @election = Election.find( params[:id] )
129 if params.has_key?[:raw_voter_list]
130 process_incoming_voters( params[:raw_voter_list] )
132 @raw_voter_list = RawVoterList.new
137 @election = Election.find( params[:id] )
138 if params.has_key?( :raw_voter_list )
139 process_incoming_voters( params[:raw_voter_list] )
142 @raw_voter_list = RawVoterList.new
146 voter = Voter.find( params[:id] )
150 ## methods for computing and printing results
151 ####################################################################
153 @election = Election.find( params[:id] )
156 @election.voters.each do |voter|
157 if voter.vote and voter.vote.confirmed?
158 votes << voter.vote.rankings.sort.collect {|vote| vote.candidate_id}
162 @voteobj = CloneproofSSDVote.new(votes)
163 @resultobj = @voteobj.result
164 @winners = @resultobj.winners
166 @candidates_by_id = {}
167 @election.candidates.each {|cand| @candidates_by_id[cand.id] = cand}
176 @election.voters. each do |voter|
177 if voter.vote and voter.vote.confirmed?
178 @voter_list << voter.email
179 @vote_list << voter.vote
184 @vote_list.sort! { |a,b| a.token <=> b.token }
188 ####################################################################
191 def process_incoming_voters(raw_voter_list)
192 incoming_voters = RawVoterList.new( raw_voter_list )
194 unless incoming_voters.entries.empty?
195 incoming_voters.each do |new_voter|
197 if incoming_voters.email == 0
198 new_voter.contacted = 1
199 elsif incoming_voters.email == 1
200 email_voter( new_voter )
201 new_voter.contacted = 1
203 new_voter.contacted = 0
206 # the new voter should be in good shape. save add to the election
208 @election.voters << new_voter
212 # reset the next time to have a the same default value for emailing
213 @raw_voter_list = RawVoterList.new
214 @raw_voter_list.email = incoming_voters.email
217 def email_voter(email=nil)