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 }
58 unless self.voter.election.quickvote?
59 token.destroy and token.reload if token
60 self.token = Token.new
69 def votestring=(string="")
70 candidate_ids = voter.election.candidates.sort.collect \
71 { |candidate| candidate.id.to_i }
73 rel_votes = string.split("").collect { |vote| vote.to_i }
75 # covert relative orders to absolute candidate ids
76 self.votes = rel_votes.collect { |vote| candidate_ids[ vote - 1 ] }
80 # create a mapping of candidates ids and the relative order of the
81 # candidates as they appear when sorted alphabetically
83 self.voter.election.candidates.sort.each_with_index do |c, i|
84 cand_relnums[c.id] = i + 1
87 # assemble the votestring
88 self.votes.collect {|v| cand_relnums[v]}.join("")
91 # the following subroutine is used for quickvotes. it creates a vote
92 # with the candidates listed in order of preference based on
93 # alphabetical order. it is meant to be manipulated and then confirmed
95 self.votes = voter.election.candidates.sort.collect {|c| c.id }