Merge from jdong; all testcases should succeed at this point, and all deprecations...
[selectricity-live] / app / models / quick_vote.rb
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
12
13   def validate
14     if not @raw_candidates or @raw_candidates.length < 2
15       errors.add(nil, "You must list at least two candidates.")
16     end
17     
18     @raw_candidates.each do |c|
19       unless c.instance_of? String
20         errors.add(nil, "Candidates must be strings")
21         next
22       end
23       c.strip!
24       if c.length == 0
25         errors.add(nil, "Candidate name must not be empty")
26         next
27       end
28     end if @raw_candidates
29
30     errors.add(nil, "Candidates must all be unique") if @raw_candidates and @raw_candidates.uniq!
31
32     if name =~ /[^A-Za-z0-9]/
33       errors.add(:name, "must only include numbers and letters.")
34     end
35     if name =~ /^[0-9]+$/
36       errors.add(:name, "must not be a number")
37     end
38     
39     if name =~ /^(create|index|confirm|change|results)$/
40       errors.add(:name, " is a reserved word.")
41     end
42   end
43   
44   def initialize(params={})
45     super
46     self.startdate = Time.now
47     self.enddate =  Time.now + 30.days
48     self.active = 1
49     self.anonymous = 1
50   end
51
52   def candidatelist=(candlist)
53     @raw_candidates = candlist
54   end
55
56   def name
57     read_attribute( :name ).downcase() if read_attribute( :name )
58   end
59
60   def reviewed?
61     reviewed.to_i == 1
62   end
63
64   def create_candidates
65     return unless errors.empty?
66     @raw_candidates.each do |name|
67       candidate = Candidate.new({:name => name})
68       self.candidates << candidate
69     end
70   end
71
72   #Calculate Election Results
73   def results
74     # initalize the tallies to empty arrays
75     preference_tally = Array.new
76     plurality_tally = Array.new
77     approval_tally = Array.new
78
79     self.voters.each do |voter|
80       # skip if the voter has not voted or has an unconfirmed vote
81       next unless voter.voted?
82
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 }
88     end
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
96
97   end
98
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)
104     else
105       quickvote = QuickVote.find(:all, :conditions => ["name = ?", ident])[0]
106     end
107
108     return quickvote
109   end
110 end

Benjamin Mako Hill || Want to submit a patch?