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

Benjamin Mako Hill || Want to submit a patch?