X-Git-Url: https://projects.mako.cc/source/selectricity/blobdiff_plain/6a935d078f20d2b1b0d60f0a30c41a642d40758a..4446a6555bd54ca10a925340bb65c706678ac98e:/app/models/vote.rb diff --git a/app/models/vote.rb b/app/models/vote.rb index bb91a6b..719aa7b 100644 --- a/app/models/vote.rb +++ b/app/models/vote.rb @@ -39,7 +39,7 @@ class Vote < ActiveRecord::Base if rankings.empty? @votes = Array.new else - @votes = rankings.sort.collect { |ranking| ranking.candidate.id } + @votes = self.rankings.sort.collect { |ranking| ranking.candidate.id } end end @@ -51,6 +51,9 @@ class Vote < ActiveRecord::Base end def save_rankings + self.votes # i need to initalize this before destroying rankings + # or else the ranks themselves show up as nil + destroy_rankings self.votes.each_with_index do |candidate_id, index| ranking = Ranking.new @@ -69,19 +72,20 @@ class Vote < ActiveRecord::Base rankings.each { |ranking| ranking.destroy } end - def settime - self.time = Time.now - self.save - end - def confirm! - self.confirmed = 1 - self.save - - unless self.voter.election.quickvote? - token.destroy and token.reload if token - self.token = Token.new + if self.voter.election.candidates.length == self.rankings.length + self.confirmed = 1 + self.time = Time.now self.save + + unless self.voter.election.quickvote? + token.destroy and token.reload if token + self.token = Token.new + self.save + end + return false + else + return true end end @@ -105,7 +109,7 @@ class Vote < ActiveRecord::Base # too. It creates a vote with the candidates listed in order of preference # based on alphabetical order. Meant to be manipulated and then confirmed def set_defaults! - self.votes = voter.election.candidates.sort.collect {|c| c.id } + self.votes = self.voter.election.candidates.sort_by { rand }.collect {|c| c.id } self.save end