]> projects.mako.cc - selectricity-live/blob - app/controllers/quickvote_controller.rb
fixed the string modification function and added support for a titlecase method
[selectricity-live] / app / controllers / quickvote_controller.rb
1 class QuickvoteController < ApplicationController
2   layout 'hc'
3   model :quick_voter
4   model :vote
5   model :election
6
7   def index
8     @election = QuickVote.find_all(["name = ?", params[:votename]])[0]
9     
10     @voter = QuickVoter.find_all(["session_id = ? and election_id = ?",
11                                   session.session_id, @election.id])[0]
12     unless @voter 
13       @voter = QuickVoter.new
14       @voter.election = QuickVote.find_all( [ "name = ?", params[:votename] ] )[0]
15     end
16   end
17
18   def create
19     if params[:quickvote] 
20       @quickvote = QuickVote.new(params[:quickvote])
21       if @quickvote.reviewed? and @quickvote.save
22           @quickvote = @quickvote.reload
23           render :action => 'success'
24       end 
25     end
26   end
27
28   def change
29     voter = QuickVoter.find_all(["session_id = ?", session.session_id])[0]
30     voter.destroy
31     redirect_to quickvote_url( :votename => params[:votename] )
32   end
33
34   def confirm
35     election = QuickVote.find_all(["name = ?", params[:votename]])[0]
36
37     if QuickVoter.find_all(["session_id = ? and election_id = ?", 
38                             session.session_id, election.id])[0]
39       flash[:notice] = "You have already voted!"
40       redirect_to quickvote_url( :votename => params[:votename] )
41     else
42       @voter = QuickVoter.new()
43       @voter.election = election
44       @voter.session_id = session.session_id
45       @voter.ipaddress = request.env["REMOTE_ADDR"]
46       @voter.save
47       @voter.reload
48         
49       @voter.vote = Vote.new
50       @voter.vote.votestring = params[:vote][:votestring]
51       @voter.vote.confirm!
52       render :action => 'thanks'
53     end
54   end
55   
56   def results
57     @election = QuickVote.find_all( ["name = ?", params[:votename]] )[0]
58
59     preference_tally = []
60     plurality_tally = []
61     approval_tally = []
62     @election.voters.each do |voter|
63       plurality_tally << voter.vote.rankings.sort[0].candidate.id
64       approval_tally << voter.vote.rankings.sort[0..1].collect {|ranking| ranking.candidate.id}
65       preference_tally << voter.vote.rankings.sort.collect {|ranking| ranking.candidate.id}
66     end
67  
68     @plurality_result = PluralityVote.new(plurality_tally).result
69     @approval_result = ApprovalVote.new(approval_tally).result
70     @condorcet_result = CloneproofSSDVote.new(preference_tally).result
71     @ssd_result = PureCondorcetVote.new(preference_tally).result
72     @borda_result = BordaVote.new(preference_tally).result
73     @runoff_result = InstantRunoffVote.new(preference_tally).result
74
75     @candidates = {} 
76     @election.candidates.each {|c| @candidates[c.id] = c}
77   end
78
79 end

Benjamin Mako Hill || Want to submit a patch?