From 97d77da9cce4c002f740ca475655593529591f9a Mon Sep 17 00:00:00 2001 From: Adrian Short Date: Sat, 29 Dec 2012 16:45:26 +0000 Subject: [PATCH] Sort candidate's candidacies in date order. Fix #7 This needs to be written as an SQL query as there's no way I can see in DataMapper to order a result set by a column in a joined table (the election date). --- app.rb | 31 +++++++++++++++++++++++++++++++ views/candidate.haml | 26 +++++++++++++------------- 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/app.rb b/app.rb index 2d80ba5..a650207 100644 --- a/app.rb +++ b/app.rb @@ -159,6 +159,37 @@ end get '/candidates/:id/?' do if @candidate = Candidate.get(params[:id]) + @candidacies = repository(:default).adapter.select(" + SELECT + e.d, + c.*, + p.name AS party_name, + p.colour AS party_colour, + b.name AS body_name, + b.slug AS body_slug, + b.districts_name AS districts_name, + d.name AS district_name, + d.slug AS district_slug + + FROM candidacies c + + INNER JOIN elections e + ON c.election_id = e.id + + INNER JOIN parties p + ON c.party_id = p.id + + INNER JOIN bodies b + ON e.body_id = b.id + + INNER JOIN districts d + ON c.district_id = d.id + + WHERE c.candidate_id = #{@candidate.id} + + ORDER BY d + ") + haml :candidate else 404 diff --git a/views/candidate.haml b/views/candidate.haml index 6d47f76..a0a3ccf 100644 --- a/views/candidate.haml +++ b/views/candidate.haml @@ -17,22 +17,22 @@ %th position %th   - - @candidate.candidacies.each do |ccy| + - @candidacies.each do |ccy| %tr - %td{ :style => "background-color: #{ccy.party.colour}" }   + %td{ :style => "background-color: #{ccy['party_colour']}" }   %td - %a{ :href => "/bodies/#{ccy.election.body.slug}/elections/#{ccy.election.d}" } - = long_date(ccy.election.d) - %td= ccy.party.name + %a{ :href => "/bodies/#{ccy['body_slug']}/elections/#{ccy['d']}" } + = long_date(Date.parse(ccy['d'])) + %td= ccy['party_name'] %td - %a{ :href => "/bodies/#{ccy.election.body.slug}" } - = ccy.election.body.name + %a{ :href => "/bodies/#{ccy['body_slug']}" } + = ccy['body_name'] %td - %a{ :href => "/bodies/#{ccy.election.body.slug}/elections/#{ccy.election.d}/#{ccy.district.body.districts_name}/#{ccy.district.slug}" } - = ccy.district.name - %td.right= commify(ccy.votes) - %td.right= to_ordinal(ccy.position) - %td= ccy.seats == 1 ? "Elected" : "Not elected" - + %a{ :href => "/bodies/#{ccy['body_slug']}/elections/#{ccy['d']}/#{ccy['districts_name']}/#{ccy['district_slug']}" } + = ccy['district_name'] + %td.right= commify(ccy['votes']) + %td.right= to_ordinal(ccy['position']) + %td= ccy['seats'] == 1 ? "Elected" : "Not elected" + .warning This might not be the complete electoral history for this candidate. They might have stood in elections outside Sutton and / or in Sutton elections for which we don't have data. \ No newline at end of file