A few major changes:
[selectricity-live] / app / controllers / election_controller.rb
1 class ElectionController < ApplicationController
2   model :raw_voter_list, :voter, :vote, :candidate
3   layout 'vb'
4
5   before_filter :login_required
6
7   ## methods for displaying, creating,
8   ## and manipulating election overview data
9   ####################################################################
10
11   def new
12     @election = Election.new
13   end
14   
15   def create_election
16     @election = Election.new(params[:election])
17     
18     # default options
19     @election.user = session[:user]
20     @election.anonymous = 1
21     @election.startdate = Time.now
22
23     if @election.save
24       flash[:notice] = 'Election was successfully created.'
25       redirect_to :action => 'edit_candidates', :id => @election.id
26     else
27       render :action => 'new'
28     end
29   end
30   
31   # add filter to verify that the person working on or looking at
32   # something is the owner
33   def edit
34     @election = Election.find(params[:id])
35   end
36
37   def show
38     @election = Election.find(params[:id])
39   end
40
41   def update
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
46     else
47       render :action => 'edit'
48     end
49   end
50
51   def start_election
52     @election = Election.find(params[:id])
53     
54     @election.voters.each do |voter|
55       email = VoterNotify.create_votestart(voter)
56       render(:text => "<pre>" + email.encoded + "</pre>")
57       breakpoint
58       break
59     end
60
61     #@election.activate!
62   end
63
64   # methods fod display, adding, deleting, and manipulating candidate
65   # information for elections
66   ####################################################################
67   def edit_candidates
68     @election = Election.find( params[:id] )
69   end
70
71   def add_candidate
72     @election = Election.find(params[:id])
73     @candidate = Candidate.new(params[:candidate])
74     
75     if @candidate.save
76       @election.candidates << @candidate
77       @candidate = Candidate.new
78       redirect_to :action => 'edit_candidates', :id => @election.id
79     else
80       render :action => 'edit_candidates', :id => @election.id
81     end
82   end
83   
84   def delete_candidate
85     candidate = Candidate.find( params[:id] )
86     candidate.destroy
87   end
88
89   def lessinfo_candidate
90     @show_details = false
91     @candidate = Candidate.find( params[:id] )
92     render :partial => 'candidate_line'
93   end
94
95   def moreinfo_candidate
96     @show_details = true
97     @candidate = Candidate.find( params[:id] )
98     render :partial => 'candidate_line'
99   end
100
101   def edit_candidate
102     @candidate = Candidate.find( params[:id] )
103     @election = @candidate.election
104   end
105
106   def update_candidate
107     @candidate = Candidate.find(params[:id])
108     @election = @candidate.election
109
110     if @candidate.update_attributes(params[:candidate])
111       redirect_to :action => 'edit_candidates', :id => @candidate.election.id
112     else
113       render :action => 'edit_candidate'
114     end
115   end
116
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' )
123   end
124
125   ## methods for displaying, adding, deleting, and manipulating voters
126   ## for a particular election
127   ####################################################################
128   def new_voters
129     @election = Election.find( params[:id] )
130     if params.has_key?[:raw_voter_list]
131       process_incoming_voters( params[:raw_voter_list] )
132     end
133     @raw_voter_list = RawVoterList.new
134
135   end
136   
137   def edit_voters
138     @election = Election.find( params[:id] )
139     if params.has_key?( :raw_voter_list )
140       process_incoming_voters( params[:raw_voter_list] )
141     end
142
143     @raw_voter_list = RawVoterList.new
144   end
145   
146   def delete_voter
147     voter = Voter.find( params[:id] )
148     voter.destroy
149   end
150   
151   ## methods for computing and printing results
152   ####################################################################
153   def results
154     @election = Election.find( params[:id] )
155     votes = []
156
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}
160       end
161     end
162     
163     @voteobj = CloneproofSSDVote.new(votes)
164     @resultobj = @voteobj.result
165     @winners = @resultobj.winners
166     
167     @candidates_by_id = {}
168     @election.candidates.each {|cand| @candidates_by_id[cand.id] = cand}
169   end
170   
171   def detailed_results
172    
173     self.results
174
175     @voter_list = []
176     @vote_list = []
177     @election.voters. each do |voter|
178       if voter.vote and voter.vote.confirmed?
179         @voter_list << voter.email
180         @vote_list << voter.vote
181       end
182     end
183
184     @vote_list.sort!
185     @vote_list.sort! { |a,b| a.token <=> b.token }
186   end
187
188   ## private methods
189   ####################################################################
190   private
191
192     def process_incoming_voters(raw_voter_list)
193       incoming_voters = RawVoterList.new( raw_voter_list )
194
195       unless incoming_voters.entries.empty?
196         incoming_voters.each do |new_voter|
197
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
203           else
204             new_voter.contacted = 0
205           end
206         
207           # the new voter should be in good shape. save add to the election
208           new_voter.save
209           @election.voters << new_voter
210         end
211       end
212  
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
216     end
217
218     def email_voter
219     end
220
221 end

Benjamin Mako Hill || Want to submit a patch?