aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers/body_controller.rb
blob: ba90f317f6aa193cc57e9d6827291c439452041f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# app/controllers/body_controller.rb:
# Show information about a public body.
#
# 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.3 2007-10-31 12:39:58 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 => [ "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 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
            if @public_bodies.size == 0
                raise "None found" # XXX proper 404
            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

end