Variety of small changes (mostly to properties) plus a few "in the
[selectricity-live] / app / controllers / election_controller.rb
1 class ElectionController < ApplicationController
2   model :raw_voter_list, :voter, :vote, :candidate
3   layout 'hc'
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.deliver_votestart(voter)
56       #render(:text => "<pre>" + email.encoded + "</pre>")
57     end
58
59     @election.activate!
60     redirect_to :action => 'show', :id => @election.id
61   end
62
63   # methods fod display, adding, deleting, and manipulating candidate
64   # information for elections
65   ####################################################################
66   def edit_candidates
67     @election = Election.find( params[:id] )
68   end
69
70   def add_candidate
71     @election = Election.find(params[:id])
72     @candidate = Candidate.new(params[:candidate])
73     
74     if @candidate.save
75       @election.candidates << @candidate
76       @candidate = Candidate.new
77       redirect_to :action => 'edit_candidates', :id => @election.id
78     else
79       render :action => 'edit_candidates', :id => @election.id
80     end
81   end
82   
83   def delete_candidate
84     candidate = Candidate.find( params[:id] )
85     candidate.destroy
86   end
87
88   def lessinfo_candidate
89     @show_details = false
90     @candidate = Candidate.find( params[:id] )
91     render :partial => 'candidate_line'
92   end
93
94   def moreinfo_candidate
95     @show_details = true
96     @candidate = Candidate.find( params[:id] )
97     render :partial => 'candidate_line'
98   end
99
100   def edit_candidate
101     @candidate = Candidate.find( params[:id] )
102     @election = @candidate.election
103   end
104
105   def update_candidate
106     @candidate = Candidate.find(params[:id])
107     @election = @candidate.election
108
109     if @candidate.update_attributes(params[:candidate])
110       redirect_to :action => 'edit_candidates', :id => @candidate.election.id
111     else
112       render :action => 'edit_candidate'
113     end
114   end
115
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' )
122   end
123
124   ## methods for displaying, adding, deleting, and manipulating voters
125   ## for a particular election
126   ####################################################################
127   def new_voters
128     @election = Election.find( params[:id] )
129     if params.has_key?[:raw_voter_list]
130       process_incoming_voters( params[:raw_voter_list] )
131     end
132     @raw_voter_list = RawVoterList.new
133
134   end
135   
136   def edit_voters
137     @election = Election.find( params[:id] )
138     if params.has_key?( :raw_voter_list )
139       process_incoming_voters( params[:raw_voter_list] )
140     end
141
142     @raw_voter_list = RawVoterList.new
143   end
144   
145   def delete_voter
146     voter = Voter.find( params[:id] )
147     voter.destroy
148   end
149   
150   ## methods for computing and printing results
151   ####################################################################
152   def results
153     @election = Election.find( params[:id] )
154     votes = []
155
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}
159       end
160     end
161     
162     @voteobj = CloneproofSSDVote.new(votes)
163     @resultobj = @voteobj.result
164     @winners = @resultobj.winners
165     
166     @candidates_by_id = {}
167     @election.candidates.each {|cand| @candidates_by_id[cand.id] = cand}
168   end
169   
170   def detailed_results
171    
172     self.results
173
174     @voter_list = []
175     @vote_list = []
176     @election.voters. each do |voter|
177       if voter.vote and voter.vote.confirmed?
178         @voter_list << voter.email
179         @vote_list << voter.vote
180       end
181     end
182
183     @vote_list.sort!
184     @vote_list.sort! { |a,b| a.token <=> b.token }
185   end
186
187   ## private methods
188   ####################################################################
189   private
190
191     def process_incoming_voters(raw_voter_list)
192       incoming_voters = RawVoterList.new( raw_voter_list )
193
194       unless incoming_voters.entries.empty?
195         incoming_voters.each do |new_voter|
196
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
202           else
203             new_voter.contacted = 0
204           end
205         
206           # the new voter should be in good shape. save add to the election
207           new_voter.save
208           @election.voters << new_voter
209         end
210       end
211  
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
215     end
216
217     def email_voter(email=nil)
218       if email
219         
220       end
221     end
222
223 end

Benjamin Mako Hill || Want to submit a patch?