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 not @raw_candidates or @raw_candidates.length < 2
14 errors.add(nil, "You must list at least two candidates.")
17 if name =~ /[^A-Za-z0-9]/
18 errors.add(:name, "must only include numbers and letters.")
21 errors.add(:name, "must not be a number")
24 if name =~ /^(create|index|confirm|change|results)$/
25 errors.add(:name, " is a reserved word.")
29 def initialize(params={})
31 self.startdate = Time.now
32 self.enddate = Time.now + 30.days
37 def candidatelist=(candlist)
38 @raw_candidates = candlist
42 read_attribute( :name ).downcase()
50 return unless errors.empty?
51 @raw_candidates.each do |name|
52 candidate = Candidate.new({:name => name})
53 self.candidates << candidate
57 #Calculate Election Results
59 # initalize the tallies to empty arrays
60 preference_tally = Array.new
61 plurality_tally = Array.new
62 approval_tally = Array.new
64 self.voters.each do |voter|
65 # skip if the voter has not voted or has an unconfirmed vote
66 next unless voter.voted?
68 plurality_tally << voter.vote.rankings.sort[0].candidate.id
69 approval_tally << voter.vote.rankings.sort[0..1].collect \
70 { |ranking| ranking.candidate.id }
71 preference_tally << voter.vote.rankings.sort.collect \
72 { |ranking| ranking.candidate.id }
74 @plurality_result = PluralityVote.new(plurality_tally).result
75 @approval_result = ApprovalVote.new(approval_tally).result
76 @condorcet_result = PureCondorcetVote.new(preference_tally).result
77 @ssd_result = CloneproofSSDVote.new(preference_tally).result
78 @borda_result = BordaVote.new(preference_tally).result
79 #@runoff_result = InstantRunoffVote.new(preference_tally).result
80 #@runoff_results = PluralityVote.new(preference_tally).result
84 ### Convert a shortname or id into a QuickVote
85 def self.ident_to_quickvote(ident)
86 return nil unless ident
87 if ident.match(/^\d+$/)
88 quickvote = QuickVote.find(ident)
90 quickvote = QuickVote.find_all(["name = ?", ident])[0]