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

Benjamin Mako Hill || Want to submit a patch?