From ed3f23628d8a1671d13f63cc758af2acebcf8e10 Mon Sep 17 00:00:00 2001 From: Adrian Short Date: Fri, 16 Apr 2010 20:15:45 +0100 Subject: [PATCH] Tidied up app.rb by using chained model associations properly --- app.rb | 22 ++++++---------------- lib/models.rb | 21 +++++++++++++++------ views/_register-to-vote.haml | 3 ++- views/aliens.haml | 3 ++- views/error.haml | 3 ++- views/wards.haml | 4 ++-- 6 files changed, 29 insertions(+), 27 deletions(-) diff --git a/app.rb b/app.rb index e266108..0633af0 100644 --- a/app.rb +++ b/app.rb @@ -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 diff --git a/lib/models.rb b/lib/models.rb index a22b221..ef749d8 100644 --- a/lib/models.rb +++ b/lib/models.rb @@ -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") diff --git a/views/_register-to-vote.haml b/views/_register-to-vote.haml index eed36d8..9175e39 100644 --- a/views/_register-to-vote.haml +++ b/views/_register-to-vote.haml @@ -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. \ No newline at end of file + %span.phone 020 8770 4888. + \ No newline at end of file diff --git a/views/aliens.haml b/views/aliens.haml index a82251b..626e371 100644 --- a/views/aliens.haml +++ b/views/aliens.haml @@ -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. \ No newline at end of file + %p Your postcode is outside Sutton. Sorry, we can't help you further. + \ No newline at end of file diff --git a/views/error.haml b/views/error.haml index 90d4425..2413721 100644 --- a/views/error.haml +++ b/views/error.haml @@ -1,4 +1,5 @@ .grid_12 %h1 Invalid postcode %p - %a{ :href => "/" }Please go back and try again \ No newline at end of file + %a{ :href => "/" }Please go back and try again + \ No newline at end of file diff --git a/views/wards.haml b/views/wards.haml index efc7a13..77a0fb0 100644 --- a/views/wards.haml +++ b/views/wards.haml @@ -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]