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 if name =~ /^(create|index|confirm|change|results)$/
22 errors.add(:name, " is a reserved word.")
26 def initialize(params={})
28 self.startdate = Time.now
29 self.enddate = Time.now + 30.days
34 def candidatelist=(candlist)
35 @raw_candidates = candlist
39 read_attribute( :name ).downcase()
47 return unless errors.empty?
48 @raw_candidates.each do |name|
49 candidate = Candidate.new({:name => name})
50 self.candidates << candidate
54 #Calculate Election Results
56 # initalize the tallies to empty arrays
57 preference_tally = Array.new
58 plurality_tally = Array.new
59 approval_tally = Array.new
61 self.voters.each do |voter|
62 # skip if the voter has not voted or has an unconfirmed vote
63 next unless voter.voted?
65 plurality_tally << voter.vote.rankings.sort[0].candidate.id
66 approval_tally << voter.vote.rankings.sort[0..1].collect \
67 { |ranking| ranking.candidate.id }
68 preference_tally << voter.vote.rankings.sort.collect \
69 { |ranking| ranking.candidate.id }
71 @plurality_result = PluralityVote.new(plurality_tally).result
72 @approval_result = ApprovalVote.new(approval_tally).result
73 @condorcet_result = PureCondorcetVote.new(preference_tally).result
74 @ssd_result = CloneproofSSDVote.new(preference_tally).result
75 @borda_result = BordaVote.new(preference_tally).result
76 #@runoff_result = InstantRunoffVote.new(preference_tally).result
77 #@runoff_results = PluralityVote.new(preference_tally).result
81 ### Convert a shortname or id into a QuickVote
82 def self.ident_to_quickvote(ident)
83 return nil unless ident
84 if ident.match(/^\d+$/)
85 quickvote = QuickVote.find(ident)
87 quickvote = QuickVote.find_all(["name = ?", ident])[0]