Procházet zdrojové kódy

Tidied up app.rb by using chained model associations properly

tags/last-sinatra-version
Adrian Short před 14 roky
rodič
revize
ed3f23628d
6 změnil soubory, kde provedl 29 přidání a 27 odebrání
  1. +6
    -16
      app.rb
  2. +15
    -6
      lib/models.rb
  3. +2
    -1
      views/_register-to-vote.haml
  4. +2
    -1
      views/aliens.haml
  5. +2
    -1
      views/error.haml
  6. +2
    -2
      views/wards.haml

+ 6
- 16
app.rb Zobrazit soubor

@@ -3,9 +3,6 @@ require 'sinatra'
require 'sinatra-helpers/haml/partials'
require 'haml'
require 'pat'
require 'dm-core'
require 'dm-validations'
require 'dm-timestamps'
require 'lib/models'

get '/' do
@@ -14,33 +11,26 @@ end

get '/wards/:id' do
@ward = Ward.get(params[:id])
@council_candidates = Councilcandidate.all( :ward_id => @ward.id, :order => [ 'surname' ] )
@parly_candidates = Parliamentcandidate.all( :constituency_id => @ward.constituency.id, :order => [ 'surname' ])
haml :wards
end

get '/wards' do
@postcode = params[:postcode].strip.upcase
# Postcode not found/invalid
# Postcode valid but not in LB Sutton
result = Pat.get(@postcode)

# Invalid postcode
if result.code == 404
redirect '/error'
end

@district_name = result['administrative']['district']['title']
if @district_name != "Sutton London Borough Council"
# Postcode valid but not in LB Sutton
if result['administrative']['district']['title'] != "Sutton London Borough Council"
redirect '/aliens'
end
@ward_name = result['administrative']['ward']['title']
@ward = Ward.first( { :name => @ward_name } )
@council_candidates = Councilcandidate.all( :ward_id => @ward.id, :order => [ 'surname' ])
@parly_candidates = Parliamentcandidate.all( :constituency_id => @ward.constituency.id, :order => [ 'surname' ])
# Postcode in LB Sutton
@ward = Ward.first( :name => result['administrative']['ward']['title'] )
haml :wards
end



+ 15
- 6
lib/models.rb Zobrazit soubor

@@ -1,3 +1,7 @@
require 'dm-core'
require 'dm-validations'
require 'dm-timestamps'

class Postcode
include DataMapper::Resource

@@ -19,9 +23,14 @@ class Ward
property :ons_id, String, :required => true
property :name, String, :required => true
property :constituency_id, Integer, :required => true
has n, :councilcandidates
has n, :councilcandidates, :order => 'surname'
belongs_to :constituency
def self.slugify(name)
name.gsub(/[^\w\s-]/, '').gsub(/\s+/, '-').downcase
end
end

class Party
@@ -30,8 +39,8 @@ class Party
property :id, Serial
property :name, String, :required => true
has n, :councilcandidates
has n, :parliamentcandidates
has n, :councilcandidates, :order => 'surname'
has n, :parliamentcandidates, :order => 'surname'
end

class Councilcandidate
@@ -70,8 +79,8 @@ class Constituency
property :id, Serial
property :name, String, :required => true
has n, :wards
has n, :parliamentcandidates
has n, :wards, :order => 'name'
has n, :parliamentcandidates, :order => 'surname'
end

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

+ 2
- 1
views/_register-to-vote.haml Zobrazit soubor

@@ -9,4 +9,5 @@
You can get a registration form at the
%a{ :href => "http://www.aboutmyvote.co.uk/" } About My Vote
website or by phoning Sutton Council on
%span.phone 020 8770 4888.
%span.phone 020 8770 4888.

+ 2
- 1
views/aliens.haml Zobrazit soubor

@@ -3,4 +3,5 @@
%p This site only has information about elections in the London Borough of Sutton.
%p Your postcode is outside Sutton. Sorry, we can't help you further.
%p Your postcode is outside Sutton. Sorry, we can't help you further.

+ 2
- 1
views/error.haml Zobrazit soubor

@@ -1,4 +1,5 @@
.grid_12
%h1 Invalid postcode
%p
%a{ :href => "/" }Please go back and try again
%a{ :href => "/" }Please go back and try again

+ 2
- 2
views/wards.haml Zobrazit soubor

@@ -44,7 +44,7 @@
.clear

.grid_6
- for candidate in @parly_candidates
- for candidate in @ward.constituency.parliamentcandidates
%p.vcard
%span.candidate_name.fn
= candidate.forenames.split(' ')[0]
@@ -61,7 +61,7 @@
MP.

.grid_6
- for candidate in @council_candidates
- for candidate in @ward.councilcandidates
%p.vcard
%span.candidate_name.fn
= candidate.forenames.split(' ')[0]


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