1 # Selectricity: Voting Machinery for the Masses
2 # Copyright (C) 2007, 2008 Benjamin Mako Hill <mako@atdot.cc>
3 # Copyright (C) 2007 Massachusetts Institute of Technology
5 # This program is free software. Please see the COPYING file for
8 class Vote < ActiveRecord::Base
9 # relationships to other classes
15 after_update :save_rankings
16 before_destroy :destroy_rankings
23 self.votes.each {|vote| yield vote}
31 @votes = self.rankings.sort.collect { |ranking| ranking.candidate.id }
43 self.votes # i need to initalize this before destroying rankings
44 # or else the ranks themselves show up as nil
47 self.votes.each_with_index do |candidate_id, index|
50 ranking.candidate = Candidate.find(candidate_id)
51 self.rankings << ranking
61 rankings.each { |ranking| ranking.destroy }
65 if self.voter.election.candidates.length == self.rankings.length
70 unless self.voter.election.quickvote?
71 token.destroy and token.reload if token
72 self.token = Token.new
86 # create a mapping of candidates ids and the relative order of the
87 # candidates as they appear when sorted alphabetically
89 self.voter.election.candidates.sort.each_with_index do |c, i|
90 cand_relnums[c.id] = i + 1
93 # assemble the votestring
94 self.votes.collect {|v| (cand_relnums[v] + 64).chr}.join("")
97 # the following subroutine is used for quickvotes, but need for elections now
98 # too. It creates a vote with the candidates listed in order of preference
99 # based on alphabetical order. Meant to be manipulated and then confirmed
101 self.votes = self.voter.election.candidates.sort_by { rand }.collect {|c| c.id }