1 class Vote < ActiveRecord::Base
2 # relationships to other classes
8 after_update :save_rankings
9 before_destroy :destroy_rankings
16 self.votes.each {|vote| yield vote}
24 @votes = rankings.sort.collect { |ranking| ranking.candidate.id }
37 self.votes.each_with_index do |candidate_id, index|
40 ranking.candidate = Candidate.find(candidate_id)
41 self.rankings << ranking
51 rankings.each { |ranking| ranking.destroy }
63 unless self.voter.election.quickvote?
64 token.destroy and token.reload if token
65 self.token = Token.new
75 # create a mapping of candidates ids and the relative order of the
76 # candidates as they appear when sorted alphabetically
78 self.voter.election.candidates.sort.each_with_index do |c, i|
79 cand_relnums[c.id] = i + 1
82 # assemble the votestring
83 self.votes.collect {|v| (cand_relnums[v] + 64).chr}.join("")
86 # the following subroutine is used for quickvotes, but need for elections now
87 # too. It creates a vote with the candidates listed in order of preference
88 # based on alphabetical order. Meant to be manipulated and then confirmed
90 self.votes = self.voter.election.candidates.sort_by { rand }.collect {|c| c.id }