1 class Election < ActiveRecord::Base
6 validates_presence_of :name, :description
8 #validate that method is one of the listed election types
10 attr_reader :plurality_result
11 attr_reader :approval_result
12 attr_reader :condorcet_result
13 attr_reader :ssd_result
14 attr_reader :borda_result
18 def initialize(params={})
20 self.enddate = read_attribute( :enddate ) || \
21 Time.now + 14.days - 1.second
26 @other_methods = ELECTION_TYPES.reject {|i| i == election_method}
34 read_attribute( :startdate ) || Time.now
45 self.voters.each do |voter|
52 self.candidates.each do |candidate|
61 if self.candidates.length <= 1
62 reasons << "You must have at least two candidates."
65 if self.voters.length <= 1
66 reasons << "You must have at least two voters."
78 self.class == 'QuickVote'
90 shortdesc = description.split(/\n/)[0]
94 longdesc = description.split(/\n/)[1..-1].join("")
95 longdesc.length > 0 ? longdesc : nil
98 #Calculate Election Results
100 # initalize the tallies to empty arrays
101 preference_tally = Array.new
102 plurality_tally = Array.new
103 approval_tally = Array.new
105 self.voters.each do |voter|
106 # skip if the voter has not voted or has an unconfirmed vote
107 next unless voter.voted?
109 plurality_tally << voter.vote.rankings.sort[0].candidate.id
110 approval_tally << voter.vote.rankings.sort[0..1].collect \
111 { |ranking| ranking.candidate.id }
112 preference_tally << voter.vote.rankings.sort.collect \
113 { |ranking| ranking.candidate.id }
116 @plurality_result = PluralityVote.new(plurality_tally).result
117 @approval_result = ApprovalVote.new(approval_tally).result
118 @condorcet_result = PureCondorcetVote.new(preference_tally).result
119 @ssd_result = CloneproofSSDVote.new(preference_tally).result
120 @borda_result = BordaVote.new(preference_tally).result
122 { 'plurality' => @plurality_result,
123 'approval' => @approval_result,
124 'condorcet' => @condorcet_result,
125 'ssd' => @ssd_result,
126 'borda' => @borda_result }
132 competitors = self.candidates.sort.collect {|candidate| candidate.id}
133 competitors.each do |candidate|
134 names[candidate] = Candidate.find(candidate).name