Procházet zdrojové kódy

Added slugs for wards and /wards ward list page

tags/last-sinatra-version
Adrian Short před 14 roky
rodič
revize
f849c5872d
7 změnil soubory, kde provedl 60 přidání a 35 odebrání
  1. +28
    -19
      app.rb
  2. +19
    -0
      import-wards.rb
  3. +0
    -10
      import.rb
  4. +2
    -5
      lib/models.rb
  5. +1
    -1
      views/home.haml
  6. +2
    -0
      views/layout.haml
  7. +8
    -0
      views/wardlist.haml

+ 28
- 19
app.rb Zobrazit soubor

@@ -2,37 +2,46 @@ require 'rubygems'
require 'sinatra'
require 'sinatra-helpers/haml/partials'
require 'haml'
# require 'pat'
require 'lib/models'

get '/' do
if params[:postcode]
@postcode = params[:postcode].strip.upcase

unless result = Postcode.finder(@postcode)
# Invalid postcode
redirect '/error'
end
# Postcode valid but not in LB Sutton
if result.district_code != "00BF"
redirect '/aliens'
end
# Postcode in LB Sutton
@ward = Ward.first( :ons_id => result.ward_code )
redirect "/wards/#{@ward.slug}/postcode/#{@postcode}"
end
haml :home
end

get '/wards/:id' do
@ward = Ward.get(params[:id])
get '/wards/:slug/postcode/:postcode/?' do
@ward = Ward.first(:slug => params[:slug])
@postcode = params[:postcode]
haml :wards
end

get '/wards' do
@postcode = params[:postcode].strip.upcase
unless result = Postcode.finder(@postcode)
# Invalid postcode
redirect '/error'
end

# Postcode valid but not in LB Sutton
if result.district_code != "00BF"
redirect '/aliens'
end
# Postcode in LB Sutton
@ward = Ward.first( :ons_id => result.ward_code )
get '/wards/:slug/?' do
@ward = Ward.first(:slug => params[:slug])
haml :wards
end

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

get '/how-the-council-election-works' do
haml :election
end


+ 19
- 0
import-wards.rb Zobrazit soubor

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

# Import wards

CSV::Reader.parse(File.open('wards.csv', 'rb')) do |row|
p row
puts Ward.create!(
'ons_id' => row[0],
'name' => row[1],
'slug' => Ward.slugify(row[1]),
'constituency_id' => row[2] == 'Carshalton and Wallington' ? 1 : 2
)
end

+ 0
- 10
import.rb Zobrazit soubor

@@ -5,16 +5,6 @@ require 'dm-validations'
require 'dm-timestamps'
require 'lib/models'

# 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 = [


+ 2
- 5
lib/models.rb Zobrazit soubor

@@ -17,10 +17,6 @@ class Postcode
property :ward_name, String, :required => true
property :ward_code, String, :required => true
def postcode=(postcode)
attribute_set(:postcode, postcode.strip.upcase)
end
def self.finder(postcode)
postcode = postcode.strip.upcase
@@ -32,7 +28,7 @@ class Postcode

unless result.code == 404
# cache API result
return self.create(
self.create(
:postcode => postcode,
:lat => result['geo']['lat'],
:lng => result['geo']['lng'],
@@ -53,6 +49,7 @@ 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


+ 1
- 1
views/home.haml Zobrazit soubor

@@ -16,7 +16,7 @@
%p Who you can vote for depends on where you live. You can find your local candidates and get more information on how the elections work here.
%form{ :method => 'get', :action => '/wards' }
%form{ :method => 'get', :action => '/' }
%label{ :for => "postcode" } My postcode is
%input{ :type => 'text', :name => 'postcode', :value => 'SM1 1EA', :size => 8, :maxlength => 8 }
%input{ :type => 'submit', :value => "Find it" }

+ 2
- 0
views/layout.haml Zobrazit soubor

@@ -21,6 +21,8 @@
%a{ :href => '/how-the-parliament-election-works' } How the parliament election works
%p
%a{ :href => '/how-the-council-election-works' } How the council election works
%p
%a{ :href => '/wards' } Sutton Council wards
%p
%a{ :href => '/about' } About this website
<script type="text/javascript">


+ 8
- 0
views/wardlist.haml Zobrazit soubor

@@ -0,0 +1,8 @@
.grid_9
%h1 Sutton Council Wards
- for ward in @wards
%p.adr
%a{ :href => "/wards/#{ward.slug}" }
%span.locality
= ward.name

Načítá se…
Zrušit
Uložit