From 71e10fcbce6104e3615da020e5133443ee2cc743 Mon Sep 17 00:00:00 2001 From: Adrian Short Date: Thu, 12 Jan 2012 09:42:20 +0000 Subject: [PATCH] Easy way to merge duplicate candidates --- scripts/merge-candidates.rb | 49 +++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 scripts/merge-candidates.rb diff --git a/scripts/merge-candidates.rb b/scripts/merge-candidates.rb new file mode 100644 index 0000000..98d5d5c --- /dev/null +++ b/scripts/merge-candidates.rb @@ -0,0 +1,49 @@ +require_relative "../models" + +def show_candidate(id) + c = Candidate.get(id) + + template = "%6s\t%50s\t%20s\t%3s" + + puts template % [ c.id, c.forenames, c.surname, c.sex ] + + ccy_template = "%20s\t%15s\t%20s\t%20s" + c.candidacies.each do |ccy| + puts template % [ ccy.election.body.name, ccy.election.d, ccy.district.name, ccy.party.name] + end +end + +winner = ARGV.shift +loser = ARGV.shift + +puts "WINNER" +show_candidate(winner) + +puts + +puts "LOSER" +show_candidate(loser) + +puts "Are you sure you want to merge these two candidates? The loser will be deleted. (Y or N)" + +answer = gets.chomp.downcase + +unless answer == 'y' + puts "Aborting. No changes made to the database." + exit +end + +# Transfer all the loser's candidacies to the winner +repository(:default).adapter.select(" + UPDATE candidacies + SET candidate_id = #{winner} + WHERE candidate_id = #{loser} +") + +# Delete the loser candidate +Candidate.get(loser).destroy + +puts "Merge completed. Here is the merged candidate:" +puts +show_candidate(winner) +