Browse Source

Look up postcodes to find the user's ward

tags/last-sinatra-version
Adrian Short 10 years ago
parent
commit
92f6d1a4a6
3 changed files with 45 additions and 40 deletions
  1. +25
    -17
      app.rb
  2. +11
    -0
      public/style.css
  3. +9
    -23
      views/index.haml

+ 25
- 17
app.rb View File

@@ -2,8 +2,11 @@ require 'rubygems'
require 'sinatra'
require 'haml'
require './models'
require 'rack-flash'

set :root, File.dirname(__FILE__)
enable :sessions
use Rack::Flash

class String
def pluralize(num)
@@ -62,24 +65,29 @@ helpers do
end

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
if params[:postcode]
@postcode = params[:postcode].strip.upcase

if @p = Postcode.get(@postcode)
# Postcode is valid and in LB Sutton
@ward = Ward.get(@p.ward_id)
flash[:notice] = "Postcode <strong>#{@postcode}</strong> is in #{@ward.name} ward"
redirect "/bodies/sutton-council/wards/#{@ward.slug}"
else
flash.now[:error] = "<strong>#{@postcode}</strong> is not a postcode in Sutton"
end
end
# Display a random postcode as default search term
@random_pc = repository(:default).adapter.select("
SELECT postcode
FROM postcodes
ORDER BY RANDOM()
LIMIT 1
")

@default_pc = @random_pc[0]

@future_elections = Election.future
@past_elections = Election.past
haml :index


+ 11
- 0
public/style.css View File

@@ -180,6 +180,17 @@ tr
border: 0;
}

#lookup {
background-color: beige;
padding: 10px 25px;
}

#error {
background-color: red;
color: white;
padding: 10px 20px;
}

/* Wardmap */

.wardmap tr, .wardmap td.blank


+ 9
- 23
views/index.haml View File

@@ -1,5 +1,13 @@
%h1 Sutton Elections

#lookup
%h2
Vote for your local councillors in Sutton on 22 May 2014
%form{ :method => 'get', :action => '/' }
%label{ :for => "postcode" } My postcode is
%input{ :type => 'text', :name => 'postcode', :value => @default_pc, :size => 8, :maxlength => 8 }
%input{ :type => 'submit', :value => "Find my candidates" }

- if @future_elections.size > 0

%h2 Forthcoming elections
@@ -88,26 +96,4 @@
%td.blank
%td.blank

-#
%h1
Vote for your MP and
%br
councillors in Sutton
%p
On 6 May 2010 you can vote to
%strong choose your local MP
and help to decide the next government of the country.
%p
You can also
%strong elect your local councillors
and help to decide who runs Sutton Council.
%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 => '/' }
%label{ :for => "postcode" } My postcode is
%input{ :type => 'text', :name => 'postcode', :value => 'SM1 1EA', :size => 8, :maxlength => 8 }
%input{ :type => 'submit', :value => "Find it" }


Loading…
Cancel
Save