1 class Election < ActiveRecord::Base
6 validates_presence_of :name, :description
8 attr_reader :plurality_result
9 attr_reader :approval_result
10 attr_reader :condorcet_result
11 attr_reader :ssd_result
12 attr_reader :borda_result
17 read_attribute( :startdate ) || Time.now
21 date = read_attribute( :enddate ) || Time.now + 14
33 self.voters.each do |voter|
40 self.candidates.each do |candidate|
49 if self.candidates.length <= 1
50 reasons << "You must have at least two candidates."
53 if self.voters.length <= 1
54 reasons << "You must have at least two voters."
66 self.class == 'QuickVote'
78 shortdesc = description.split(/\n/)[0]
82 longdesc = description.split(/\n/)[1..-1].join("")
83 longdesc.length > 0 ? longdesc : nil
86 #Calculate Election Results
88 # initalize the tallies to empty arrays
89 preference_tally = Array.new
90 plurality_tally = Array.new
91 approval_tally = Array.new
93 self.voters.each do |voter|
94 # skip if the voter has not voted or has an unconfirmed vote
95 next unless voter.voted?
97 plurality_tally << voter.vote.rankings.sort[0].candidate.id
98 approval_tally << voter.vote.rankings.sort[0..1].collect \
99 { |ranking| ranking.candidate.id }
100 preference_tally << voter.vote.rankings.sort.collect \
101 { |ranking| ranking.candidate.id }
103 @plurality_result = PluralityVote.new(plurality_tally).result
104 @approval_result = ApprovalVote.new(approval_tally).result
105 @condorcet_result = PureCondorcetVote.new(preference_tally).result
106 @ssd_result = CloneproofSSDVote.new(preference_tally).result
107 @borda_result = BordaVote.new(preference_tally).result
108 #@runoff_result = InstantRunoffVote.new(preference_tally).result
110 nil # to stay consistent
116 competitors = self.candidates.sort.collect {|candidate| candidate.id}
117 competitors.each do |candidate|
118 names[candidate] = Candidate.find(candidate).name