]> projects.mako.cc - selectricity-live/blob - app/models/quick_vote.rb
HTML escape description to prevent code injection onto page
[selectricity-live] / 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     if name =~ /^[0-9]+$/
21       errors.add(:name, "must not be a number")
22     end
23     
24     if name =~ /^(create|index|confirm|change|results)$/
25       errors.add(:name, " is a reserved word.")
26     end
27   end
28   
29   def initialize(params={})
30     super
31     self.startdate = Time.now
32     self.enddate =  Time.now + 30.days
33     self.active = 1
34     self.anonymous = 1
35   end
36
37   def candidatelist=(candlist)
38     @raw_candidates = candlist
39   end
40
41   def name
42     read_attribute( :name ).downcase()
43   end
44
45   def reviewed?
46     reviewed.to_i == 1
47   end
48
49   def create_candidates
50     return unless errors.empty?
51     @raw_candidates.each do |name|
52       candidate = Candidate.new({:name => name})
53       self.candidates << candidate
54     end
55   end
56
57   #Calculate Election Results
58   def results
59     # initalize the tallies to empty arrays
60     preference_tally = Array.new
61     plurality_tally = Array.new
62     approval_tally = Array.new
63
64     self.voters.each do |voter|
65       # skip if the voter has not voted or has an unconfirmed vote
66       next unless voter.voted?
67
68       plurality_tally << voter.vote.rankings.sort[0].candidate.id
69       approval_tally << voter.vote.rankings.sort[0..1].collect \
70         { |ranking| ranking.candidate.id }
71       preference_tally << voter.vote.rankings.sort.collect \
72         { |ranking| ranking.candidate.id }
73     end
74     @plurality_result = PluralityVote.new(plurality_tally).result
75     @approval_result = ApprovalVote.new(approval_tally).result
76     @condorcet_result = PureCondorcetVote.new(preference_tally).result
77     @ssd_result = CloneproofSSDVote.new(preference_tally).result
78     @borda_result = BordaVote.new(preference_tally).result
79     #@runoff_result = InstantRunoffVote.new(preference_tally).result
80     #@runoff_results = PluralityVote.new(preference_tally).result
81
82   end
83
84   ### Convert a shortname or id into a QuickVote
85   def self.ident_to_quickvote(ident)
86     return nil unless ident
87     if ident.match(/^\d+$/)
88       quickvote = QuickVote.find(ident)
89     else
90       quickvote = QuickVote.find_all(["name = ?", ident])[0]
91     end
92
93     return quickvote
94   end
95 end

Benjamin Mako Hill || Want to submit a patch?