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: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Affero General Public License for more details.
15 # You should have received a copy of the GNU Affero General Public
16 # License along with this program. If not, see
17 # <http://www.gnu.org/licenses/>.
19 class Vote < ActiveRecord::Base
20 # relationships to other classes
26 after_update :save_rankings
27 before_destroy :destroy_rankings
34 self.votes.each {|vote| yield vote}
42 @votes = rankings.sort.collect { |ranking| ranking.candidate.id }
55 self.votes.each_with_index do |candidate_id, index|
58 ranking.candidate = Candidate.find(candidate_id)
59 self.rankings << ranking
69 rankings.each { |ranking| ranking.destroy }
77 unless self.voter.election.quickvote?
78 token.destroy and token.reload if token
79 self.token = Token.new
89 # create a mapping of candidates ids and the relative order of the
90 # candidates as they appear when sorted alphabetically
92 self.voter.election.candidates.sort.each_with_index do |c, i|
93 cand_relnums[c.id] = i + 1
96 # assemble the votestring
97 self.votes.collect {|v| (cand_relnums[v] + 64).chr}.join("")
100 # the following subroutine is used for quickvotes, but need for elections now
101 # too. It creates a vote with the candidates listed in order of preference
102 # based on alphabetical order. Meant to be manipulated and then confirmed
104 self.votes = self.voter.election.candidates.sort_by { rand }.collect {|c| c.id }