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 @raw_candidates.each do |c|
19 unless c.instance_of? String
20 errors.add(nil, "Candidates must be strings")
25 errors.add(nil, "Candidate name must not be empty")
28 end if @raw_candidates
30 errors.add(nil, "Candidates must all be unique") if @raw_candidates and @raw_candidates.uniq!
32 if name =~ /[^A-Za-z0-9]/
33 errors.add(:name, "must only include numbers and letters.")
36 errors.add(:name, "must not be a number")
39 if name =~ /^(create|index|confirm|change|results)$/
40 errors.add(:name, " is a reserved word.")
44 def initialize(params={})
46 self.startdate = Time.now
47 self.enddate = Time.now + 30.days
52 def candidatelist=(candlist)
53 @raw_candidates = candlist
57 read_attribute( :name ).downcase() if read_attribute( :name )
65 return unless errors.empty?
66 @raw_candidates.each do |name|
67 candidate = Candidate.new({:name => name})
68 self.candidates << candidate
72 #Calculate Election Results
74 # initalize the tallies to empty arrays
75 preference_tally = Array.new
76 plurality_tally = Array.new
77 approval_tally = Array.new
79 self.voters.each do |voter|
80 # skip if the voter has not voted or has an unconfirmed vote
81 next unless voter.voted?
83 plurality_tally << voter.vote.rankings.sort[0].candidate.id
84 approval_tally << voter.vote.rankings.sort[0..1].collect \
85 { |ranking| ranking.candidate.id }
86 preference_tally << voter.vote.rankings.sort.collect \
87 { |ranking| ranking.candidate.id }
89 @plurality_result = PluralityVote.new(plurality_tally).result
90 @approval_result = ApprovalVote.new(approval_tally).result
91 @condorcet_result = PureCondorcetVote.new(preference_tally).result
92 @ssd_result = CloneproofSSDVote.new(preference_tally).result
93 @borda_result = BordaVote.new(preference_tally).result
94 #@runoff_result = InstantRunoffVote.new(preference_tally).result
95 #@runoff_results = PluralityVote.new(preference_tally).result
99 ### Convert a shortname or id into a QuickVote
100 def self.ident_to_quickvote(ident)
101 return nil unless ident
102 if ident.match(/^\d+$/)
103 quickvote = QuickVote.find(ident)
105 quickvote = QuickVote.find(:all, :conditions => ["name = ?", ident])[0]