From ee8ac9a17bd32acf1492b6c4e9173a8b5ca67a86 Mon Sep 17 00:00:00 2001 From: Adrian Short Date: Fri, 9 Sep 2016 16:43:02 +0100 Subject: [PATCH] Move set positions logic to Election/Poll models. #15. Fix #30 --- models.rb | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/models.rb b/models.rb index cbcb8d5..a3de52c 100644 --- a/models.rb +++ b/models.rb @@ -35,6 +35,29 @@ class Poll def successful_candidacies # Candidacies where the candidate was elected Candidacy.all(:election => @election, :district => @district, :order => [:position], :limit => @seats) end + + # Set candidacy.position for every candidacy in this poll + # Returns array of candidacies, or false if we don't have results for this poll + def set_positions + # Check that every candidacy for this poll has its votes recorded (ie that the election results are known) + if Candidacy.count(:conditions => { :district_id => @district_id, :election_id => @election_id, :votes => nil }) == 0 + return false + end + + # Get the candidacies for this poll + ccys = Candidacy.all(:conditions => { :district_id => @district_id, :election_id => @election_id }, :order => [:votes.desc]) + + puts "Found no candidacies for this poll" if ccys.size == 0 + + position = 1 + ccys.each do |ccy| + position <= @seats ? ccy.seats = 1 : ccy.seats = 0 + ccy.position = position + ccy.save + position += 1 + end + ccys + end belongs_to :election belongs_to :district @@ -200,6 +223,10 @@ class Election def ballot_papers_issued Poll.sum(:ballot_papers_issued, :election => self) end + + def set_positions + polls.each { |p| p.set_positions } + end end class District @@ -209,7 +236,6 @@ class District property :body_id, Integer, :required => true property :name, String, :length => 255, :required => true property :slug, String - property :seats, Integer property :ons_district_code, String belongs_to :body