Skip to content

Commit

Permalink
Add generic static route handler.
Browse files Browse the repository at this point in the history
This puts all static pages under /about, and lets you add new static
pages by placing new HTML in your about template directory.
  • Loading branch information
dracos committed Oct 29, 2015
1 parent 783eb40 commit 2a6e538
Show file tree
Hide file tree
Showing 39 changed files with 85 additions and 42 deletions.
67 changes: 67 additions & 0 deletions perllib/FixMyStreet/App/Controller/About.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package FixMyStreet::App::Controller::About;
use Moose;
use namespace::autoclean;

BEGIN { extends 'Catalyst::Controller'; }

=head1 NAME
FixMyStreet::App::Controller::About - Catalyst Controller
=head1 DESCRIPTION
About pages Catalyst Controller.
=head1 METHODS
=cut

my %found;

sub page : Path("/about") : Args(1) {
my ( $self, $c, $page ) = @_;
my $template = $c->forward('find_template');
$c->detach('/page_error_404_not_found', []) unless $template;
$c->stash->{template} = $template;
}

sub index : Path("/about") : Args(0) {
my ( $self, $c ) = @_;
$c->forward('page', [ 'about' ]);
}

# We have multiple possibilities to try, and we want to cache where we find it
sub find_template : Private {
my ( $self, $c, $page ) = @_;

return $found{$page} if !FixMyStreet->config('STAGING_SITE') && exists $found{$page};

my $lang_code = $c->stash->{lang_code};
foreach my $dir_templates (@{$c->stash->{additional_template_paths}}, @{$c->view('Web')->paths}) {
foreach my $dir_static (static_dirs($page, $dir_templates)) {
foreach my $file ("$page-$lang_code.html", "$page.html") {
if (-e "$dir_templates/$dir_static/$file") {
$found{$page} = "$dir_static/$file";
return $found{$page};
}
}
}
}
# Cache that the page does not exist, so we don't look next time
$found{$page} = undef;
return $found{$page};
}

sub static_dirs {
my ($page, $dir_templates) = @_;
my @v = ("about");
# If legacy directories exist, check for templates there too;
# The FAQ page used to be in its own directory
push @v, "static" if -d "$dir_templates/static";
push @v, "faq" if -d "$dir_templates/faq" && $page =~ /faq/;
return @v;
}

__PACKAGE__->meta->make_immutable;

1;
51 changes: 9 additions & 42 deletions perllib/FixMyStreet/App/Controller/Static.pm
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,23 @@ FixMyStreet::App::Controller::Static - Catalyst Controller
=head1 DESCRIPTION
Static pages Catalyst Controller. FAQ does some smarts to choose the correct
template depending on language, will need extending at some point.
Old static pages Catalyst Controller.
=head1 METHODS
=cut

sub about : Global : Args(0) {
my ( $self, $c ) = @_;

my $lang_code = $c->stash->{lang_code};
my $template = "static/about-$lang_code.html";
$c->stash->{template} = $template;
}

sub privacy : Global : Args(0) {
sub about_redirect : Private {
my ( $self, $c ) = @_;
$c->res->redirect( $c->uri_for_action('/about/page', [ $c->action->name ] ));
}

sub faq : Global : Args(0) {
my ( $self, $c ) = @_;

# There should be a faq template for each language in a cobrand or default.
# This is because putting the FAQ translations into the PO files is
# overkill.

# We rely on the list of languages for the site being restricted so that there
# will be a faq template for that language/cobrand combo.

my $lang_code = $c->stash->{lang_code};
my $template = "faq/faq-$lang_code.html";
$c->stash->{template} = $template;
}

sub fun : Global : Args(0) {
my ( $self, $c ) = @_;
# don't need to do anything here - should just pass through.
}

sub posters : Global : Args(0) {
my ( $self, $c ) = @_;
}

sub iphone : Global : Args(0) {
my ( $self, $c ) = @_;
}

sub council : Global : Args(0) {
my ( $self, $c ) = @_;
}
sub faq : Global : Args(0) { $_[1]->forward('/about/page', ['faq']) }
sub privacy : Global : Args(0) { $_[1]->detach('about_redirect') }
sub fun : Global : Args(0) { $_[1]->detach('about_redirect') }
sub posters : Global : Args(0) { $_[1]->detach('about_redirect') }
sub iphone : Global : Args(0) { $_[1]->detach('about_redirect') }
sub council : Global : Args(0) { $_[1]->detach('about_redirect') }

sub unresponsive : Global : Args(0) {
my ( $self, $c ) = @_;
Expand Down
9 changes: 9 additions & 0 deletions t/app/controller/about.t
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ $mech->get_ok('/about');
$mech->content_like(qr{About us ::\s+FixMyStreet});
$mech->content_contains('html class="no-js" lang="en-gb"');

$mech->get_ok('/privacy');
is $mech->res->code, 200, "got 200 for final destination";
is $mech->res->previous->code, 302, "got 302 for redirect";
is $mech->uri->path, '/about/privacy';

$mech->get('/about/page-that-does-not-exist');
ok !$mech->res->is_success(), "want a bad response";
is $mech->res->code, 404, "got 404";

FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'emptyhomes' ],
}, sub {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 2a6e538

Please sign in to comment.