1 class QuickvoteController < ApplicationController
8 #############################################################
9 # the following methods pertain to creating quickvotes
10 #############################################################
14 @quickvote = QuickVote.new(params[:quickvote])
16 # store the candidate grabbed through ajax and stored in flash
17 @quickvote.candidatelist = flash[:candlist]
19 # try to save, if it fails, show the page again (the flash should
22 @quickvote = @quickvote.reload
23 render :action => 'success'
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)
37 candidate_name = CGI.escapeHTML(params[:ajax][:newcandidate])
38 unless candidate_name.strip.empty?
39 if flash.has_key?(:candlist) and flash[:candlist].instance_of?(Array)
40 flash[:candlist] << candidate_name
42 flash[:candlist] = [ candidate_name ]
46 render_partial 'candidate_list'
49 #############################################################
50 # the following methods pertain to *voting* in the quickvotes
51 #############################################################
54 @election = QuickVote.ident_to_quickvote(params[:ident])
56 # if the person has specified an election, we show them the voting
57 # page. otherwise, we redirect back to main the page
60 # look to see that the voter has been created and has voted in
61 # this election, and has confirmed their vote
62 @voter = QuickVoter.find_all(["session_id = ? and election_id = ?",
63 session.session_id, @election.id])[0]
65 # if the voter has not voted we destroy them
66 if @voter and not @voter.voted?
71 # if the voter does not exist or has has been destroyed, lets
74 # create a new voter and populate it
75 @voter = QuickVoter.new
76 @voter.election = @election
77 @voter.session_id = session.session_id
79 # create new vote and make it the defaulted sorted list
80 @voter.vote = Vote.new
82 @voter.vote.set_defaults!
86 redirect_to :controller => 'site'
91 # we need the election to verify that we have the right voter
92 election = QuickVote.ident_to_quickvote(params[:ident])
94 # find out who the voter is for this election
95 @voter = QuickVoter.find_all(["session_id = ? and election_id = ?",
96 session.session_id, election.id])[0]
99 # we have not seen this voter before. something is wrong, try
101 redirect_to quickvote_url( :ident => params[:ident] )
104 # this person has already voted, we try again
105 flash[:notice] = "You have already voted!"
106 redirect_to quickvote_url( :ident => params[:ident] )
109 # record the ip address for posterity
110 @voter.ipaddress = request.env["REMOTE_ADDR"]
113 # save the time the vote was made for statistical use
114 @voter.vote.time = Time.now
116 # toggle the confirmation bit
120 render :action => 'thanks'
125 voter = QuickVoter.find_all(["session_id = ?", session.session_id])[0]
127 redirect_to quickvote_url( :ident => params[:ident] )
131 @vote = Vote.find(params[:id])
133 @vote.rankings.each do |ranking|
134 ranking.rank = params['rankings-list'].index(ranking.candidate.id.to_s) + 1
137 render :nothing => true
141 ###############################################################
142 # the following method pertains to displaying the results of a
144 ###############################################################
147 @election = QuickVote.ident_to_quickvote(params[:ident])
150 @election.candidates.each {|c| @candidates[c.id] = c}