Election results in the London Borough of Sutton.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

import-results.rb 1.7 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. require_relative '../models'
  2. require 'pp'
  3. # $ ruby scripts/import-results.rb [ELECTION ID] [CSV FILENAME]
  4. # Import a CSV file of election results for a specified election
  5. # The election for which you're importing data must already exist in the elections table
  6. # Run setpositions.rb after this importer to set candidacies.position and candidacies.elected
  7. @election = Election.get(ARGV.shift)
  8. CSV.foreach(ARGV.shift) do |line|
  9. district_name, candidate_name, party, votes, seats = line
  10. pp line
  11. # District
  12. @district = District.first(:name => district_name)
  13. puts @district.name
  14. # Candidate
  15. # Assumes that the candidate name is written "forename surname"
  16. bits = candidate_name.split(" ")
  17. candidate_forenames = bits[0..-2].join(" ")
  18. candidate_surname = bits.last
  19. # Assumes that the candidate name is written "surname, forename(s)"
  20. # bits = candidate_name.split(", ")
  21. # candidate_forenames = bits[1]
  22. # candidate_surname = bits[0]
  23. @candidate = Candidate.first_or_create(:forenames => candidate_forenames, :surname => candidate_surname)
  24. pp @candidate
  25. unless @candidate.saved?
  26. $stderr.puts "Couldn't save candidate #{@candidate}"
  27. exit 1
  28. end
  29. # Party
  30. @party = Party.first_or_create(:name => party)
  31. puts @party.name
  32. unless @party.saved?
  33. $stderr.puts "Couldn't save party #{@party}"
  34. exit 1
  35. end
  36. # Candidacy
  37. @candidacy = Candidacy.create(
  38. :election => @election,
  39. :candidate => @candidate,
  40. :party => @party,
  41. :district => @district,
  42. :votes => votes,
  43. :seats => seats
  44. )
  45. unless @candidacy.saved?
  46. $stderr.puts "Couldn't save candidacy #{@candidacy}"
  47. exit 1
  48. end
  49. end