Removed the breakpointer from the quickvote create method.
[selectricity] / app / controllers / quickvote_controller.rb
1 class QuickvoteController < ApplicationController
2   layout 'hc'
3   model :quick_voter
4   model :quick_vote
5   model :vote
6   model :election
7
8   #############################################################
9   # the following methods pertain to creating quickvotes
10   #############################################################
11
12   def create
13     if params[:quickvote] 
14       @quickvote = QuickVote.new(params[:quickvote])
15
16       # store the candidate grabbed through ajax and stored in flash
17       @quickvote.candidatelist = flash[:candlist]
18
19       # try to save, if it fails, show the page again (the flash should
20       # still be intact
21       if @quickvote.save
22         @quickvote = @quickvote.reload
23         render :action => 'success'
24       else
25         flash.keep(:candlist)
26       end 
27
28     else
29       # if we don't have a quickvote param, it means that the person
30       # here has not been hitting this page and we can clear any
31       # candlist in the flash
32       flash.delete(:candlist) if flash.has_key?(:candlist)
33     end
34   end
35
36   def add_candidate
37     candidate_name = params[:ajax][:newcandidate]
38     if flash.has_key?(:candlist) and flash[:candlist].instance_of?(Array) 
39       flash[:candlist] << candidate_name
40     else
41       flash[:candlist] = [ candidate_name ]
42     end
43     flash.keep(:candlist)
44     render_partial 'candidate_list'
45   end
46  
47   #############################################################
48   # the following methods pertain to *voting* in the quickvotes
49   #############################################################
50
51   def index
52     @election = QuickVote.find_all(["name = ?", params[:votename]])[0]
53     
54     # if the person has specified an election, we show them the voting
55     # page. otherwise, we redirect back to main the page
56     if @election
57
58       # look to see that the voter has been created and has voted in
59       # this election, and has confirmed their vote
60       @voter = QuickVoter.find_all(["session_id = ? and election_id = ?",
61                                   session.session_id, @election.id])[0]
62
63       # if the voter has not voted we destroy them
64       if @voter and not @voter.voted?
65         @voter.destroy
66         @voter = nil
67       end
68
69       # if the voter does not exist or has has been destroyed, lets
70       # create a new one
71       unless @voter
72         # create a new voter and populate it
73         @voter = QuickVoter.new
74         @voter.election = QuickVote.find_all( [ "name = ?", params[:votename] ] )[0]
75         @voter.session_id = session.session_id
76
77         # create new vote and make it the defaulted sorted list
78         @voter.vote = Vote.new
79         @voter.save
80         @voter.vote.set_defaults!
81         @voter.reload
82       end
83     else
84       redirect_to :controller => 'site'
85     end
86   end
87
88   def confirm
89     # we need the election to verify that we have the right voter
90     election = QuickVote.find_all( [ "name = ?", params[:votename] ] )[0]
91
92     # find out who the voter is for this election
93     @voter = QuickVoter.find_all(["session_id = ? and election_id = ?", 
94                                   session.session_id, election.id])[0]
95
96     if not @voter
97       # we have not seen this  voter before. something is wrong, try
98       # again
99       redirect_to quickvote_url( :votename => params[:votename] ) 
100       
101     elsif @voter.voted?
102       # this person has already voted, we try again
103       flash[:notice] = "You have already voted!"
104       redirect_to quickvote_url( :votename => params[:votename] )
105       
106     else
107       # record the ip address for posterity
108       @voter.ipaddress = request.env["HTTP_X_FORWARDED_FOR"]
109       @voter.save
110       
111       # toggle the confirmation bit
112       @voter.vote.confirm!
113       @voter.reload
114       render :action => 'thanks'
115     end
116   end
117  
118   def change
119     voter = QuickVoter.find_all(["session_id = ?", session.session_id])[0]
120     voter.destroy
121     redirect_to quickvote_url( :votename => params[:votename] )
122   end
123
124   def sort_candidates
125     @vote = Vote.find(params[:id])
126
127     @vote.rankings.each do |ranking|
128       ranking.rank = params['rankings-list'].index(ranking.candidate.id.to_s) + 1
129       ranking.save
130     end
131     render :nothing => true
132   end
133                 
134
135   ###############################################################
136   # the following method pertains to displaying the results of a
137   # quickvote
138   ###############################################################
139
140   def results
141     @election = QuickVote.find_all( ["name = ?", params[:votename]] )[0]
142
143     # initalize the tallies to empty arrays
144     preference_tally = Array.new
145     plurality_tally = Array.new
146     approval_tally = Array.new
147
148     @election.voters.each do |voter|
149       # skip if the voter has not voted or has an unconfirmed vote
150       next unless voter.voted?
151       
152       plurality_tally << voter.vote.rankings.sort[0].candidate.id
153       approval_tally << voter.vote.rankings.sort[0..1].collect \
154         { |ranking| ranking.candidate.id }
155       preference_tally << voter.vote.rankings.sort.collect \
156         { |ranking| ranking.candidate.id }
157     end
158  
159     @plurality_result = PluralityVote.new(plurality_tally).result
160     @approval_result = ApprovalVote.new(approval_tally).result
161     @condorcet_result = PureCondorcetVote.new(preference_tally).result
162     @ssd_result = CloneproofSSDVote.new(preference_tally).result
163     @borda_result = BordaVote.new(preference_tally).result
164     #@runoff_result = InstantRunoffVote.new(preference_tally).result
165     #@runoff_results = PluralityVote.new(preference_tally).result
166
167
168     @candidates = {} 
169     @election.candidates.each {|c| @candidates[c.id] = c}
170   end
171
172 end

Benjamin Mako Hill || Want to submit a patch?