ソースを参照

Moved model code to /lib. Added start of candidate listings in wards and ward listing on home page

tags/last-sinatra-version
Adrian Short 14年前
コミット
200d9a89fd
4個のファイルの変更89行の追加18行の削除
  1. +21
    -16
      app.rb
  2. +48
    -0
      lib/models.rb
  3. +7
    -1
      views/home.haml
  4. +13
    -1
      views/wards.haml

+ 21
- 16
app.rb ファイルの表示

@@ -4,29 +4,22 @@ require 'pat'
require 'dm-core'
require 'dm-validations'
require 'dm-timestamps'
require 'lib/models'

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

class Postcode
include DataMapper::Resource

property :id, Integer, :serial => true
property :postcode, String, :required => true
property :created_at, DateTime
property :lat, Float
property :lng, Float
property :district_name, String
property :district_code, String
property :ward_name, String
property :ward_code, String
end

DataMapper.auto_upgrade!

get '/' do
@wards = Ward.all
haml :home
end

get '/wards/:id' do
@ward = Ward.get(params[:id])
@candidates = Councilcandidate.all( :ward_id => @ward.id )
haml :wards
end

get '/wards' do
@postcode = params[:postcode].strip.upcase
result = Pat.get(@postcode)
@@ -44,6 +37,18 @@ end
# end

get '/about' do
@accounts = %w{ adrianshort mashthestate openlylocal openelection lbsuttonnews stef stonecothill sutmoblib mysociety pezholio stonecotparking }
@accounts = %w{
adrianshort
mashthestate
openlylocal
openelection
lbsuttonnews
stef
stonecothill
sutmoblib
mysociety
pezholio
stonecotparking
}
haml :about
end

+ 48
- 0
lib/models.rb ファイルの表示

@@ -0,0 +1,48 @@
class Postcode
include DataMapper::Resource

property :id, Serial
property :postcode, String, :required => true
property :created_at, DateTime
property :lat, Float
property :lng, Float
property :district_name, String
property :district_code, String
property :ward_name, String
property :ward_code, String
end

class Ward
include DataMapper::Resource
property :id, Serial
property :ons_id, String, :required => true
property :name, String, :required => true
has n, :councilcandidates
end

class Party
include DataMapper::Resource
property :id, Serial
property :name, String, :required => true
has n, :councilcandidates
end

class Councilcandidate
include DataMapper::Resource
property :id, Serial
property :ward_id, Integer, :required => true
property :party_id, Integer, :required => true
property :forenames, String
property :surname, String
property :address, String
property :postcode, String

belongs_to :party
belongs_to :ward

end

+ 7
- 1
views/home.haml ファイルの表示

@@ -7,4 +7,10 @@
%label{ :for => "postcode" } My postcode is
%input{ :type => 'text', :name => 'postcode', :value => 'SM1 1EA', :size => 8, :maxlength => 8 }
%input{ :type => 'submit', :value => "Find it" }

%h2 Council Wards
- for ward in @wards
%p
%a{ :href => "/wards/#{ward.id.to_s}" }
= ward.name

+ 13
- 1
views/wards.haml ファイルの表示

@@ -1,3 +1,7 @@
%h1
= @ward.name
Ward

%h2
= @postcode
is in
@@ -5,4 +9,12 @@
= @ward_name
Ward
in
= @district_name
= @district_name
%h2 Council Candidates
- for candidate in @candidates
%p
= candidate.forenames
= candidate.surname
%br
= candidate.party.name

読み込み中…
キャンセル
保存