]> projects.mako.cc - selectricity-live/blob - app/models/quick_vote.rb
Correct a bunch of the testcases. Poor copy-and-paste led to description => nil where...
[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     if name =~ /[^A-Za-z0-9]/
19       errors.add(:name, "must only include numbers and letters.")
20     end
21     if name =~ /^[0-9]+$/
22       errors.add(:name, "must not be a number")
23     end
24     
25     if name =~ /^(create|index|confirm|change|results)$/
26       errors.add(:name, " is a reserved word.")
27     end
28   end
29   
30   def initialize(params={})
31     super
32     self.startdate = Time.now
33     self.enddate =  Time.now + 30.days
34     self.active = 1
35     self.anonymous = 1
36   end
37
38   def candidatelist=(candlist)
39     @raw_candidates = candlist
40   end
41
42   def name
43     read_attribute( :name ).downcase() if read_attribute( :name )
44   end
45
46   def reviewed?
47     reviewed.to_i == 1
48   end
49
50   def create_candidates
51     return unless errors.empty?
52     @raw_candidates.each do |name|
53       candidate = Candidate.new({:name => name})
54       self.candidates << candidate
55     end
56   end
57
58   #Calculate Election Results
59   def results
60     # initalize the tallies to empty arrays
61     preference_tally = Array.new
62     plurality_tally = Array.new
63     approval_tally = Array.new
64
65     self.voters.each do |voter|
66       # skip if the voter has not voted or has an unconfirmed vote
67       next unless voter.voted?
68
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 }
74     end
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
82
83   end
84
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)
90     else
91       quickvote = QuickVote.find_all(["name = ?", ident])[0]
92     end
93
94     return quickvote
95   end
96 end

Benjamin Mako Hill || Want to submit a patch?