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] )
155 ## methods for computing and printing results
156 ####################################################################
158 @election = Election.find( params[:id] )
161 @election.voters.each do |voter|
162 if voter.vote and voter.vote.confirmed?
163 votes << voter.vote.rankings.sort.collect {|vote| vote.candidate_id}
167 @voteobj = CloneproofSSDVote.new(votes)
168 @resultobj = @voteobj.result
169 @winners = @resultobj.winners
171 @candidates_by_id = {}
172 @election.candidates.each {|cand| @candidates_by_id[cand.id] = cand}
181 @election.voters. each do |voter|
182 if voter.vote and voter.vote.confirmed?
183 @voter_list << voter.email
184 @vote_list << voter.vote
189 @vote_list.sort! { |a,b| a.token <=> b.token }
193 ####################################################################
196 def process_incoming_voters(raw_voter_list)
197 incoming_voters = RawVoterList.new( raw_voter_list )
199 unless incoming_voters.entries.empty?
200 incoming_voters.each do |new_voter|
201 new_voter.email.strip! # There's a trailing \r on all but the last in
204 # the new voter should be in good shape. save add to the election
205 @election.voters << new_voter
208 # TODO: Can we do some kind of AJAX error message for the voter being invalid?
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
218 def email_voter(voter=nil)
220 VoterNotify.deliver_votestart(voter)