Election results in the London Borough of Sutton.
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

272 wiersze
8.4 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 DeletedCandidate
  72. include DataMapper::Resource
  73. property :old_candidate_id, Integer, :key => true # ID of candidate that has been merged/deleted
  74. property :candidate_id, Integer, :required => true # ID of candidate that has been kept
  75. end
  76. class Candidacy
  77. include DataMapper::Resource
  78. property :id, Serial
  79. property :election_id, Integer, :required => true
  80. property :candidate_id, Integer, :required => true
  81. property :party_id, Integer
  82. property :district_id, Integer, :required => true
  83. property :votes, Integer
  84. property :address, String, :length => 200
  85. property :postcode, String
  86. property :position, Integer # Position of this candidate in this district. (1..n)
  87. property :seats, Integer # Number of seats won by this candidacy (0 or 1)
  88. property :labcoop, Boolean, :default => false # Candidacy is for joint Labour/Co-op party
  89. belongs_to :election
  90. belongs_to :candidate
  91. belongs_to :party
  92. belongs_to :district
  93. end
  94. class Campaign
  95. include DataMapper::Resource
  96. property :party_id, Integer, :key => true
  97. property :election_id, Integer, :key => true
  98. property :party_url, String, :length => 255
  99. property :manifesto_html_url, String, :length => 255
  100. property :manifesto_pdf_url, String, :length => 255
  101. belongs_to :party
  102. belongs_to :election
  103. end
  104. class Election
  105. include DataMapper::Resource
  106. property :id, Serial
  107. property :body_id, Integer, :required => true
  108. property :d, Date, :required => true, :index => true
  109. property :reason, String, :length => 255
  110. property :kind, String, :length => 255
  111. has n, :candidacies
  112. has n, :polls
  113. belongs_to :body
  114. has n, :campaigns
  115. def self.past
  116. self.all(:d.lt => Time.now.to_s, :order => [ :d.desc ])
  117. end
  118. def self.future
  119. self.all(:d.gte => Time.now.to_s, :order => [ :d.desc ])
  120. end
  121. # electorate and ballot_papers_issued assume there's a Poll object for every district in this election
  122. def electorate
  123. Poll.sum(:electorate, :election => self)
  124. end
  125. def ballot_papers_issued
  126. Poll.sum(:ballot_papers_issued, :election => self)
  127. end
  128. end
  129. class District
  130. include DataMapper::Resource
  131. property :id, Serial
  132. property :body_id, Integer, :required => true
  133. property :name, String, :length => 255, :required => true
  134. property :slug, String
  135. property :seats, Integer
  136. property :ons_district_code, String
  137. belongs_to :body
  138. has n, :postcodes, :child_key => [:ward_id]
  139. has n, :polls
  140. def self.slugify(name)
  141. name.gsub(/[^\w\s-]/, '').gsub(/\s+/, '-').downcase
  142. end
  143. end
  144. class Body
  145. include DataMapper::Resource
  146. property :id, Serial
  147. property :name, String, :length => 255, :required => true
  148. property :district_name, String, :length => 255, :required => true # singular
  149. property :districts_name, String, :length => 255, :required => true # plural
  150. property :slug, String, :length => 255
  151. has n, :elections
  152. has n, :districts
  153. end
  154. class Party
  155. include DataMapper::Resource
  156. property :id, Serial
  157. property :name, String, :required => true
  158. property :colour, String
  159. has n, :candidacies
  160. has n, :campaigns
  161. end
  162. # These models are now redundant
  163. class Ward
  164. include DataMapper::Resource
  165. property :id, Serial
  166. property :slug, String, :required => true
  167. property :ons_id, String, :required => true
  168. property :name, String, :required => true
  169. property :constituency_id, Integer, :required => true
  170. has n, :councilcandidates, :order => ['surname']
  171. belongs_to :constituency
  172. def self.slugify(name)
  173. name.gsub(/[^\w\s-]/, '').gsub(/\s+/, '-').downcase
  174. end
  175. end
  176. class Councilcandidate
  177. include DataMapper::Resource
  178. property :id, Serial
  179. property :ward_id, Integer, :required => true
  180. property :party_id, Integer, :required => true
  181. property :forenames, String, :required => true
  182. property :surname, String, :required => true
  183. property :address, String, :length => 200
  184. property :postcode, String, :required => true
  185. property :votes_2010, Integer
  186. belongs_to :party
  187. belongs_to :ward
  188. end
  189. class Parliamentcandidate
  190. include DataMapper::Resource
  191. property :id, Serial
  192. property :constituency_id, Integer, :required => true
  193. property :party_id, Integer, :required => true
  194. property :forenames, String, :required => true
  195. property :surname, String, :required => true
  196. property :address, String, :length => 200
  197. property :postcode, String
  198. property :votes_2010, Integer
  199. property :votes_2005, Integer
  200. property :percent_2005, Float
  201. belongs_to :party
  202. belongs_to :constituency
  203. end
  204. class Constituency
  205. include DataMapper::Resource
  206. property :id, Serial
  207. property :name, String, :required => true
  208. has n, :wards, :order => ['name']
  209. has n, :parliamentcandidates, :order => ['surname']
  210. end
  211. DataMapper.setup(:default, ENV['DATABASE_URL'] || "postgres://postgres@localhost:5432/suttonelections")
  212. DataMapper.auto_upgrade!