1 class QuickVote < Election
2 after_validation :create_candidates
3 validates_uniqueness_of :name
4 attr_accessor :raw_candidates
5 attr_accessor :reviewed
6 attr_accessor :plurality_result
7 attr_accessor :approval_result
8 attr_accessor :condorcet_result
9 attr_accessor :ssd_result
10 attr_accessor :borda_result
13 if @raw_candidates.length < 2
14 errors.add("You must list at least two candidates.")
17 if name =~ /[^A-Za-z0-9]/
18 errors.add("The name must only include numbers and letters.")
22 def initialize(params={})
24 self.startdate = Time.now
25 self.enddate = Time.now + 30.days
30 def candidatelist=(candlist)
31 @raw_candidates = candlist
35 read_attribute( :name ).downcase()
43 @raw_candidates.each do |name|
44 candidate = Candidate.new({:name => name})
45 self.candidates << candidate
49 #Calculate Election Results
51 # initalize the tallies to empty arrays
52 preference_tally = Array.new
53 plurality_tally = Array.new
54 approval_tally = Array.new
56 self.voters.each do |voter|
57 # skip if the voter has not voted or has an unconfirmed vote
58 next unless voter.voted?
60 plurality_tally << voter.vote.rankings.sort[0].candidate.id
61 approval_tally << voter.vote.rankings.sort[0..1].collect \
62 { |ranking| ranking.candidate.id }
63 preference_tally << voter.vote.rankings.sort.collect \
64 { |ranking| ranking.candidate.id }
67 @plurality_result = PluralityVote.new(plurality_tally).result
68 @approval_result = ApprovalVote.new(approval_tally).result
69 @condorcet_result = PureCondorcetVote.new(preference_tally).result
70 @ssd_result = CloneproofSSDVote.new(preference_tally).result
71 @borda_result = BordaVote.new(preference_tally).result
72 #@runoff_result = InstantRunoffVote.new(preference_tally).result
73 #@runoff_results = PluralityVote.new(preference_tally).result
78 ### Convert a shortname or id into a QuickVote
79 def self.ident_to_quickvote(ident)
80 if ident.match(/^\d+$/)
81 quickvote = QuickVote.find(ident)
83 quickvote = QuickVote.find_all(["name = ?", ident])[0]