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 }
69 unless self.voter.election.quickvote?
70 token.destroy and token.reload if token
71 self.token = Token.new
81 # create a mapping of candidates ids and the relative order of the
82 # candidates as they appear when sorted alphabetically
84 self.voter.election.candidates.sort.each_with_index do |c, i|
85 cand_relnums[c.id] = i + 1
88 # assemble the votestring
89 self.votes.collect {|v| (cand_relnums[v] + 64).chr}.join("")
92 # the following subroutine is used for quickvotes, but need for elections now
93 # too. It creates a vote with the candidates listed in order of preference
94 # based on alphabetical order. Meant to be manipulated and then confirmed
96 self.votes = self.voter.election.candidates.sort_by { rand }.collect {|c| c.id }