Election results in the London Borough of Sutton.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

213 lines
4.5 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. @elections = Election.all(:body => @body, :order => [:d.desc])
  119. @districts = District.all(:body => @body, :order => [:name])
  120. haml :body
  121. end
  122. # get '/wards/:slug/postcode/:postcode/?' do
  123. # @ward = Ward.first(:slug => params[:slug])
  124. # @postcode = params[:postcode]
  125. # haml :wards
  126. # end
  127. get '/candidates/:id/?' do
  128. if @candidate = Candidate.get(params[:id])
  129. haml :candidate
  130. else
  131. 404
  132. end
  133. end
  134. get '/candidates/?' do
  135. @candidates = Candidate.all(:order => [ :surname, :forenames ])
  136. haml :candidates
  137. end
  138. get '/bodies/:body/elections/:date/:districts_name/:district' do
  139. @district = District.first(:slug => params[:district])
  140. @body = Body.first(:slug => params[:body])
  141. @election = Election.first(:body => @body, :d => params[:date])
  142. @candidacies = Candidacy.all(:district => @district, :election => @election, :order => [:votes.desc])
  143. @total_votes = Candidacy.sum(:votes, :district => @district, :election => @election)
  144. @districts_in_this_election = @election.candidacies.districts
  145. haml :resultsdistrict
  146. end
  147. get '/bodies/:body/:districts_name/:district' do
  148. @district = District.first(:slug => params[:district])
  149. @body = Body.first(:slug => params[:body])
  150. haml :district
  151. end
  152. get '/how-the-council-election-works' do
  153. haml :election
  154. end
  155. get '/how-the-parliament-election-works' do
  156. haml :parliament
  157. end
  158. # get '/voting' do
  159. # haml :voting
  160. # end
  161. get '/error' do
  162. haml :error
  163. end
  164. get '/about' do
  165. haml :about
  166. end
  167. # get '/aliens' do
  168. # haml :aliens
  169. # end
  170. not_found do
  171. haml :not_found
  172. end