1 class QuickVote < Election
2 after_validation :create_candidates
3 validates_uniqueness_of :name
4 validates_presence_of :name
5 attr_accessor :raw_candidates
6 attr_accessor :reviewed
7 attr_accessor :plurality_result
8 attr_accessor :approval_result
9 attr_accessor :condorcet_result
10 attr_accessor :ssd_result
11 attr_accessor :borda_result
14 if not @raw_candidates or @raw_candidates.length < 2
15 errors.add(nil, "You must list at least two candidates.")
18 if name =~ /[^A-Za-z0-9]/
19 errors.add(:name, "must only include numbers and letters.")
22 errors.add(:name, "must not be a number")
25 if name =~ /^(create|index|confirm|change|results)$/
26 errors.add(:name, " is a reserved word.")
30 def initialize(params={})
32 self.startdate = Time.now
33 self.enddate = Time.now + 30.days
38 def candidatelist=(candlist)
39 @raw_candidates = candlist
43 read_attribute( :name ).downcase() if read_attribute( :name )
51 return unless errors.empty?
52 @raw_candidates.each do |name|
53 candidate = Candidate.new({:name => name})
54 self.candidates << candidate
58 #Calculate Election Results
60 # initalize the tallies to empty arrays
61 preference_tally = Array.new
62 plurality_tally = Array.new
63 approval_tally = Array.new
65 self.voters.each do |voter|
66 # skip if the voter has not voted or has an unconfirmed vote
67 next unless voter.voted?
69 plurality_tally << voter.vote.rankings.sort[0].candidate.id
70 approval_tally << voter.vote.rankings.sort[0..1].collect \
71 { |ranking| ranking.candidate.id }
72 preference_tally << voter.vote.rankings.sort.collect \
73 { |ranking| ranking.candidate.id }
75 @plurality_result = PluralityVote.new(plurality_tally).result
76 @approval_result = ApprovalVote.new(approval_tally).result
77 @condorcet_result = PureCondorcetVote.new(preference_tally).result
78 @ssd_result = CloneproofSSDVote.new(preference_tally).result
79 @borda_result = BordaVote.new(preference_tally).result
80 #@runoff_result = InstantRunoffVote.new(preference_tally).result
81 #@runoff_results = PluralityVote.new(preference_tally).result
85 ### Convert a shortname or id into a QuickVote
86 def self.ident_to_quickvote(ident)
87 return nil unless ident
88 if ident.match(/^\d+$/)
89 quickvote = QuickVote.find(ident)
91 quickvote = QuickVote.find(:all, :conditions => ["name = ?", ident])[0]