Selaa lähdekoodia

Add seats won and total number of candidates; Fix SQL incompatibility with Postgres

tags/last-sinatra-version
Adrian Short 12 vuotta sitten
vanhempi
commit
e5db0c4520
2 muutettua tiedostoa jossa 9 lisäystä ja 2 poistoa
  1. +5
    -1
      app.rb
  2. +4
    -1
      views/resultsdistrict.haml

+ 5
- 1
app.rb Näytä tiedosto

@@ -175,13 +175,17 @@ get '/bodies/:body/elections/:date/:districts_name/:district' do
@election = Election.first(:body => @body, :d => params[:date])
@candidacies = Candidacy.all(:district => @district, :election => @election, :order => [:votes.desc])
@total_votes = Candidacy.sum(:votes, :district => @district, :election => @election)
@total_candidates = Candidacy.count(:district => @district, :election => @election)
@total_seats = Candidacy.sum(:seats, :district => @district, :election => @election)
@districts_in_this_election = @election.candidacies.districts
# 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
p.name AS party_name,
p.colour AS party_colour,
COUNT(c.id) AS num_candidates,
SUM(c.seats) AS num_seats,
SUM(c.votes) AS total_votes

FROM candidacies c
@@ -192,7 +196,7 @@ get '/bodies/:body/elections/:date/:districts_name/:district' do
WHERE c.district_id = #{@district.id}
AND c.election_id = #{@election.id}
GROUP BY p.name
GROUP BY p.name, p.colour
ORDER BY total_votes DESC
")


+ 4
- 1
views/resultsdistrict.haml Näytä tiedosto

@@ -40,6 +40,7 @@
%th
%th
%th
%th.right.highlight seats won
%th candidates
%th.right votes
%th.right % votes
@@ -52,6 +53,7 @@
%td.right= count
%td{ :style => "background-color: #{row['party_colour'] }" }  
%td= row['party_name']
%td.right.highlight= row['num_seats']
%td.right= row['num_candidates']
%td.right= commify(row['total_votes'])
%td.right= format_percent(row['total_votes'].to_f / @total_votes * 100)
@@ -60,7 +62,8 @@
%td
%td
%td
%td
%td.right.highlight= @total_seats
%td.right= @total_candidates
%td.right= commify(@total_votes)
%td



Ladataan…
Peruuta
Tallenna