]> projects.mako.cc - selectricity/blob - app/models/quick_vote.rb
Use WHOIS to search up meaningful organizational data about voters. NOTE: Probably...
[selectricity] / app / models / quick_vote.rb
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
11
12   def validate
13     if not @raw_candidates or @raw_candidates.length < 2
14         errors.add(nil, "You must list at least two candidates.")
15     end
16     
17     if name =~ /[^A-Za-z0-9]/
18       errors.add(:name, "must only include numbers and letters.")
19     end
20     
21     if name =~ /^(create|index|confirm|change|results)$/
22       errors.add(:name, " is a reserved word.")
23     end
24   end
25   
26   def initialize(params={})
27     super
28     self.startdate = Time.now
29     self.enddate =  Time.now + 30.days
30     self.active = 1
31     self.anonymous = 1
32   end
33
34   def candidatelist=(candlist)
35     @raw_candidates = candlist
36   end
37
38   def name
39     read_attribute( :name ).downcase()
40   end
41
42   def reviewed?
43     reviewed.to_i == 1
44   end
45
46   def create_candidates
47     return unless errors.empty?
48     @raw_candidates.each do |name|
49       candidate = Candidate.new({:name => name})
50       self.candidates << candidate
51     end
52   end
53
54   #Calculate Election Results
55   def results
56     # initalize the tallies to empty arrays
57     preference_tally = Array.new
58     plurality_tally = Array.new
59     approval_tally = Array.new
60
61     self.voters.each do |voter|
62       # skip if the voter has not voted or has an unconfirmed vote
63       next unless voter.voted?
64
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 }
70     end
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
78
79   end
80
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)
86     else
87       quickvote = QuickVote.find_all(["name = ?", ident])[0]
88     end
89
90     return quickvote
91   end
92 end

Benjamin Mako Hill || Want to submit a patch?