Browse Source

Calculate vote share in multi-member districts better #19

tags/last-sinatra-version
Adrian Short 9 years ago
parent
commit
56347021a0
3 changed files with 27 additions and 6 deletions
  1. +10
    -1
      app.rb
  2. +12
    -4
      models.rb
  3. +5
    -1
      views/resultsdistrict.haml

+ 10
- 1
app.rb View File

@@ -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


+ 12
- 4
models.rb View File

@@ -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


+ 5
- 1
views/resultsdistrict.haml View File

@@ -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


Loading…
Cancel
Save