Working on a step-by-step workflow audit.
[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 => 'new_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 destroy
52     election = Election.find(params[:id]).destroy
53     redirect_to :action => 'list'
54   end
55
56   # methods fod display, adding, deleting, and manipulating candidate
57   # information for elections
58   ####################################################################
59   def new_candidates
60     @election = Election.find( params[:id] )
61   end
62
63   def add_candidate
64     election = Election.find( params[:id] )
65     @candidate = Candidate.new
66     @candidate.name = params[:newcandidate][:name]
67     @candidate.description = params[:newcandidate][:description]
68     
69     @candidate.save
70     election.candidates << @candidate
71     render :partial => 'candidate_line'
72   end
73   
74   def delete_candidate
75     candidate = Candidate.find( params[:id] )
76     candidate.destroy
77   end
78
79   def edit_candidates
80     @election = Election.find( params[:id] )
81   end
82
83   ## methods for displaying, adding, deleting, and manipulating voters
84   ## for a particular election
85   ####################################################################
86   def new_voters
87     @election = Election.find( params[:id] )
88     if params.has_key?[:raw_voter_list]
89       process_incoming_voters( params[:raw_voter_list] )
90     end
91     @raw_voter_list = RawVoterList.new
92
93   end
94   
95   def edit_voters
96     @election = Election.find( params[:id] )
97     if params.has_key?( :raw_voter_list )
98       process_incoming_voters( params[:raw_voter_list] )
99     end
100
101     @raw_voter_list = RawVoterList.new
102   end
103   
104   def delete_voter
105     voter = Voter.find( params[:id] )
106     voter.destroy
107   end
108   
109   ## methods for computing and printing results
110   ####################################################################
111   def results
112     @election = Election.find( params[:id] )
113     votes = []
114
115     @election.voters.each do |voter|
116       if voter.vote and voter.vote.confirmed?
117         votes << voter.vote.rankings.sort.collect {|vote| vote.candidate_id}
118       end
119     end
120     
121     @voteobj = CloneproofSSDVote.new(votes)
122     @resultobj = @voteobj.result
123     @winners = @resultobj.winners
124     
125     @candidates_by_id = {}
126     @election.candidates.each {|cand| @candidates_by_id[cand.id] = cand}
127   end
128   
129   def detailed_results
130    
131     self.results
132
133     @voter_list = []
134     @vote_list = []
135     @election.voters. each do |voter|
136       if voter.vote and voter.vote.confirmed?
137         @voter_list << voter.email
138         @vote_list << voter.vote
139       end
140     end
141
142     @vote_list.sort!
143     @vote_list.sort! { |a,b| a.token <=> b.token }
144   end
145
146   ## private methods
147   ####################################################################
148   private
149
150     def process_incoming_voters(raw_voter_list)
151       incoming_voters = RawVoterList.new( raw_voter_list )
152
153       unless incoming_voters.entries.empty?
154         incoming_voters.each do |new_voter|
155
156           if incoming_voters.email == 0
157             new_voter.contacted = 1
158           elsif incoming_voters.email == 1
159             email_voter( new_voter )
160             new_voter.contacted = 1
161           else
162             new_voter.contacted = 0
163           end
164         
165           # the new voter should be in good shape. save add to the election
166           new_voter.save
167           @election.voters << new_voter
168         end
169       end
170  
171       # reset the next time to have a the same default value for emailing
172       @raw_voter_list = RawVoterList.new
173       @raw_voter_list.email = incoming_voters.email
174     end
175
176     def email_voter
177     end
178
179 end

Benjamin Mako Hill || Want to submit a patch?