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: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Affero General Public License for more details.
15 # You should have received a copy of the GNU Affero General Public
16 # License along with this program. If not, see
17 # <http://www.gnu.org/licenses/>.
19 class Election < ActiveRecord::Base
24 validates_presence_of :name, :description
26 #validate that method is one of the listed election types
28 attr_reader :plurality_result
29 attr_reader :approval_result
30 attr_reader :condorcet_result
31 attr_reader :ssd_result
32 attr_reader :borda_result
36 def initialize(params={})
38 self.enddate = read_attribute( :enddate ) || \
39 Time.now + 30.days - 1.second
44 @other_methods = ELECTION_TYPES.keys.reject {|i| i == election_method}
52 read_attribute( :startdate ) || Time.now
57 self.voters.each do |voter|
64 self.candidates.each do |candidate|
72 if self.candidates.length <= 1
73 reasons << "You must have at least two candidates."
76 if self.voters.length <= 1
77 reasons << "You must have at least two voters."
89 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