From 91c075521a9a4dc9188c25b5c0f19cbb61c18beb Mon Sep 17 00:00:00 2001 From: Adrian Short Date: Fri, 9 Sep 2016 16:44:23 +0100 Subject: [PATCH] Put set_positions task in Rakefile. Fix #15 scripts/setpositions.rb deleted as now redundant. New rake task works for all elections (full/byelections). --- Rakefile | 35 ++++++++++++++++++++++++++++++++++ scripts/setpositions.rb | 42 ----------------------------------------- 2 files changed, 35 insertions(+), 42 deletions(-) create mode 100644 Rakefile delete mode 100644 scripts/setpositions.rb diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..ba2cd64 --- /dev/null +++ b/Rakefile @@ -0,0 +1,35 @@ +require './models' + +desc "Set the candidate positions for an election (will prompt you for election ID)." +task :set_positions do + Election.all.each do |e| + puts "%d %s %s" % [ e.id, e.d, e.body.name ] + end + + puts "Which election ID?" + + STDOUT.flush + id = STDIN.gets.chomp.to_i + + if e = Election.get(id) + puts "%d %s %s" % [ e.id, e.d,e.body.name ] + e.polls.each do |poll| + poll.set_positions + + separator = '-' * poll.district.name.size + puts + puts separator + puts poll.district.name + puts separator + + ccys = Candidacy.all(:conditions => { :district_id => poll.district_id, :election_id => e.id }, :order => [:votes.desc]) + + ccys.each do |cand| + puts "%-25s %-25s %-40s %5d %2d %s" % [ cand.candidate.surname, cand.candidate.forenames, cand.party.name, cand.votes, cand.position, cand.seats == 1 ? 'elected' : '' ] + end + end + else + puts "Election ID #{id} not found." + return 1 + end +end diff --git a/scripts/setpositions.rb b/scripts/setpositions.rb deleted file mode 100644 index 10033c1..0000000 --- a/scripts/setpositions.rb +++ /dev/null @@ -1,42 +0,0 @@ -require_relative '../models' -require 'pp' - -# Set position and elected for each candidacy -# WARNING - This will only work in full council elections not byelections (as the number of seats being elected in that eleciton is probably less than the total number of seats in the ward) -# This will break all the byelection data currently in the database - -election = Election.first(:d => ARGV.shift) - -unless election - puts "Election not found. Usage example: $ %s 2006-05-04" % __FILE__ - exit 1 -end - -puts "Setting candidacy positions for %s %s %s" % [ election.body.name, election.kind, election.d.to_s ] -if election.candidacies.size < 30 - puts "This script only works for full council elections. Your election looks like a byelection, so quitting." - exit 1 -end -puts "%d candidacies in this election" % election.candidacies.size - -election.body.districts.each do |district| - cands = Candidacy.all(:conditions => { :district_id => district.id, :election_id => election.id }, :order => [:votes.desc]) - # pp cands - puts - pos = 1 - cands.each do |cand| - if pos == 1 - puts "-" * 62 - puts district.name - puts "-" * 62 - end - - # If this candidate's position was in the top district.seats positions, they won a seat - pos <= district.seats ? seats = 1 : seats = 0 - puts "%-25s %-25s %5d %2d %d" % [ cand.candidate.surname, cand.candidate.forenames, cand.votes, pos, seats ] - cand.position = pos - cand.seats = seats - cand.save - pos += 1 - end -end