File Coverage

blib/lib/MojoMojo/Formatter/Redirect.pm
Criterion Covered Total %
statement 10 11 90.9
branch 3 6 50.0
condition 1 3 33.3
subroutine 3 3 100.0
pod 2 2 100.0
total 19 25 76.0


line stmt bran cond sub pod time code
1             package MojoMojo::Formatter::Redirect;
2              
3 27     27   63438 use parent qw/MojoMojo::Formatter/;
  27         286  
  27         159  
4              
5             =head1 NAME
6              
7             MojoMojo::Formatter::Redirect - Handles {{redirect /path}}
8              
9             =head1 DESCRIPTION
10              
11             Redirect to another page. Useful if your URL changes and
12             you want to make sure bookmarked URLs will still work:
13             C</help/tutrial> could contain:
14             C<{{redirect /help/tutorial}}>
15              
16             To edit a page that redirects, surf to $page_URL . '.edit'
17             See also http://mojomojo.ideascale.com/akira/dtd/6415-2416
18              
19             =head1 METHODS
20              
21             =head2 format_content_order
22              
23             Format order can be 1-99. The Redirect formatter runs first.
24              
25             =cut
26              
27 496     496 1 1251 sub format_content_order { 1 }
28              
29             =head2 format_content
30              
31             Calls the formatter. Takes a ref to the content as well as the
32             context object.
33              
34             =cut
35              
36             sub format_content {
37 126     126 1 2266 my ( $class, $content, $c ) = @_;
38 126 100       1053 if ( my ($page) = $$content =~ m/
39             \{\{
40             \s*redirect\s+
41             (\S+)
42             \s*}}/x
43             ) {
44 2 50 33     6 if ($c->action->name eq 'view' && !$c->ajax) {
    0          
45 2         26 $c->flash->{'redirect'}=$c->stash->{'path'};;
46 2         21 $c->res->redirect( $c->uri_for($page) );
47             } elsif ($c->action->name eq 'render') {
48 0         0 $$content=$c->loc("redirect to")." ".$page;
49             }
50             # We don't want to precompile a redirected page so turn it off
51 2         26 $c->stash->{precompile_off} = 1;
52             }
53            
54              
55             }
56              
57             =head1 SEE ALSO
58              
59             L<MojoMojo>, L<Module::Pluggable::Ordered>, L<URI::Fetch>
60              
61             =head1 AUTHORS
62              
63             Marcus Ramberg <mramberg@cpan.org>
64              
65             =head1 LICENSE
66              
67             This library is free software. You can redistribute it and/or modify
68             it under the same terms as Perl itself.
69              
70             =cut
71              
72             1;