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
74 def votestring=(string="")
75 candidate_ids = voter.election.candidates.sort.collect \
76 { |candidate| candidate.id.to_i }
78 rel_votes = string.split("").collect { |vote| vote.to_i }
80 # covert relative orders to absolute candidate ids
81 self.votes = rel_votes.collect { |vote| candidate_ids[ vote - 1 ] }
85 # create a mapping of candidates ids and the relative order of the
86 # candidates as they appear when sorted alphabetically
88 self.voter.election.candidates.sort.each_with_index do |c, i|
89 cand_relnums[c.id] = i + 1
92 # assemble the votestring
93 self.votes.collect {|v| cand_relnums[v]}.join("")
96 # the following subroutine is used for quickvotes, but need for elections now
97 # too. It creates a vote with the candidates listed in order of preference
98 # based on alphabetical order. It is meant to be manipulated and then confirmed
100 self.votes = voter.election.candidates.sort.collect {|c| c.id }