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
20 @other_methods = ELECTION_TYPES.reject {|i| i == election_method}
28 read_attribute( :startdate ) || Time.now
32 date = read_attribute( :enddate ) || Time.now + 14
44 self.voters.each do |voter|
51 self.candidates.each do |candidate|
60 if self.candidates.length <= 1
61 reasons << "You must have at least two candidates."
64 if self.voters.length <= 1
65 reasons << "You must have at least two voters."
77 self.class == 'QuickVote'
89 shortdesc = description.split(/\n/)[0]
93 longdesc = description.split(/\n/)[1..-1].join("")
94 longdesc.length > 0 ? longdesc : nil
97 #Calculate Election Results
99 # initalize the tallies to empty arrays
100 preference_tally = Array.new
101 plurality_tally = Array.new
102 approval_tally = Array.new
104 self.voters.each do |voter|
105 # skip if the voter has not voted or has an unconfirmed vote
106 next unless voter.voted?
108 plurality_tally << voter.vote.rankings.sort[0].candidate.id
109 approval_tally << voter.vote.rankings.sort[0..1].collect \
110 { |ranking| ranking.candidate.id }
111 preference_tally << voter.vote.rankings.sort.collect \
112 { |ranking| ranking.candidate.id }
115 @plurality_result = PluralityVote.new(plurality_tally).result
116 @approval_result = ApprovalVote.new(approval_tally).result
117 @condorcet_result = PureCondorcetVote.new(preference_tally).result
118 @ssd_result = CloneproofSSDVote.new(preference_tally).result
119 @borda_result = BordaVote.new(preference_tally).result
121 { 'plurality' => @plurality_result,
122 'approval' => @approval_result,
123 'condorcet' => @condorcet_result,
124 'ssd' => @ssd_result,
125 'borda' => @borda_result }
131 competitors = self.candidates.sort.collect {|candidate| candidate.id}
132 competitors.each do |candidate|
133 names[candidate] = Candidate.find(candidate).name