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 + 30.days - 1.second
26 @other_methods = ELECTION_TYPES.reject {|i| i == election_method}
34 read_attribute( :startdate ) || Time.now
39 self.voters.each do |voter|
46 self.candidates.each do |candidate|
54 if self.candidates.length <= 1
55 reasons << "You must have at least two candidates."
58 if self.voters.length <= 1
59 reasons << "You must have at least two voters."
71 self.class == 'QuickVote'
83 shortdesc = description.split(/\n/)[0]
87 longdesc = description.split(/\n/)[1..-1].join("")
88 longdesc.length > 0 ? longdesc : nil
91 #Calculate Election Results
93 # initalize the tallies to empty arrays
94 preference_tally = Array.new
95 plurality_tally = Array.new
96 approval_tally = Array.new
98 self.voters.each do |voter|
99 # skip if the voter has not voted or has an unconfirmed vote
100 next unless voter.voted?
102 plurality_tally << voter.vote.rankings.sort[0].candidate.id
103 approval_tally << voter.vote.rankings.sort[0..1].collect \
104 { |ranking| ranking.candidate.id }
105 preference_tally << voter.vote.rankings.sort.collect \
106 { |ranking| ranking.candidate.id }
109 @plurality_result = PluralityVote.new(plurality_tally).result
110 @approval_result = ApprovalVote.new(approval_tally).result
111 @condorcet_result = PureCondorcetVote.new(preference_tally).result
112 @ssd_result = CloneproofSSDVote.new(preference_tally).result
113 @borda_result = BordaVote.new(preference_tally).result
115 { 'plurality' => @plurality_result,
116 'approval' => @approval_result,
117 'condorcet' => @condorcet_result,
118 'ssd' => @ssd_result,
119 'borda' => @borda_result }
125 competitors = self.candidates.sort.collect {|candidate| candidate.id}
126 competitors.each do |candidate|
127 names[candidate] = Candidate.find(candidate).name