1 # Selectricity: Voting Machinery for the Masses
2 # Copyright (C) 2007, 2008 Benjamin Mako Hill <mako@atdot.cc>
3 # Copyright (C) 2007 Massachusetts Institute of Technology
5 # This program is free software. Please see the COPYING file for
8 class Election < ActiveRecord::Base
13 validates_presence_of :name, :description
15 # enforce constraints associated with dependencies (i.e., a kiosk
16 # election can't also be unauthenticated)
17 before_save :enforce_constraints
19 #validate that method is one of the listed election types
20 attr_reader :plurality_result
21 attr_reader :approval_result
22 attr_reader :condorcet_result
23 attr_reader :ssd_result
24 attr_reader :borda_result
28 def initialize(params={})
30 self.enddate = read_attribute( :enddate ) || \
31 Time.now + 30.days - 1.second
36 @other_methods = ELECTION_TYPES.keys.reject {|i| i == election_method}
44 read_attribute( :startdate ) || Time.now
49 self.voters.each do |voter|
56 self.candidates.each do |candidate|
64 if self.candidates.length <= 1
65 reasons << "You must have at least two candidates."
68 if self.voters.length <= 1 and self.authenticated?
69 reasons << "You must have at least two voters."
81 self.class == QuickVote
101 shortdesc = description.split(/\n/)[0]
105 longdesc = description.split(/\n/)[1..-1].join("")
106 longdesc.length > 0 ? longdesc : nil
109 #Calculate results if not in memcache
111 # Assignment is intentional
112 if Cache and c = Cache.get("election_results:#{id}:#{self.votes.length}")
113 @plurality_result = c['plurality']
114 @approval_result = c['approval']
115 @condorcet_result = c['condorcet']
116 @ssd_result = c['ssd']
117 @borda_result = c['borda']
120 # memcache is available, but missed.
121 results = self.results!
122 Cache.set("election_results:#{id}:#{self.votes.length}", results)
129 #Always Calculate Election Results
131 # initalize the tallies to empty arrays
132 preference_tally = Array.new
133 plurality_tally = Array.new
134 approval_tally = Array.new
136 self.voters.each do |voter|
137 # skip if the voter has not voted or has an unconfirmed vote
138 next unless voter.voted?
140 plurality_tally << voter.vote.rankings.sort[0].candidate.id
141 approval_tally << voter.vote.rankings.sort[0..1].collect \
142 { |ranking| ranking.candidate.id }
143 preference_tally << voter.vote.rankings.sort.collect \
144 { |ranking| ranking.candidate.id }
147 @plurality_result = PluralityVote.new(plurality_tally).result
148 @approval_result = ApprovalVote.new(approval_tally).result
149 @condorcet_result = PureCondorcetVote.new(preference_tally).result
150 @ssd_result = CloneproofSSDVote.new(preference_tally).result
151 @borda_result = BordaVote.new(preference_tally).result
153 { 'plurality' => @plurality_result,
154 'approval' => @approval_result,
155 'condorcet' => @condorcet_result,
156 'ssd' => @ssd_result,
157 'borda' => @borda_result }
163 competitors = self.candidates.sort.collect {|candidate| candidate.id}
164 competitors.each do |candidate|
165 names[candidate] = Candidate.find(candidate).name
173 self.candidates.each {|c| hash[c.id] = c}
178 # TODO now that this code is in here, we should go ahead and remove
179 # date checking from other places in the code
181 if self.active < 2 and self.enddate < Time.now
188 def enforce_constraints
189 # kiosks can't be authenticated
190 self.authenticated = false if kiosk?