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

Benjamin Mako Hill || Want to submit a patch?