diff options
Diffstat (limited to 'app/controllers')
-rw-r--r-- | app/controllers/application.rb | 14 | ||||
-rw-r--r-- | app/controllers/body_controller.rb | 17 |
2 files changed, 27 insertions, 4 deletions
diff --git a/app/controllers/application.rb b/app/controllers/application.rb index 0241ec66e..2affefa96 100644 --- a/app/controllers/application.rb +++ b/app/controllers/application.rb @@ -6,7 +6,7 @@ # Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. # Email: francis@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: application.rb,v 1.16 2007-10-24 11:39:37 francis Exp $ +# $Id: application.rb,v 1.17 2007-10-30 18:52:27 francis Exp $ class ApplicationController < ActionController::Base @@ -138,6 +138,18 @@ class ApplicationController < ActionController::Base return request.env["REMOTE_USER"] end end + + # Simplified links to our objects + # XXX See controllers/user_controller.rb controllers/body_controller.rb for inverse + # XXX consolidate somehow with stuff in helpers/application_helper.rb + # use :helper_method => :your_method_name + def simplify_url_part(text) + text.downcase! + text.gsub!(/ /, "-") + text.gsub!(/[^a-z0-9_-]/, "") + text + end + end diff --git a/app/controllers/body_controller.rb b/app/controllers/body_controller.rb index 2027e13a1..0a267118a 100644 --- a/app/controllers/body_controller.rb +++ b/app/controllers/body_controller.rb @@ -4,13 +4,24 @@ # Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. # Email: francis@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: body_controller.rb,v 1.1 2007-10-11 22:01:36 francis Exp $ +# $Id: body_controller.rb,v 1.2 2007-10-30 18:52:27 francis Exp $ class BodyController < ApplicationController + # XXX tidy this up with better error messages, and a more standard infrastructure for the redirect to canonical URL def show - @public_bodies = PublicBody.find(:all, :conditions => [ "short_name = ?", params[:short_name] ]) + @public_bodies = PublicBody.find(:all, + :conditions => [ "regexp_replace(replace(lower(short_name), ' ', '-'), '[^a-z0-9_-]', '', 'g') = ?", params[:simple_short_name] ]) if @public_bodies.size > 1 - raise "Two bodies with the same short name: " . params[:short_name] + raise "Two bodies with the same simplified short name: " . params[:simple_short_name] + end + # If none found, then search the history of short names, and do a redirect + if @public_bodies.size == 0 + @public_bodies = PublicBody.find(:all, + :conditions => [ "id in (select public_body_id from public_body_versions where regexp_replace(replace(lower(short_name), ' ', '-'), '[^a-z0-9_-]', '', 'g') = ?)", params[:simple_short_name] ]) + if @public_bodies.size > 1 + raise "Two bodies with the same historical simplified short name: " . params[:simple_short_name] + end + redirect_to show_public_body_url(:simple_short_name => simplify_url_part(@public_bodies[0].short_name)) end @public_body = @public_bodies[0] end |