|
- #!/usr/bin/env ruby
- require 'petrify'
- require_relative '../models'
- require_relative '../lib/helpers'
- require 'pp'
-
- def gen_info_pages
- Petrify.page('about', 'about')
- Petrify.page('guides', 'guides')
- Petrify.page(%w(guides how-the-parliament-election-works), 'parliament')
- Petrify.page(%w(guides how-the-council-election-works), 'election')
- end
-
- def gen_bodies_pages
- # Body detail pages
- Body.each do |b|
- locals = {
- body: b,
- districts: District.all(:body => b, :order => [:name])
- }
-
- locals['elections'] = repository(:default).adapter.select("
- SELECT
- e.id,
- e.kind,
- e.d,
- (SUM(p.ballot_papers_issued) * 1.0) / SUM(p.electorate) * 100 AS turnout_percent
-
- FROM elections e
-
- LEFT JOIN polls p
- ON e.id = p.election_id
-
- WHERE e.body_id = ?
-
- GROUP BY p.election_id, e.id
- ORDER BY e.d DESC
- ", b.id)
-
- Petrify.page(['bodies', b.slug], 'body', locals)
-
- # Districts for this body
- b.districts.each do |d|
- locals = {
- district: d,
- body: b
- }
- Petrify.page(['bodies', b.slug, b.districts_name, d.slug], 'district', locals)
- end
- end
- end
-
- def gen_candidates_pages
- # Candidate index
- locals = { candidates: Candidate.all(:order => [ :surname, :forenames ]) }
- Petrify.page('candidates', 'candidates', locals)
-
- # Candidate pages
- # FIXME: What do we do about deleted candidates/redirects?
- Candidate.each do |c|
- locals = {
- candidate: c
- }
-
- locals['candidacies'] = repository(:default).adapter.select("
- SELECT
- e.d,
- c.*,
- p.name AS party_name,
- p.colour AS party_colour,
- b.name AS body_name,
- b.slug AS body_slug,
- b.districts_name AS districts_name,
- d.name AS district_name,
- d.slug AS district_slug
-
- FROM candidacies c
-
- INNER JOIN elections e
- ON c.election_id = e.id
-
- INNER JOIN parties p
- ON c.party_id = p.id
-
- INNER JOIN bodies b
- ON e.body_id = b.id
-
- INNER JOIN districts d
- ON c.district_id = d.id
-
- WHERE c.candidate_id = ?
-
- ORDER BY d
- ", c.id)
-
- Petrify.page(['candidates', c.id.to_s], 'candidate', locals)
- end
- end
-
- def gen_elections_pages
- # Election pages
- Election.each do |e|
- locals = {
- body: Body.first(:slug => e.body.slug),
- election: Election.first(:body => e.body, :d => e.d),
- elections_for_this_body: Election.all(:body => e.body, :order => [:d]),
- total_seats: Candidacy.sum(:seats, :election => e),
- total_votes: Candidacy.sum(:votes, :election => e)
- }
-
- # There's got to be a better way to do this, either with SQL or Datamapper
- locals['total_districts'] = repository(:default).adapter.select("
- SELECT district_id
- FROM candidacies
- WHERE election_id = ?
- GROUP BY district_id
- ORDER BY district_id
- ", e.id).count
-
- locals['results_by_party'] = repository(:default).adapter.select("
- SELECT
- p.colour,
- p.name,
- SUM(c.votes) AS votez,
- SUM(c.seats) AS seatz,
- COUNT(*) AS cands
-
- FROM candidacies c
-
- LEFT JOIN parties p ON p.id = c.party_id
-
- WHERE c.election_id = ?
-
- GROUP BY c.party_id, p.colour, p.name
-
- ORDER BY seatz DESC, votez DESC
- ", e.id)
-
- Petrify.page(['bodies', e.body.slug, 'elections', e.d.to_s], 'electionsummary', locals)
-
- # District results for this election (resultsdistrict)
- # Loop through all districts in this election
- e.candidacies.districts.each do |d|
- total_seats = Candidacy.sum(:seats, :district => d, :election => e)
- total_votes = Candidacy.sum(:votes, :district => d, :election => e)
- poll = Poll.get(d.id, e.id)
-
- locals = {
- district: d,
- body: d.body,
- election: e,
- candidacies: Candidacy.all(:district => d, :election => e, :order => [:position]),
- total_votes: total_votes,
- total_candidates: Candidacy.count(:district => d, :election => e),
- total_seats: total_seats,
- districts_in_this_election: e.candidacies.districts,
- poll: poll
- }
-
- locals['share_message'] = nil
- if total_seats == 1
- locals['share_denominator'] = total_votes
- elsif poll && poll.valid_ballot_papers
- locals['share_denominator'] = poll.valid_ballot_papers
- else
- locals['share_denominator'] = total_votes / total_seats
- locals['share_message'] = "The vote share percentages have been estimated as we don't have data for the number of valid ballot papers in this poll."
- end
-
- # Postgres: All the columns selected when using GROUP BY must either be aggregate functions or appear in the GROUP BY clause
- locals['results_by_party'] = repository(:default).adapter.select("
- SELECT
- p.name AS party_name,
- p.colour AS party_colour,
- COUNT(c.id) AS num_candidates,
- SUM(c.seats) AS num_seats,
- SUM(c.votes) AS total_votes
-
- FROM candidacies c
-
- LEFT JOIN parties p
- ON c.party_id = p.id
-
- WHERE c.district_id = ?
- AND c.election_id = ?
-
- GROUP BY p.name, p.colour
-
- ORDER BY total_votes DESC
- ", d.id, e.id)
-
- Petrify.page(['bodies', e.body.slug, 'elections', e.d.to_s, e.body.districts_name, d.slug], 'resultsdistrict', locals)
- end
- end
- end
-
- def gen_homepage
- locals = {
- future_elections: Election.future,
- past_elections: Election.past
- }
- Petrify.page('.', 'index', locals)
- end
-
- Petrify.setup
- gen_homepage
- gen_elections_pages
- gen_bodies_pages
- gen_info_pages
- gen_candidates_pages
|