Election results in the London Borough of Sutton.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

257 Zeilen
7.9 KiB

  1. require 'data_mapper'
  2. class Poll
  3. include DataMapper::Resource
  4. property :district_id, Integer, :key => true
  5. property :election_id, Integer, :key => true
  6. property :electorate, Integer # The number of people eligible to vote in this district in this election
  7. property :ballot_papers_issued, Integer # The number of ballot papers issued (includes spoiled ballots)
  8. property :seats, Integer, :required => true # The number of seats to be elected in this district in this election
  9. def turnout_percent
  10. @ballot_papers_issued.to_f / @electorate.to_f * 100.0
  11. end
  12. belongs_to :election
  13. belongs_to :district
  14. end
  15. class Postcode
  16. include DataMapper::Resource
  17. # Postcode natural key, uppercase with space, eg. "SM1 1EA"
  18. # Column names derived from Ordnance Survey CodePoint Open
  19. property :postcode, String, :key => true
  20. property :positional_quality_indicator, Integer
  21. property :eastings, Integer, :required => true
  22. property :northings, Integer, :required => true
  23. property :country_code, String, :required => true
  24. property :nhs_regional_ha_code, String, :required => true
  25. property :nhs_ha_code, String, :required => true
  26. property :admin_county_code, String # NULL within Greater London
  27. property :admin_district_code, String, :required => true # e.g. London Borough of Sutton
  28. property :admin_ward_code, String, :required => true # e.g. Sutton Central
  29. property :lat, Float, :required => true
  30. property :lng, Float, :required => true
  31. property :ward_id, Integer, :required => true # Sutton Council
  32. property :constituency_id, Integer, :required => false # UK Parliament
  33. belongs_to :district, :child_key => [:ward_id]
  34. def self.finder(postcode)
  35. postcode = postcode.strip.upcase
  36. if o = self.get(postcode)
  37. return o
  38. end
  39. result = Pat.get(postcode)
  40. unless result.code == 404
  41. # cache API result
  42. self.create(
  43. :postcode => postcode,
  44. :lat => result['geo']['lat'],
  45. :lng => result['geo']['lng'],
  46. :district_name => result['administrative']['district']['title'],
  47. :district_code => result['administrative']['district']['uri'].match(/.+\/(.+)$/)[1],
  48. :ward_name => result['administrative']['ward']['title'],
  49. :ward_code => result['administrative']['ward']['uri'].match(/.+\/(.+)$/)[1]
  50. )
  51. else
  52. # invalid postcode
  53. nil
  54. end
  55. end
  56. end
  57. class Candidate
  58. include DataMapper::Resource
  59. property :id, Serial
  60. property :forenames, String, :required => true
  61. property :surname, String, :required => true, :index => true
  62. property :sex, String
  63. has n, :candidacies
  64. def short_name
  65. @forenames.split(' ')[0] + ' ' + @surname
  66. end
  67. def name
  68. @forenames + ' ' + @surname
  69. end
  70. end
  71. class Candidacy
  72. include DataMapper::Resource
  73. property :id, Serial
  74. property :election_id, Integer, :required => true
  75. property :candidate_id, Integer, :required => true
  76. property :party_id, Integer
  77. property :district_id, Integer, :required => true
  78. property :votes, Integer
  79. property :address, String, :length => 200
  80. property :postcode, String
  81. property :position, Integer # Position of this candidate in this district. (1..n)
  82. property :seats, Integer # Number of seats won by this candidacy (0 or 1)
  83. property :labcoop, Boolean, :default => false # Candidacy is for joint Labour/Co-op party
  84. belongs_to :election
  85. belongs_to :candidate
  86. belongs_to :party
  87. belongs_to :district
  88. end
  89. class Campaign
  90. include DataMapper::Resource
  91. property :party_id, Integer, :key => true
  92. property :election_id, Integer, :key => true
  93. property :party_url, String, :length => 255
  94. property :manifesto_html_url, String, :length => 255
  95. property :manifesto_pdf_url, String, :length => 255
  96. belongs_to :party
  97. belongs_to :election
  98. end
  99. class Election
  100. include DataMapper::Resource
  101. property :id, Serial
  102. property :body_id, Integer, :required => true
  103. property :d, Date, :required => true, :index => true
  104. property :reason, String, :length => 255
  105. property :kind, String, :length => 255
  106. has n, :candidacies
  107. has n, :polls
  108. belongs_to :body
  109. has n, :campaigns
  110. def self.past
  111. self.all(:d.lt => Time.now.to_s, :order => [ :d.desc ])
  112. end
  113. def self.future
  114. self.all(:d.gte => Time.now.to_s, :order => [ :d.desc ])
  115. end
  116. end
  117. class District
  118. include DataMapper::Resource
  119. property :id, Serial
  120. property :body_id, Integer, :required => true
  121. property :name, String, :length => 255, :required => true
  122. property :slug, String
  123. property :seats, Integer
  124. property :ons_district_code, String
  125. belongs_to :body
  126. has n, :postcodes, :child_key => [:ward_id]
  127. has n, :polls
  128. def self.slugify(name)
  129. name.gsub(/[^\w\s-]/, '').gsub(/\s+/, '-').downcase
  130. end
  131. end
  132. class Body
  133. include DataMapper::Resource
  134. property :id, Serial
  135. property :name, String, :length => 255, :required => true
  136. property :district_name, String, :length => 255, :required => true # singular
  137. property :districts_name, String, :length => 255, :required => true # plural
  138. property :slug, String, :length => 255
  139. has n, :elections
  140. has n, :districts
  141. end
  142. class Party
  143. include DataMapper::Resource
  144. property :id, Serial
  145. property :name, String, :required => true
  146. property :colour, String
  147. has n, :candidacies
  148. has n, :campaigns
  149. end
  150. # These models are now redundant
  151. class Ward
  152. include DataMapper::Resource
  153. property :id, Serial
  154. property :slug, String, :required => true
  155. property :ons_id, String, :required => true
  156. property :name, String, :required => true
  157. property :constituency_id, Integer, :required => true
  158. has n, :councilcandidates, :order => ['surname']
  159. belongs_to :constituency
  160. def self.slugify(name)
  161. name.gsub(/[^\w\s-]/, '').gsub(/\s+/, '-').downcase
  162. end
  163. end
  164. class Councilcandidate
  165. include DataMapper::Resource
  166. property :id, Serial
  167. property :ward_id, Integer, :required => true
  168. property :party_id, Integer, :required => true
  169. property :forenames, String, :required => true
  170. property :surname, String, :required => true
  171. property :address, String, :length => 200
  172. property :postcode, String, :required => true
  173. property :votes_2010, Integer
  174. belongs_to :party
  175. belongs_to :ward
  176. end
  177. class Parliamentcandidate
  178. include DataMapper::Resource
  179. property :id, Serial
  180. property :constituency_id, Integer, :required => true
  181. property :party_id, Integer, :required => true
  182. property :forenames, String, :required => true
  183. property :surname, String, :required => true
  184. property :address, String, :length => 200
  185. property :postcode, String
  186. property :votes_2010, Integer
  187. property :votes_2005, Integer
  188. property :percent_2005, Float
  189. belongs_to :party
  190. belongs_to :constituency
  191. end
  192. class Constituency
  193. include DataMapper::Resource
  194. property :id, Serial
  195. property :name, String, :required => true
  196. has n, :wards, :order => ['name']
  197. has n, :parliamentcandidates, :order => ['surname']
  198. end
  199. DataMapper.setup(:default, ENV['DATABASE_URL'] || "postgres://postgres@localhost:5432/suttonelections")
  200. DataMapper.auto_upgrade!