diff --git a/app.rb b/app.rb index 5940c69..71e48f9 100644 --- a/app.rb +++ b/app.rb @@ -10,16 +10,16 @@ get '/' do haml :home end -get '/directorates/:id' do - @directorate = Directorate.get(params[:id]) +get '/directorates/:slug' do + @directorate = Directorate.first(:slug => params[:slug]) @total = @directorate.payments.sum(:amount) haml :directorate end -get '/suppliers/:id.csv' do - @supplier = Supplier.get(params[:id]) +get '/suppliers/:slug.csv' do + @supplier = Supplier.first(:slug => params[:slug]) - headers "Content-Disposition" => "attachment;filename=supplier#{@supplier.id}.csv", + headers "Content-Disposition" => "attachment;filename=supplier-#{@supplier.slug}.csv", "Content-Type" => "application/octet-stream" result = "Date,Trans No,Directorate,Service,Amount ex. VAT\n" @@ -32,8 +32,8 @@ get '/suppliers/:id.csv' do end -get '/suppliers/:id' do - @supplier = Supplier.get(params[:id]) +get '/suppliers/:slug' do + @supplier = Supplier.first(:slug => params[:slug]) @total = @supplier.payments.sum(:amount) haml :supplier end @@ -43,10 +43,10 @@ get '/suppliers/?' do haml :suppliers end -get '/services/:id.csv' do - @service = Service.get(params[:id]) +get '/services/:slug.csv' do + @service = Service.first(:slug => params[:slug]) - headers "Content-Disposition" => "attachment;filename=service#{@service.id}.csv", + headers "Content-Disposition" => "attachment;filename=service-#{@service.slug}.csv", "Content-Type" => "application/octet-stream" result = "Date,Trans No,Directorate,Supplier,Amount ex. VAT\n" @@ -59,8 +59,8 @@ get '/services/:id.csv' do end -get '/services/:id' do - @service = Service.get(params[:id]) +get '/services/:slug' do + @service = Service.first(:slug => params[:slug]) @total = @service.payments.sum(:amount) haml :service end @@ -70,17 +70,6 @@ get '/services/?' do haml :services end -get '/wards/:slug/postcode/:postcode/?' do - @ward = Ward.first(:slug => params[:slug]) - @postcode = params[:postcode] - haml :wards -end - -get '/wards/:slug/?' do - @ward = Ward.first(:slug => params[:slug]) - haml :wards -end - get '/error' do haml :error end diff --git a/lib/models.rb b/lib/models.rb index 37c6c4a..2118b0e 100644 --- a/lib/models.rb +++ b/lib/models.rb @@ -28,8 +28,14 @@ class Directorate property :id, Serial property :name, String, :length => 255, :required => true + property :slug, String, :length => 255 has n, :payments, :order => ['d'] + + def self.slugify(name) + name.gsub(/[^\w\s-]/, '').gsub(/\s+/, '-').downcase + end + end class Service @@ -37,8 +43,14 @@ class Service property :id, Serial property :name, String, :length => 255, :required => true + property :slug, String, :length => 255 has n, :payments, :order => ['d'] + + def self.slugify(name) + name.gsub(/[^\w\s-]/, '').gsub(/\s+/, '-').downcase + end + end class Supplier @@ -46,12 +58,13 @@ class Supplier property :id, Serial property :name, String, :length => 255, :required => true + property :slug, String, :length => 255 has n, :payments, :order => ['d'] -# def self.slugify(name) -# name.gsub(/[^\w\s-]/, '').gsub(/\s+/, '-').downcase -# end + def self.slugify(name) + name.gsub(/[^\w\s-]/, '').gsub(/\s+/, '-').downcase + end end DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/db.sqlite3") diff --git a/views/directorate.haml b/views/directorate.haml index 72e228a..11af3b4 100644 --- a/views/directorate.haml +++ b/views/directorate.haml @@ -20,10 +20,10 @@ %tr %td= payment.d.strftime("%d %b %Y") %td - %a{ :href => '/services/' + payment.service.id.to_s } + %a{ :href => '/services/' + payment.service.slug } = payment.service.name %td - %a{ :href => '/suppliers/' + payment.supplier.id.to_s } + %a{ :href => '/suppliers/' + payment.supplier.slug } = payment.supplier.name %td.right= sprintf("%0d", payment.amount) diff --git a/views/home.haml b/views/home.haml index 09e392a..d2be2df 100644 --- a/views/home.haml +++ b/views/home.haml @@ -7,5 +7,5 @@ - for directorate in @directorates %p - %a{ :href=> "/directorates/#{directorate.id}" } + %a{ :href=> "/directorates/#{directorate.slug}" } = directorate.name diff --git a/views/service.haml b/views/service.haml index e55f1d2..3c36a0c 100644 --- a/views/service.haml +++ b/views/service.haml @@ -9,7 +9,7 @@ %li = @service.name %p.noprint.download - %a{ :href => "/services/#{@service.id}.csv" } + %a{ :href => "/services/#{@service.slug}.csv" } Download data as CSV %table @@ -23,10 +23,10 @@ %tr %td= payment.d.strftime("%d %b %Y") %td - %a{ :href => '/directorates/' + payment.directorate.id.to_s } + %a{ :href => '/directorates/' + payment.directorate.slug } = payment.directorate.name %td - %a{ :href => '/suppliers/' + payment.supplier.id.to_s } + %a{ :href => '/suppliers/' + payment.supplier.slug } = payment.supplier.name %td.right= sprintf("%0d", payment.amount) diff --git a/views/services.haml b/views/services.haml index 4bb8744..6e08916 100644 --- a/views/services.haml +++ b/views/services.haml @@ -9,5 +9,5 @@ - for service in @services %p - %a{ :href=> "/services/#{service.id}" } + %a{ :href=> "/services/#{service.slug}" } = service.name diff --git a/views/supplier.haml b/views/supplier.haml index d62cfe4..56aa45a 100644 --- a/views/supplier.haml +++ b/views/supplier.haml @@ -10,7 +10,7 @@ = @supplier.name %p.noprint.download - %a{ :href => "/suppliers/#{@supplier.id}.csv" } + %a{ :href => "/suppliers/#{@supplier.slug}.csv" } Download data as CSV %table @@ -24,10 +24,10 @@ %tr %td= payment.d.strftime("%d %b %Y") %td - %a{ :href => '/directorates/' + payment.directorate.id.to_s } + %a{ :href => '/directorates/' + payment.directorate.slug } = payment.directorate.name %td - %a{ :href => '/services/' + payment.service.id.to_s } + %a{ :href => '/services/' + payment.service.slug } = payment.service.name %td.right= sprintf("%0d", payment.amount) diff --git a/views/suppliers.haml b/views/suppliers.haml index 8870655..23ea4e3 100644 --- a/views/suppliers.haml +++ b/views/suppliers.haml @@ -9,5 +9,5 @@ - for supplier in @suppliers %p - %a{ :href=> "/suppliers/#{supplier.id}" } + %a{ :href=> "/suppliers/#{supplier.slug}" } = supplier.name