From 28e03c45afa7c18f966c2467603af82ecb32334e Mon Sep 17 00:00:00 2001 From: Adrian Short Date: Wed, 3 Aug 2016 11:30:30 +0100 Subject: [PATCH] Show turnout for each election on body detail page --- app.rb | 20 +++++++++++++++++++- views/body.haml | 8 ++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/app.rb b/app.rb index 7469a5b..c214016 100644 --- a/app.rb +++ b/app.rb @@ -189,8 +189,26 @@ end get '/bodies/:body/?' do @body = Body.first(:slug => params[:body]) - @elections = Election.all(:body => @body, :order => [:d.desc]) @districts = District.all(:body => @body, :order => [:name]) + + @elections = repository(:default).adapter.select(" + SELECT + e.id, + e.kind, + e.d, + SUM(p.ballot_papers_issued)::float / 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 + ", @body.id) + haml :body end diff --git a/views/body.haml b/views/body.haml index 0e25813..0963b34 100644 --- a/views/body.haml +++ b/views/body.haml @@ -7,6 +7,11 @@ %h2 Elections %table + %thead + %tr + %th + %th + %th turnout - @elections.each do |election| %tr %td @@ -14,6 +19,9 @@ = short_date(election.d) %td = election.kind + %td.right + - if election.turnout_percent + = format_percent(election.turnout_percent) %h2 = @body.districts_name.capitalize