diff --git a/scripts/loaddistricts.rb b/scripts/loaddistricts.rb new file mode 100644 index 0000000..d1575f9 --- /dev/null +++ b/scripts/loaddistricts.rb @@ -0,0 +1,88 @@ +require './app' +require 'pp' + +# Load wards as districts + +# Ward.all.each do |ward| +# District.create( +# :body_id => 1, +# :name => ward.name, +# :slug => ward.slug +# ) +# end + +# Load constituencies as districts + +# Constituency.all.each do |c| +# District.create( +# :body_id => 2, +# :name => c.name, +# :slug => c.name.gsub(/[^\w\s-]/, '').gsub(/\s+/, '-').downcase +# ) +# end + +# Load council candidates as candidates + +# Councilcandidate.all.each do |old_c| +# new_c = Candidate.create!( +# :forenames => old_c.forenames, +# :surname => old_c.surname, +# ) +# +# if new_c.saved? +# puts "Created %s OK" % new_c.surname +# else +# pp new_c +# end +# +# candidacy = Candidacy.create!( +# :election_id => 1, +# :candidate => new_c, +# :party_id => old_c.party_id, +# :district => District.first(:slug => old_c.ward.slug), +# :votes => old_c.votes_2010, +# :address => old_c.address, +# :postcode => old_c.postcode +# ) +# +# if candidacy.saved? +# puts "Candidacy created ok" +# else +# pp candidacy +# end +# end + +# Load parliamentary candidates + +Parliamentcandidate.all.each do |old_c| + new_c = Candidate.first_or_create( + :forenames => old_c.forenames, + :surname => old_c.surname, + ) + + if new_c.saved? + puts "Created %s OK" % new_c.surname + else + pp new_c + end + + + unless old_c.votes_2010.nil? + candidacy = Candidacy.create!( + :election_id => 2, + :candidate => new_c, + :party_id => old_c.party_id, + :district_id => old_c.constituency_id == 1 ? 19 : 20, + :votes => old_c.votes_2010, + :address => old_c.address, + :postcode => old_c.postcode + ) + end + + if candidacy.saved? + puts "2010 candidacy created ok" + else + pp candidacy + end + +end \ No newline at end of file diff --git a/scripts/setpositions.rb b/scripts/setpositions.rb new file mode 100644 index 0000000..1ca9610 --- /dev/null +++ b/scripts/setpositions.rb @@ -0,0 +1,21 @@ +require './lib/models' +require 'pp' + +# Set position and elected for each candidacy + +Election.all.each do |election| + 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| + pos <= district.seats ? elected = true : elected = false + print cand.candidate.surname, ' ', cand.votes, ' ', pos, ' ', elected, "\n" + cand.position = pos + cand.elected = elected + cand.save + pos += 1 + end + end +end \ No newline at end of file