瀏覽代碼

Got the candidates to display on each ward page

tags/last-sinatra-version
Adrian Short 14 年之前
父節點
當前提交
d86a4b18b8
共有 7 個文件被更改,包括 104 次插入14 次删除
  1. +2
    -0
      app.rb
  2. +60
    -0
      import.rb
  3. +1
    -1
      lib/models.rb
  4. +6
    -1
      public/style.css
  5. +2
    -2
      views/layout.haml
  6. +15
    -10
      views/wards.haml
  7. +18
    -0
      wards.csv

+ 2
- 0
app.rb 查看文件

@@ -25,6 +25,8 @@ get '/wards' do
result = Pat.get(@postcode)
@district_name = result['administrative']['district']['title']
@ward_name = result['administrative']['ward']['title']
@ward = Ward.first( :name => @ward_name )
@candidates = Councilcandidate.all( :ward_id => @ward.id )
haml :wards
end



+ 60
- 0
import.rb 查看文件

@@ -0,0 +1,60 @@
require 'rubygems'
require 'csv'
require 'dm-core'
require 'dm-validations'
require 'dm-timestamps'
require 'lib/models'

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

# Import wards

CSV::Reader.parse(File.open('wards.csv', 'rb')) do |row|
p row
Ward.create(
'ons_id' => row[0],
'name' => row[1]
)
end

# Define parties

parties = [
"British National Party",
"Christian Peoples Alliance",
"Conservative Party",
"Green Party",
"Labour Party",
"Labour and Co-Operative Party",
"Liberal Democrats",
"United Kingdom Independence Party"
]

for party in parties
puts party
Party.create( :name => party )
end

# Import candidates

CSV::Reader.parse(File.open('../candidates-pretty.csv', 'rb')) do |row|
p row
c = Councilcandidate.new(
'forenames' => row[1],
'surname' => row[2],
'address' => row[4],
'postcode' => row[5]
)

c.ward = Ward.first( :name => row[0] )
c.party = Party.first( :name => row[3] )

unless c.save
puts "ERROR: Failed to save candidate"
c.errors.each do |e|
puts e
end
end
end

+ 1
- 1
lib/models.rb 查看文件

@@ -39,7 +39,7 @@ class Councilcandidate
property :party_id, Integer, :required => true
property :forenames, String
property :surname, String
property :address, String
property :address, String, :length => 200
property :postcode, String

belongs_to :party


+ 6
- 1
public/style.css 查看文件

@@ -65,7 +65,7 @@ form

a.date
{
font-size: 620%;
font-size: 600%;
font-weight: bold;
padding: 30px 30px;
}
@@ -78,4 +78,9 @@ a.date
h1
{
margin-top: 50px;
}

.candidate_name
{
font-size: 150%;
}

+ 2
- 2
views/layout.haml 查看文件

@@ -2,10 +2,10 @@
%html
%head
%title Sutton Council Elections 2010
%link{ :rel => 'stylesheet', :type => 'text/css', :href => 'style.css' }
%link{ :rel => 'stylesheet', :type => 'text/css', :href => '/style.css' }
%body
#header
%a.date{ :href => '/' } 6 May 2010
%a.date{ :href => '/' } 6 MAY 2010
#main
= yield
#footer


+ 15
- 10
views/wards.haml 查看文件

@@ -2,19 +2,24 @@
= @ward.name
Ward

- if @postcode
%h2
= @postcode
is in
%span.highlight
= @ward_name
Ward
in
= @district_name
%h2
= @postcode
is in
%span.highlight
= @ward_name
Ward
in
= @district_name
Council candidates in
= @ward.name
%h2 Council Candidates
- for candidate in @candidates
%p
= candidate.forenames
= candidate.surname
%span.candidate_name
= candidate.forenames
= candidate.surname
%br
= candidate.party.name

+ 18
- 0
wards.csv 查看文件

@@ -0,0 +1,18 @@
00BFGC,Beddington North
00BFGD,Beddington South
00BFGE,Belmont
00BFGF,Carshalton Central
00BFGG,Carshalton South and Clockhouse
00BFGH,Cheam
00BFGJ,Nonsuch
00BFGK,St Helier
00BFGL,Stonecot
00BFGM,Sutton Central
00BFGN,Sutton North
00BFGP,Sutton South
00BFGQ,Sutton West
00BFGR,The Wrythe
00BFGS,Wallington North
00BFGT,Wallington South
00BFGU,Wandle Valley
00BFGW,Worcester Park

Loading…
取消
儲存