diff --git a/app.rb b/app.rb index 0790e6f..0d62a4a 100644 --- a/app.rb +++ b/app.rb @@ -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 diff --git a/import-wards.rb b/import-wards.rb new file mode 100644 index 0000000..a4843da --- /dev/null +++ b/import-wards.rb @@ -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 \ No newline at end of file diff --git a/import.rb b/import.rb index 01f297f..04cc71c 100644 --- a/import.rb +++ b/import.rb @@ -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 = [ diff --git a/lib/models.rb b/lib/models.rb index 29a9126..71d4bec 100644 --- a/lib/models.rb +++ b/lib/models.rb @@ -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 diff --git a/views/home.haml b/views/home.haml index 8677ae6..80cb0ce 100644 --- a/views/home.haml +++ b/views/home.haml @@ -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" } \ No newline at end of file diff --git a/views/layout.haml b/views/layout.haml index dd0ca6d..35c3908 100644 --- a/views/layout.haml +++ b/views/layout.haml @@ -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