瀏覽代碼

Moved file

tags/last-sinatra-version
Adrian Short 13 年之前
父節點
當前提交
c540ce513f
共有 1 個文件被更改,包括 106 次插入14 次删除
  1. +106
    -14
      models.rb

lib/models.rb → models.rb 查看文件

@@ -1,7 +1,4 @@
require 'dm-core'
require 'dm-validations'
require 'dm-timestamps'
require 'dm-aggregates'
require 'data_mapper'
require 'pat'

class Postcode
@@ -46,22 +43,95 @@ class Postcode
end
end

class Ward
class Candidate
include DataMapper::Resource

property :id, Serial
property :forenames, String, :required => true
property :surname, String, :required => true
property :sex, String
property :id, Serial
property :slug, String, :required => true
property :ons_id, String, :required => true
property :name, String, :required => true
property :constituency_id, Integer, :required => true
has n, :councilcandidates, :order => ['surname']
belongs_to :constituency
has n, :candidacies
def short_name
@forenames.split(' ')[0] + ' ' + @surname
end
def name
@forenames + ' ' + @surname
end
end

class Candidacy
include DataMapper::Resource

property :id, Serial
property :election_id, Integer, :required => true
property :candidate_id, Integer, :required => true
property :party_id, Integer
property :district_id, Integer, :required => true
property :votes, Integer
property :address, String, :length => 200
property :postcode, String
property :position, Integer
property :elected, Boolean
property :seats, Integer

belongs_to :election
belongs_to :candidate
belongs_to :party
belongs_to :district
end

class Election
include DataMapper::Resource

property :id, Serial
property :body_id, Integer, :required => true
property :d, Date, :required => true
property :reason, String, :length => 255
property :kind, String, :length => 255
has n, :candidacies
belongs_to :body
def self.past
self.all(:d.lt => Time.now.to_s, :order => [ :d.desc ])
end
def self.future
self.all(:d.gte => Time.now.to_s, :order => [ :d.desc ])
end
end

class District
include DataMapper::Resource

property :id, Serial
property :body_id, Integer, :required => true
property :name, String, :length => 255, :required => true
property :slug, String
property :seats, Integer
belongs_to :body
def self.slugify(name)
name.gsub(/[^\w\s-]/, '').gsub(/\s+/, '-').downcase
end
end

class Body
include DataMapper::Resource

property :id, Serial
property :name, String, :length => 255, :required => true
property :district_name, String, :length => 255, :required => true # singular
property :districts_name, String, :length => 255, :required => true # plural
property :slug, String, :length => 255
has n, :elections
has n, :districts
end

class Party
@@ -71,10 +141,32 @@ class Party
property :name, String, :required => true
property :colour, String
has n, :candidates, :order => ['surname']
has n, :councilcandidates, :order => ['surname']
has n, :parliamentcandidates, :order => ['surname']
end

# These models are now redundant

class Ward
include DataMapper::Resource
property :id, Serial
property :slug, String, :required => true
property :ons_id, String, :required => true
property :name, String, :required => true
property :constituency_id, Integer, :required => true
has n, :councilcandidates, :order => ['surname']
belongs_to :constituency
def self.slugify(name)
name.gsub(/[^\w\s-]/, '').gsub(/\s+/, '-').downcase
end
end

class Councilcandidate
include DataMapper::Resource
@@ -120,5 +212,5 @@ class Constituency
has n, :parliamentcandidates, :order => ['surname']
end

DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/suttonelections.db")
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/db/suttonelections.db")
DataMapper.auto_upgrade!

Loading…
取消
儲存