diff --git a/app.rb b/app.rb index 9529dc4..00f98d9 100644 --- a/app.rb +++ b/app.rb @@ -241,7 +241,8 @@ get '/bodies/:body/elections/:date/:districts_name/:district' do @total_candidates = Candidacy.count(:district => @district, :election => @election) @total_seats = Candidacy.sum(:seats, :district => @district, :election => @election) @districts_in_this_election = @election.candidacies.districts - + @poll = Poll.get(@district.id, @election.id) + # Postgres: All the columns selected when using GROUP BY must either be aggregate functions or appear in the GROUP BY clause @results_by_party = repository(:default).adapter.select(" SELECT diff --git a/models.rb b/models.rb index 50ed220..705983d 100644 --- a/models.rb +++ b/models.rb @@ -149,6 +149,14 @@ class Election self.all(:d.gte => Time.now.to_s, :order => [ :d.desc ]) end + # electorate and ballot_papers_issued assume there's a Poll object for every district in this election + def electorate + Poll.sum(:electorate, :election => self) + end + + def ballot_papers_issued + Poll.sum(:ballot_papers_issued, :election => self) + end end class District diff --git a/views/electionsummary.haml b/views/electionsummary.haml index cf3f5a4..513c825 100644 --- a/views/electionsummary.haml +++ b/views/electionsummary.haml @@ -39,7 +39,7 @@ - unless @election_held .warning We don't have the results for this election yet. - + %p= @election.reason %p @@ -107,6 +107,18 @@ %td   %td   %td   + + - if @election.ballot_papers_issued + %table + %tr + %td Electorate + %td.right= commify(@election.electorate) + %tr + %td Ballot papers issued + %td.right= commify(@election.ballot_papers_issued) + %tr + %td Turnout + %td.right= sprintf("%.0f%%", @election.ballot_papers_issued / @election.electorate.to_f * 100) %h2 = @election.body.district_name.capitalize.pluralize(2) diff --git a/views/resultsdistrict.haml b/views/resultsdistrict.haml index 62ecc9b..5359e41 100644 --- a/views/resultsdistrict.haml +++ b/views/resultsdistrict.haml @@ -39,6 +39,18 @@ .warning We don't have the results for this election yet. +- if @poll + %table + %tr + %td Ballot papers issued + %td.right= commify(@poll.ballot_papers_issued) + %tr + %td Electorate + %td.right= commify(@poll.electorate) + %tr + %td Turnout + %td.right= sprintf("%.0f%%", @poll.turnout_percent) + %h2 Votes by party %table @@ -128,3 +140,4 @@ %td.right= commify(@total_votes) %td   %td   +