]> projects.mako.cc - selectricity/blob - app/controllers/quickvote_controller.rb
97f4ab05fb726bd0a03cdce10ee88c025cbbfd98
[selectricity] / 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     if @election
11       @voter = QuickVoter.find_all(["session_id = ? and election_id = ?",
12                                   session.session_id, @election.id])[0]
13       unless @voter 
14         @voter = QuickVoter.new
15         @voter.election = QuickVote.find_all( [ "name = ?", params[:votename] ] )[0]
16       end
17     else
18       redirect_to :controller => 'site'
19     end
20   end
21
22   def create
23     if params[:quickvote] 
24       @quickvote = QuickVote.new(params[:quickvote])
25
26       # store the candidate grabbed through ajax and stored in flash
27       @quickvote.candidatelist = flash[:candlist]
28
29       # try to save, if it fails, show the page again (the flash should
30       # still be intact
31       if @quickvote.save
32         @quickvote = @quickvote.reload
33         render :action => 'success'
34       else
35         flash.keep(:candlist)
36       end 
37     else
38       # if we don't have a quickvote param, it means that the person
39       # here has not been hitting this page and we can clear any
40       # candlist in the flash
41       flash.delete(:candlist) if flash.has_key?(:candlist)
42     end
43   end
44
45   def add_candidate
46     candidate_name = params[:ajax][:newcandidate]
47     if flash.has_key?(:candlist) and flash[:candlist].instance_of?(Array) 
48       flash[:candlist] << candidate_name
49     else
50       flash[:candlist] = [ candidate_name ]
51     end
52     flash.keep(:candlist)
53     render_partial 'candidate_list'
54   end
55
56   def change
57     voter = QuickVoter.find_all(["session_id = ?", session.session_id])[0]
58     voter.destroy
59     redirect_to quickvote_url( :votename => params[:votename] )
60   end
61
62   def confirm
63     election = QuickVote.find_all(["name = ?", params[:votename]])[0]
64
65     if QuickVoter.find_all(["session_id = ? and election_id = ?", 
66                             session.session_id, election.id])[0]
67       flash[:notice] = "You have already voted!"
68       redirect_to quickvote_url( :votename => params[:votename] )
69     else
70       @voter = QuickVoter.new()
71       @voter.election = election
72       @voter.session_id = session.session_id
73       @voter.ipaddress = request.env["REMOTE_ADDR"]
74       @voter.save
75       @voter.reload
76         
77       @voter.vote = Vote.new
78       @voter.vote.votestring = params[:vote][:votestring]
79       @voter.vote.confirm!
80       render :action => 'thanks'
81     end
82   end
83   
84   def results
85     @election = QuickVote.find_all( ["name = ?", params[:votename]] )[0]
86
87     preference_tally = []
88     plurality_tally = []
89     approval_tally = []
90     @election.voters.each do |voter|
91       plurality_tally << voter.vote.rankings.sort[0].candidate.id
92       approval_tally << voter.vote.rankings.sort[0..1].collect {|ranking| ranking.candidate.id}
93       preference_tally << voter.vote.rankings.sort.collect {|ranking| ranking.candidate.id}
94     end
95  
96     @plurality_result = PluralityVote.new(plurality_tally).result
97     @approval_result = ApprovalVote.new(approval_tally).result
98     @condorcet_result = CloneproofSSDVote.new(preference_tally).result
99     @ssd_result = PureCondorcetVote.new(preference_tally).result
100     @borda_result = BordaVote.new(preference_tally).result
101     @runoff_result = InstantRunoffVote.new(preference_tally).result
102
103     @candidates = {} 
104     @election.candidates.each {|c| @candidates[c.id] = c}
105   end
106
107 end

Benjamin Mako Hill || Want to submit a patch?