| @@ -176,6 +176,27 @@ get '/bodies/:body/elections/:date/:districts_name/:district' do | |||||
| @candidacies = Candidacy.all(:district => @district, :election => @election, :order => [:votes.desc]) | @candidacies = Candidacy.all(:district => @district, :election => @election, :order => [:votes.desc]) | ||||
| @total_votes = Candidacy.sum(:votes, :district => @district, :election => @election) | @total_votes = Candidacy.sum(:votes, :district => @district, :election => @election) | ||||
| @districts_in_this_election = @election.candidacies.districts | @districts_in_this_election = @election.candidacies.districts | ||||
| @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.votes) AS total_votes | |||||
| FROM candidacies c | |||||
| LEFT JOIN parties p | |||||
| ON c.party_id = p.id | |||||
| WHERE c.district_id = #{@district.id} | |||||
| AND c.election_id = #{@election.id} | |||||
| GROUP BY p.name | |||||
| ORDER BY total_votes DESC | |||||
| ") | |||||
| haml :resultsdistrict | haml :resultsdistrict | ||||
| end | end | ||||
| @@ -33,7 +33,39 @@ | |||||
| %h1= @district.name + " " + @district.body.district_name | %h1= @district.name + " " + @district.body.district_name | ||||
| - count = 0 | |||||
| %h2 Votes by party | |||||
| %table | |||||
| %tr.header | |||||
| %th | |||||
| %th | |||||
| %th | |||||
| %th candidates | |||||
| %th.right votes | |||||
| %th.right % votes | |||||
| - count = 0 | |||||
| - @results_by_party.each do |row| | |||||
| - count += 1 | |||||
| %tr | |||||
| %td.right= count | |||||
| %td{ :style => "background-color: #{row['party_colour'] }" } | |||||
| %td= row['party_name'] | |||||
| %td.right= row['num_candidates'] | |||||
| %td.right= commify(row['total_votes']) | |||||
| %td.right= format_percent(row['total_votes'].to_f / @total_votes * 100) | |||||
| %tr.footer | |||||
| %td | |||||
| %td | |||||
| %td | |||||
| %td | |||||
| %td.right= commify(@total_votes) | |||||
| %td | |||||
| %h2 Votes by candidate | |||||
| %table | %table | ||||
| %tr.header | %tr.header | ||||
| @@ -44,6 +76,7 @@ | |||||
| %th.right % votes | %th.right % votes | ||||
| %th | %th | ||||
| - count = 0 | |||||
| - for candidacy in @candidacies | - for candidacy in @candidacies | ||||
| - count += 1 | - count += 1 | ||||
| %tr.vcard | %tr.vcard | ||||
| @@ -69,7 +102,7 @@ | |||||
| %td | %td | ||||
| %td | %td | ||||
| %td | %td | ||||
| %td= commify(@total_votes) | |||||
| %td.right= commify(@total_votes) | |||||
| %td | %td | ||||
| %td | %td | ||||
| %td | %td | ||||