Election results in the London Borough of Sutton.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

124 rindas
3.6 KiB

  1. require 'dm-core'
  2. require 'dm-validations'
  3. require 'dm-timestamps'
  4. require 'dm-aggregates'
  5. require 'pat'
  6. class Postcode
  7. include DataMapper::Resource
  8. # Postcode natural key, uppercase with space, eg. "SM1 1EA"
  9. property :postcode, String, :key => true
  10. property :created_at, DateTime
  11. property :updated_at, DateTime
  12. property :lat, Float, :required => true
  13. property :lng, Float, :required => true
  14. property :district_name, String, :required => true
  15. property :district_code, String, :required => true
  16. property :ward_name, String, :required => true
  17. property :ward_code, String, :required => true
  18. def self.finder(postcode)
  19. postcode = postcode.strip.upcase
  20. if o = self.get(postcode)
  21. return o
  22. end
  23. result = Pat.get(postcode)
  24. unless result.code == 404
  25. # cache API result
  26. self.create(
  27. :postcode => postcode,
  28. :lat => result['geo']['lat'],
  29. :lng => result['geo']['lng'],
  30. :district_name => result['administrative']['district']['title'],
  31. :district_code => result['administrative']['district']['uri'].match(/.+\/(.+)$/)[1],
  32. :ward_name => result['administrative']['ward']['title'],
  33. :ward_code => result['administrative']['ward']['uri'].match(/.+\/(.+)$/)[1]
  34. )
  35. else
  36. # invalid postcode
  37. nil
  38. end
  39. end
  40. end
  41. class Ward
  42. include DataMapper::Resource
  43. property :id, Serial
  44. property :slug, String, :required => true
  45. property :ons_id, String, :required => true
  46. property :name, String, :required => true
  47. property :constituency_id, Integer, :required => true
  48. has n, :councilcandidates, :order => ['surname']
  49. belongs_to :constituency
  50. def self.slugify(name)
  51. name.gsub(/[^\w\s-]/, '').gsub(/\s+/, '-').downcase
  52. end
  53. end
  54. class Party
  55. include DataMapper::Resource
  56. property :id, Serial
  57. property :name, String, :required => true
  58. property :colour, String
  59. has n, :councilcandidates, :order => ['surname']
  60. has n, :parliamentcandidates, :order => ['surname']
  61. end
  62. class Councilcandidate
  63. include DataMapper::Resource
  64. property :id, Serial
  65. property :ward_id, Integer, :required => true
  66. property :party_id, Integer, :required => true
  67. property :forenames, String, :required => true
  68. property :surname, String, :required => true
  69. property :address, String, :length => 200
  70. property :postcode, String, :required => true
  71. property :votes_2010, Integer
  72. belongs_to :party
  73. belongs_to :ward
  74. end
  75. class Parliamentcandidate
  76. include DataMapper::Resource
  77. property :id, Serial
  78. property :constituency_id, Integer, :required => true
  79. property :party_id, Integer, :required => true
  80. property :forenames, String, :required => true
  81. property :surname, String, :required => true
  82. property :address, String, :length => 200
  83. property :postcode, String
  84. property :votes_2010, Integer
  85. property :votes_2005, Integer
  86. property :percent_2005, Float
  87. belongs_to :party
  88. belongs_to :constituency
  89. end
  90. class Constituency
  91. include DataMapper::Resource
  92. property :id, Serial
  93. property :name, String, :required => true
  94. has n, :wards, :order => ['name']
  95. has n, :parliamentcandidates, :order => ['surname']
  96. end
  97. DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/db.sqlite3")
  98. DataMapper.auto_upgrade!