File Coverage

blib/lib/Catalyst/ActionRole/RenderView.pm
Criterion Covered Total %
statement 16 17 94.1
branch 5 10 50.0
condition 1 3 33.3
subroutine 3 3 100.0
pod 0 1 0.0
total 25 34 73.5


line stmt bran cond sub pod time code
1             package Catalyst::ActionRole::RenderView;
2              
3             our $VERSION = '0.002';
4              
5 1     1   411133 use Moose::Role;
  1         3  
  1         9  
6 1     1   6414 use Catalyst::ActionRole::RenderView::Utils::NoView;
  1         435  
  1         363  
7              
8             requires 'execute';
9              
10             around 'execute', sub {
11             my ($orig, $self, $controller, $c, @args) = @_;
12             my @return = $self->$orig($controller, $c, @args);
13             $self->try_forwarding_to_view($c);
14             return @return;
15             };
16              
17             sub try_forwarding_to_view {
18 1     1 0 5 my ($self, $c) = @_;
19              
20 1 50       5 return 0 if $c->req->method eq 'HEAD'; # Don't need a response for a HEAD request
21 1 50       90 return 0 if defined $c->response->body; # Don't need a response if we have one
22 1 50       66 return 0 if scalar @{ $c->error }; # Don't try to make a response if there's an unhandled error
  1         9  
23 1 50       33 return 0 if $c->response->status =~ /^(?:204|3\d\d)$/; # Don't response if its a redirect or 'No Content' response
24            
25             # This will either be the default_view (from config) or the 'current' view, as set by
26             # stash flags 'current_view' or 'current_view_instance'.
27              
28 1   33     152 my $view = $c->view() || Catalyst::ActionRole::RenderView::Utils::NoView->throw;
29 1 50       425 $c->log->debug("Forwarding to view: @{[ $view->catalyst_component_name ]}") if $c->debug;
  0         0  
30 1         18 $c->forward($view);
31 1         1192 return 1;
32             }
33              
34             1;
35              
36             =head1 NAME
37              
38             Catalyst::ActionRole::RenderView - Call the default view
39              
40             =head1 SYNOPSIS
41              
42             package Example::Controller::Root;
43              
44             use Moose;
45             use MooseX::MethodAttributes;
46              
47             extends 'Catalyst::Controller';
48              
49             sub end : Action Does(RenderView) {}
50              
51             =head1 DESCRIPTION
52              
53             This is basically L<Catalyst::Action::RenderView> done as an action role (basically a L<Moose>
54             role) rather than as a base class. This is a bit more flexible if you plan to do fancy
55             stuff with your end action.
56              
57             Two things it doesn't do that the classic L<Catalyst::Action::RenderView> does is it doesn't
58             set a default content type if none is found (old one just set C<text/html> which was probably
59             ok back in the 'Aughts but not always true now) and we don't support the C<dump_info> when in
60             debug mode since I really think something like that belongs in another part of the stack.
61              
62             I'm willing to be proven wrong, just send me your use cases and patches.
63              
64             =head1 EXCEPTIONS
65              
66             This class can throw the following exceptions which are compatible with L<CatalystX::Errors>
67              
68             =head2 No View found
69              
70             If there's no view found when calling '$c->view()' we throw L<Catalyst::ActionRole::RenderView::Utils::NoView>.
71             You can use L<CatalystX::Errors> to catch and handle that or roll your own error handling.
72              
73             =head1 AUTHOR
74              
75             John Napiorkowski <jnapiork@cpan.org>
76            
77             =head1 COPYRIGHT
78            
79             Copyright (c) 2023 the above named AUTHOR
80            
81             =head1 LICENSE
82            
83             You may distribute this code under the same terms as Perl itself.
84            
85             =cut