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.
 
 
 
 

211 lignes
4.4 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("%.1f%%", 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. @election = Election.first(:body => Body.first(:slug => params[:body]), :d => params[:date])
  71. @total_seats = Candidacy.sum(:seats, :election => @election)
  72. @total_votes = Candidacy.sum(:votes, :election => @election)
  73. # There's got to be a better way to do this, either with SQL or Datamapper
  74. @total_districts = repository(:default).adapter.select("
  75. SELECT district_id
  76. FROM candidacies
  77. WHERE election_id = #{@election.id}
  78. GROUP BY district_id
  79. ORDER BY district_id
  80. ").count
  81. @results_by_party = repository(:default).adapter.select("
  82. SELECT
  83. p.colour,
  84. p.name,
  85. SUM(c.votes) AS votez,
  86. SUM(c.seats) AS seatz,
  87. COUNT(*) AS cands
  88. FROM candidacies c
  89. LEFT JOIN parties p ON p.id = c.party_id
  90. WHERE c.election_id = #{@election.id}
  91. GROUP BY c.party_id, p.colour, p.name
  92. ORDER BY seatz DESC, votez DESC
  93. ")
  94. @results_by_district = repository(:default).adapter.select("
  95. SELECT
  96. d.name,
  97. d.slug AS district_slug,
  98. SUM(c.seats) AS seats,
  99. SUM(c.votes) AS votez
  100. FROM districts d, candidacies c
  101. WHERE
  102. c.district_id = d.id
  103. AND c.election_id = #{@election.id}
  104. GROUP BY c.district_id, d.name, d.slug
  105. ORDER BY d.name
  106. ")
  107. haml :electionsummary
  108. end
  109. # get '/bodies/:body/elections/:date/parties/:party' do
  110. # Not written yet. Show how this party did at this election.
  111. # end
  112. get '/bodies/?' do
  113. @bodies = Body.all
  114. haml :bodies
  115. end
  116. get '/bodies/:body/?' do
  117. @body = Body.first(:slug => params[:body])
  118. haml :body
  119. end
  120. # get '/wards/:slug/postcode/:postcode/?' do
  121. # @ward = Ward.first(:slug => params[:slug])
  122. # @postcode = params[:postcode]
  123. # haml :wards
  124. # end
  125. get '/candidates/:id/?' do
  126. if @candidate = Candidate.get(params[:id])
  127. haml :candidate
  128. else
  129. 404
  130. end
  131. end
  132. get '/candidates/?' do
  133. @candidates = Candidate.all(:order => [ :surname, :forenames ])
  134. haml :candidates
  135. end
  136. get '/bodies/:body/elections/:date/:districts_name/:district' do
  137. @district = District.first(:slug => params[:district])
  138. @body = Body.first(:slug => params[:body])
  139. @election = Election.first(:body => @body, :d => params[:date])
  140. @candidacies = Candidacy.all(:district => @district, :election => @election, :order => [:votes.desc])
  141. @total_votes = Candidacy.sum(:votes, :district => @district, :election => @election)
  142. @districts_in_this_election = @election.candidacies.districts
  143. haml :resultsdistrict
  144. end
  145. get '/bodies/:body/:districts_name/:district' do
  146. @district = District.first(:slug => params[:district])
  147. @body = Body.first(:slug => params[:body])
  148. haml :district
  149. end
  150. get '/how-the-council-election-works' do
  151. haml :election
  152. end
  153. get '/how-the-parliament-election-works' do
  154. haml :parliament
  155. end
  156. # get '/voting' do
  157. # haml :voting
  158. # end
  159. get '/error' do
  160. haml :error
  161. end
  162. get '/about' do
  163. haml :about
  164. end
  165. # get '/aliens' do
  166. # haml :aliens
  167. # end
  168. not_found do
  169. haml :not_found
  170. end