1 class Vote < ActiveRecord::Base
2 # relationships to other classes
8 after_update :save_rankings
9 before_destroy :destroy_rankings
18 self.votes.each {|vote| yield vote}
26 @votes = rankings.sort.collect { |ranking| ranking.candidate.id }
39 self.votes.each_with_index do |candidate_id, index|
42 ranking.candidate = Candidate.find(candidate_id)
43 self.rankings << ranking
53 rankings.each { |ranking| ranking.destroy }
65 unless self.voter.election.quickvote?
66 token.destroy and token.reload if token
67 self.token = Token.new
76 def votestring=(string="")
77 candidate_ids = voter.election.candidates.sort.collect \
78 { |candidate| candidate.id.to_i }
80 rel_votes = string.split("").collect { |vote| vote.to_i }
82 # covert relative orders to absolute candidate ids
83 self.votes = rel_votes.collect { |vote| candidate_ids[ vote - 1 ] }
87 # create a mapping of candidates ids and the relative order of the
88 # candidates as they appear when sorted alphabetically
90 self.voter.election.candidates.sort.each_with_index do |c, i|
91 cand_relnums[c.id] = i + 1
94 # assemble the votestring
95 self.votes.collect {|v| cand_relnums[v]}.join("")
98 # the following subroutine is used for quickvotes. it creates a vote
99 # with the candidates listed in order of preference based on
100 # alphabetical order. it is meant to be manipulated and then confirmed
102 self.votes = voter.election.candidates.sort.collect {|c| c.id }