Election results in the London Borough of Sutton.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

215 lignes
4.6 KiB

  1. require 'rubygems'
  2. require 'sinatra'
  3. require 'haml'
  4. require './models'
  5. class String
  6. def pluralize(num)
  7. if num == 1
  8. return self
  9. end
  10. case self[-1]
  11. when 'y'
  12. self[0..-2] + 'ies'
  13. when 's'
  14. self + "es"
  15. else
  16. self + "s"
  17. end
  18. end
  19. end
  20. helpers do
  21. # Format a number with commas for every ^3
  22. def commify(num)
  23. num.to_s.reverse.gsub(/(\d\d\d)(?=\d)(?!\d*\.)/,'\1,').reverse
  24. end
  25. # From http://snippets.dzone.com/posts/show/593
  26. def to_ordinal(num)
  27. num = num.to_i
  28. if (10...20) === num
  29. "#{num}th"
  30. else
  31. g = %w{ th st nd rd th th th th th th }
  32. a = num.to_s
  33. c = a[-1..-1].to_i
  34. a + g[c]
  35. end
  36. end
  37. def format_percent(num)
  38. sprintf("%.0f%%", num)
  39. end
  40. def short_date(d)
  41. d.strftime("%e %b %Y")
  42. end
  43. def long_date(d)
  44. d.strftime("%e %B %Y")
  45. end
  46. end
  47. get '/' do
  48. # if params[:postcode]
  49. # @postcode = params[:postcode].strip.upcase
  50. #
  51. # unless result = Postcode.finder(@postcode)
  52. # # Invalid postcode
  53. # redirect '/error'
  54. # end
  55. #
  56. # # Postcode valid but not in LB Sutton
  57. # if result.district_code != "00BF"
  58. # redirect '/aliens'
  59. # end
  60. #
  61. # # Postcode in LB Sutton
  62. # @ward = Ward.first( :ons_id => result.ward_code )
  63. # redirect "/wards/#{@ward.slug}/postcode/#{@postcode}"
  64. # end
  65. @future_elections = Election.future
  66. @past_elections = Election.past
  67. haml :index
  68. end
  69. get '/bodies/:body/elections/:date' do
  70. @body = Body.first(:slug => params[:body])
  71. @election = Election.first(:body => @body, :d => params[:date])
  72. @elections_for_this_body = Election.all(:body => @body, :order => [:d])
  73. @total_seats = Candidacy.sum(:seats, :election => @election)
  74. @total_votes = Candidacy.sum(:votes, :election => @election)
  75. # There's got to be a better way to do this, either with SQL or Datamapper
  76. @total_districts = repository(:default).adapter.select("
  77. SELECT district_id
  78. FROM candidacies
  79. WHERE election_id = #{@election.id}
  80. GROUP BY district_id
  81. ORDER BY district_id
  82. ").count
  83. @results_by_party = repository(:default).adapter.select("
  84. SELECT
  85. p.colour,
  86. p.name,
  87. SUM(c.votes) AS votez,
  88. SUM(c.seats) AS seatz,
  89. COUNT(*) AS cands
  90. FROM candidacies c
  91. LEFT JOIN parties p ON p.id = c.party_id
  92. WHERE c.election_id = #{@election.id}
  93. GROUP BY c.party_id, p.colour, p.name
  94. ORDER BY seatz DESC, votez DESC
  95. ")
  96. @results_by_district = repository(:default).adapter.select("
  97. SELECT
  98. d.name,
  99. d.slug AS district_slug,
  100. SUM(c.seats) AS seats,
  101. SUM(c.votes) AS votez
  102. FROM districts d, candidacies c
  103. WHERE
  104. c.district_id = d.id
  105. AND c.election_id = #{@election.id}
  106. GROUP BY c.district_id, d.name, d.slug
  107. ORDER BY d.name
  108. ")
  109. haml :electionsummary
  110. end
  111. # get '/bodies/:body/elections/:date/parties/:party' do
  112. # Not written yet. Show how this party did at this election.
  113. # end
  114. get '/bodies/?' do
  115. @bodies = Body.all
  116. haml :bodies
  117. end
  118. get '/bodies/:body/?' do
  119. @body = Body.first(:slug => params[:body])
  120. @elections = Election.all(:body => @body, :order => [:d.desc])
  121. @districts = District.all(:body => @body, :order => [:name])
  122. haml :body
  123. end
  124. # get '/wards/:slug/postcode/:postcode/?' do
  125. # @ward = Ward.first(:slug => params[:slug])
  126. # @postcode = params[:postcode]
  127. # haml :wards
  128. # end
  129. get '/candidates/:id/?' do
  130. if @candidate = Candidate.get(params[:id])
  131. haml :candidate
  132. else
  133. 404
  134. end
  135. end
  136. get '/candidates/?' do
  137. @candidates = Candidate.all(:order => [ :surname, :forenames ])
  138. haml :candidates
  139. end
  140. get '/bodies/:body/elections/:date/:districts_name/:district' do
  141. @district = District.first(:slug => params[:district])
  142. @body = Body.first(:slug => params[:body])
  143. @election = Election.first(:body => @body, :d => params[:date])
  144. @candidacies = Candidacy.all(:district => @district, :election => @election, :order => [:votes.desc])
  145. @total_votes = Candidacy.sum(:votes, :district => @district, :election => @election)
  146. @districts_in_this_election = @election.candidacies.districts
  147. haml :resultsdistrict
  148. end
  149. get '/bodies/:body/:districts_name/:district' do
  150. @district = District.first(:slug => params[:district])
  151. @body = Body.first(:slug => params[:body])
  152. haml :district
  153. end
  154. get '/how-the-council-election-works' do
  155. haml :election
  156. end
  157. get '/how-the-parliament-election-works' do
  158. haml :parliament
  159. end
  160. # get '/voting' do
  161. # haml :voting
  162. # end
  163. get '/error' do
  164. haml :error
  165. end
  166. get '/about' do
  167. haml :about
  168. end
  169. # get '/aliens' do
  170. # haml :aliens
  171. # end
  172. not_found do
  173. haml :not_found
  174. end