File Coverage

blib/lib/Catalyst/Action/RenderView.pm
Criterion Covered Total %
statement 30 38 78.9
branch 10 18 55.5
condition 5 13 38.4
subroutine 6 7 85.7
pod 1 1 100.0
total 52 77 67.5


line stmt bran cond sub pod time code
1             package Catalyst::Action::RenderView;
2              
3 3     3   3186566 use strict;
  3         16  
  3         135  
4 3     3   19 use warnings;
  3         7  
  3         312  
5              
6             our $VERSION = '0.17';
7              
8 3     3   26 use base 'Catalyst::Action';
  3         14  
  3         2486  
9              
10 3     3   1596421 use MRO::Compat;
  3         9  
  3         115  
11 3     3   2284 use Data::Visitor::Callback;
  3         253822  
  3         1798  
12              
13             my %ignore_classes = ();
14              
15             sub execute {
16 4     4 1 507451 my $self = shift;
17 4         15 my ($controller, $c ) = @_;
18 4         35 $self->next::method( @_ );
19              
20             $c->config->{'Action::RenderView'}->{ignore_classes} =
21             ( ref($c->config->{'debug'}) eq 'HASH' ? $c->config->{'debug'}->{ignore_classes} : undef )
22             || [ qw/
23             DBIx::Class::ResultSource::Table
24             DBIx::Class::ResultSourceHandle
25             DateTime
26 4 100 50     290 / ] unless exists $c->config->{'Action::RenderView'}->{ignore_classes};
27              
28             $c->config->{'Action::RenderView'}->{scrubber_func} =
29             ( ref($c->config->{'debug'}) eq 'HASH' ? $c->config->{'debug'}->{scrubber_func} : undef )
30 0     0     || sub { $_='[stringified to: ' . $_ . ']' }
31 4 100 33     708 unless exists $c->config->{'Action::RenderView'}->{scrubber_func};
32              
33 4 0 33     558 if ($c->debug && $c->req->params->{dump_info}) {
34 0 0       0 unless ( keys %ignore_classes ) {
35 0         0 foreach my $class (@{$c->config->{'Action::RenderView'}->{ignore_classes}}) {
  0         0  
36 0         0 $ignore_classes{$class} = $c->config->{'Action::RenderView'}->{scrubber_func};
37             }
38             }
39 0         0 my $scrubber=Data::Visitor::Callback->new(
40             "ignore_return_values" => 1,
41             "object" => "visit_ref",
42             %ignore_classes,
43             );
44 0         0 $scrubber->visit( $c->stash );
45 0         0 die('Forced debug - Scrubbed output');
46             }
47              
48 4 50       162 if(! $c->response->content_type ) {
49 4         1459 $c->response->content_type( 'text/html; charset=utf-8' );
50             }
51 4 50       1025 return 1 if $c->req->method eq 'HEAD';
52 4 100       404 return 1 if defined $c->response->body;
53 2 50 33     77 return 1 if scalar @{ $c->error } && !$c->stash->{template};
  2         7  
54 2 50       63 return 1 if $c->response->status =~ /^(?:204|3\d\d)$/;
55 2   50     299 my $view = $c->view()
56             || die "Catalyst::Action::RenderView could not find a view to forward to.\n";
57 2         882 $c->forward( $view );
58             };
59              
60             1;
61              
62             __END__
63              
64             =pod
65              
66             =encoding UTF-8
67              
68             =for :stopwords Marcus Ramberg Florian Ragwitz and 3xx ResultSource
69              
70             =head1 NAME
71              
72             Catalyst::Action::RenderView - Sensible default end action.
73              
74             =head1 SYNOPSIS
75              
76             sub end : ActionClass('RenderView') {}
77              
78             =head1 DESCRIPTION
79              
80             This action implements a sensible default end action, which will forward
81             to the first available view, unless C<< $c->res->status >> is a 3xx code
82             (redirection, not modified, etc.), 204 (no content), or C<< $c->res->body >> has
83             already been set. It also allows you to pass C<dump_info=1> to the url in
84             order to force a debug screen, while in debug mode.
85              
86             If you have more than one view, you can specify which one to use with
87             the C<default_view> config setting and the C<current_view> and
88             C<current_view_instance> stash keys (see L<Catalyst>'s C<$c-E<gt>view($name)>
89             method -- this module simply calls C<< $c->view >> with no argument).
90              
91             =head1 METHODS
92              
93             =head2 end
94              
95             The default C<end> action. You can override this as required in your
96             application class; normal inheritance applies.
97              
98             =head1 INTERNAL METHODS
99              
100             =head2 execute
101              
102             Dispatches control to superclasses, then forwards to the default View.
103              
104             See L<Catalyst::Action/METHODS/action>.
105              
106             =head1 SCRUBBING OUTPUT
107              
108             When you force debug with dump_info=1, RenderView is capable of removing
109             classes from the objects in your stash. By default it will replace any
110             DBIx::Class ResultSource objects with the class name, which cleans up the
111             debug output considerably, but you can change what gets scrubbed by
112             setting a list of classes in
113             $c->config->{'Action::RenderView'}->{ignore_classes}.
114             For instance:
115              
116             $c->config->{'Action::RenderView'}->{ignore_classes} = [];
117              
118             To disable the functionality. You can also set
119             config->{'Action::RenderView'}->{scrubber_func} to change what it does with the
120             classes. For instance, this will undef it instead of putting in the
121             class name:
122              
123             $c->config->{'Action::RenderView'}->{scrubber_func} = sub { undef $_ };
124              
125             =head2 Deprecation notice
126              
127             This plugin used to be configured by setting C<< $c->config->{debug} >>.
128             That configuration key is still supported in this release, but is
129             deprecated, please use the C< 'Action::RenderView' > namespace as shown
130             above for configuration in new code.
131              
132             =head1 EXTENDING
133              
134             To add something to an C<end> action that is called before rendering,
135             simply place it in the C<end> method:
136              
137             sub end : ActionClass('RenderView') {
138             my ( $self, $c ) = @_;
139             # do stuff here; the RenderView action is called afterwards
140             }
141              
142             To add things to an C<end> action that are called I<after> rendering,
143             you can set it up like this:
144              
145             sub render : ActionClass('RenderView') { }
146              
147             sub end : Private {
148             my ( $self, $c ) = @_;
149             $c->forward('render');
150             # do stuff here
151             }
152              
153             =head1 AUTHORS
154              
155             =over 4
156              
157             =item *
158              
159             Marcus Ramberg <marcus@thefeed.no>
160              
161             =item *
162              
163             Florian Ragwitz <rafl@debian.org>
164              
165             =back
166              
167             =head1 CONTRIBUTOR
168              
169             =for stopwords Graham Knop
170              
171             Graham Knop <haarg@haarg.org>
172              
173             =head1 COPYRIGHT AND LICENSE
174              
175             This software is copyright (c) 2006 - 2009 by Marcus Ramberg and Florian Ragwitz.
176              
177             This is free software; you can redistribute it and/or modify it under
178             the same terms as the Perl 5 programming language system itself.
179              
180             =cut