aaa3d9fe7f8924cddde4edd16c5e6c4178b488c4
[selectricity-live] / app / controllers / election_controller.rb
1 class ElectionController < ApplicationController
2   model :raw_voter_list, :voter, :vote, :candidate
3
4   ## general methods for dealing with elections
5   ####################################################################
6   def index
7     list
8     render :action => 'list'
9   end
10
11   def list
12     @election_pages, @elections = paginate :elections, :per_page => 10
13   end
14
15   ## methods for displaying, creating,
16   ## and manipulating election overview data
17   ####################################################################
18
19   def show
20     @election = Election.find(params[:id])
21   end
22
23   def new
24     @election = Election.new
25   end
26   
27   def edit
28     @election = Election.find(params[:id])
29   end
30
31   def create_election
32     @election = Election.new(params[:election])
33     if @election.save
34       flash[:notice] = 'Election was successfully created.'
35       redirect_to :action => 'new_candidates', :id => @election.id
36     else
37       render :action => 'new'
38     end
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] 
67     @candidate.save
68     election.candidates << @candidate
69     render :partial => 'candidate_line'
70   end
71   
72   def delete_candidate
73     candidate = Candidate.find( params[:id] )
74     candidate.destroy
75   end
76
77   def edit_candidates
78     @election = Election.find( params[:id] )
79   end
80
81   ## methods for displaying, adding, deleting, and manipulating voters
82   ## for a particular election
83   ####################################################################
84   def new_voters
85     @election = Election.find( params[:id] )
86     if params.has_key?[:raw_voter_list]
87       process_incoming_voters( params[:raw_voter_list] )
88     end
89     @raw_voter_list = RawVoterList.new
90
91   end
92   
93   def edit_voters
94     @election = Election.find( params[:id] )
95     if params.has_key?( :raw_voter_list )
96       process_incoming_voters( params[:raw_voter_list] )
97     end
98
99     @raw_voter_list = RawVoterList.new
100   end
101   
102   def delete_voter
103     voter = Voter.find( params[:id] )
104     voter.destroy
105   end
106   
107   ## methods for computing and printing results
108   ####################################################################
109   def results
110     @election = Election.find( params[:id] )
111     votes = []
112
113     @election.voters.each do |voter|
114       if voter.vote and voter.vote.confirmed?
115         votes << voter.vote.rankings.sort.collect {|vote| vote.candidate_id}
116       end
117     end
118     
119     @voteobj = CloneproofSSDVote.new(votes)
120     @resultobj = @voteobj.result
121     @winners = @resultobj.winners
122     
123     @candidates_by_id = {}
124     @election.candidates.each {|cand| @candidates_by_id[cand.id] = cand}
125   end
126   
127   def detailed_results
128    
129     self.results
130
131     @voter_list = []
132     @vote_list = []
133     @election.voters. each do |voter|
134       if voter.vote and voter.vote.confirmed?
135         @voter_list << voter.email
136         @vote_list << voter.vote
137       end
138     end
139
140     @vote_list.sort!
141     @vote_list.sort! { |a,b| a.token <=> b.token }
142   end
143
144   ## private methods
145   ####################################################################
146   private
147
148     def process_incoming_voters(raw_voter_list)
149       incoming_voters = RawVoterList.new( raw_voter_list )
150
151       unless incoming_voters.entries.empty?
152         incoming_voters.each do |new_voter|
153
154           if incoming_voters.email == 0
155             new_voter.contacted = 1
156           elsif incoming_voters.email == 1
157             email_voter( new_voter )
158             new_voter.contacted = 1
159           else
160             new_voter.contacted = 0
161           end
162         
163           # the new voter should be in good shape. save add to the election
164           new_voter.save
165           @election.voters << new_voter
166         end
167       end
168  
169       # reset the next time to have a the same default value for emailing
170       @raw_voter_list = RawVoterList.new
171       @raw_voter_list.email = incoming_voters.email
172     end
173
174     def email_voter
175     end
176
177 end

Benjamin Mako Hill || Want to submit a patch?