From 56347021a0a4404decc02aa6ea985acb903f06a0 Mon Sep 17 00:00:00 2001 From: Adrian Short Date: Sat, 23 May 2015 16:38:23 +0100 Subject: [PATCH] Calculate vote share in multi-member districts better #19 --- app.rb | 11 ++++++++++- models.rb | 16 ++++++++++++---- views/resultsdistrict.haml | 6 +++++- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/app.rb b/app.rb index be81564..23b1d6e 100644 --- a/app.rb +++ b/app.rb @@ -259,6 +259,15 @@ get '/bodies/:body/elections/:date/:districts_name/:district' do @districts_in_this_election = @election.candidacies.districts @poll = Poll.get(@district.id, @election.id) + if @total_seats == 1 + @share_denominator = @total_votes + elsif @poll.valid_ballot_papers + @share_denominator = @poll.valid_ballot_papers + else + @share_denominator = @total_votes / @total_seats + @share_message = "The vote share calculations have been estimated as we do not 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 @results_by_party = repository(:default).adapter.select(" SELECT @@ -292,7 +301,7 @@ get '/bodies/:body/elections/:date/:districts_name/:district' do c.candidate.short_name, party_name(c.labcoop, c.party.name), commify(c.votes), - format_percent(c.votes.to_f / @total_votes * 100), + format_percent(c.votes.to_f / @share_denominator * 100), c.seats == 1 ? 'Elected' : '' ] end diff --git a/models.rb b/models.rb index bcee4dd..fee856d 100644 --- a/models.rb +++ b/models.rb @@ -18,10 +18,18 @@ class Poll end def total_rejected_ballots - @rejected_no_official_mark + \ - @rejected_too_many_candidates + \ - @rejected_identifiable_voter + \ - @rejected_blank_or_uncertain + if @rejected_no_official_mark + @rejected_no_official_mark + \ + @rejected_too_many_candidates + \ + @rejected_identifiable_voter + \ + @rejected_blank_or_uncertain + else + nil + end + end + + def valid_ballot_papers + self.total_rejected_ballots ? @ballot_papers_issued - self.total_rejected_ballots : nil end belongs_to :election diff --git a/views/resultsdistrict.haml b/views/resultsdistrict.haml index 9e150bd..37f5caa 100644 --- a/views/resultsdistrict.haml +++ b/views/resultsdistrict.haml @@ -64,7 +64,7 @@ = party_name(candidacy.labcoop, candidacy.party.name) - if @election_held %td.right= commify(candidacy.votes) - %td.right= format_percent(candidacy.votes.to_f / @total_votes * 100) + %td.right= format_percent(candidacy.votes.to_f / @share_denominator * 100) - if candidacy.seats == 1 %td.elected="Elected" @@ -84,6 +84,10 @@ %td   %td   +- if @share_message + .warning + = @share_message + %p %a{ :href => "?wiki=dokuwiki" } DokuWiki