Skip to content

Commit

Permalink
more tests for around_action hook
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jun 18, 2013
1 parent 92fb02f commit 8b72d27
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 31 deletions.
38 changes: 19 additions & 19 deletions lib/Mojolicious.pm
Expand Up @@ -512,6 +512,25 @@ served and before the router starts its work.
Mostly used for custom dispatchers and collecting metrics. (Passed the default
controller object)
=item around_action
Emitted right before an action gets invoked and wraps around it, so you have
to manually forward to the next hook if you want to continue the chain.
Default action dispatching is the last hook in the chain, yours will run
before it.
$app->hook(around_action => sub {
my ($next, $c, $action, $last) = @_;
...
return $next->();
});
This is a very powerful hook and should not be used lightly, it allows you for
example to pass additional arguments to actions or handle return values
differently. (Passed a callback leading to the next hook, the current
controller object, the action callback and a flag indicating if this action is
an endpoint)
=item after_render
Emitted after content has been generated by the renderer that is not partial.
Expand Down Expand Up @@ -541,25 +560,6 @@ applications will only work for the application that is rendering.
Useful for rewriting outgoing responses and other post-processing tasks.
(Passed the current controller object)
=item around_action
Emitted right before an action gets invoked and wraps around it, so you have
to manually forward to the next hook if you want to continue the chain.
Default action dispatching is the last hook in the chain, yours will run
before it.
$app->hook(around_action => sub {
my ($next, $c, $action, $last) = @_;
...
return $next->();
});
This is a very powerful hook and should not be used lightly, it allows you for
example to pass additional arguments to actions or handle return values
differently. (Passed a callback leading to the next hook, the current
controller object, the action callback and a flag indicating if this action is
an endpoint)
=item around_dispatch
Emitted right before the C<before_dispatch> hook and wraps around the whole
Expand Down
13 changes: 6 additions & 7 deletions t/mojolicious/dispatcher_lite_app.t
Expand Up @@ -6,6 +6,10 @@ BEGIN {
}

use Test::More;

use FindBin;
use lib "$FindBin::Bin/lib";

use Mojo::Message::Response;
use Mojolicious::Lite;
use Test::Mojo;
Expand Down Expand Up @@ -81,13 +85,8 @@ hook around_action => sub {
return $next->();
};

# Render return value
hook around_action => sub {
my ($next, $c, $action, $last) = @_;
my $value = $next->();
$c->render(text => $value) if $last && $c->stash->{return};
return $value;
};
# Plugin for rendering return values
plugin 'AroundPlugin';

# Pass argument to action
hook around_action => sub {
Expand Down
18 changes: 18 additions & 0 deletions t/mojolicious/lib/AroundPlugin.pm
@@ -0,0 +1,18 @@
package AroundPlugin;
use Mojo::Base 'Mojolicious::Plugin';

sub register {
my ($self, $app) = @_;

# Render return value
$app->hook(
around_action => sub {
my ($next, $c, $action, $last) = @_;
my $value = $next->();
$c->render(text => $value) if $last && $c->stash->{return};
return $value;
}
);
}

1;
5 changes: 4 additions & 1 deletion t/mojolicious/lib/MojoliciousTest.pm
Expand Up @@ -33,6 +33,9 @@ sub startup {
$self->plugin('test-some_plugin2');
$self->plugin('UPPERCASETestPlugin');

# Plugin for rendering return values
$self->plugin('AroundPlugin');

# Templateless renderer
$self->renderer->add_handler(
test => sub {
Expand Down Expand Up @@ -138,7 +141,7 @@ sub startup {
$r->route('/withblock')->to('foo#withBlock');

# /staged (authentication with bridges)
my $b = $r->bridge('/staged')->to(controller => 'foo', action => 'stage1');
my $b = $r->bridge('/staged')->to('foo#stage1', return => 1);
$b->route->to(action => 'stage2');

# /shortcut/act
Expand Down
5 changes: 1 addition & 4 deletions t/mojolicious/lib/MojoliciousTest/Foo.pm
Expand Up @@ -52,10 +52,7 @@ sub stage1 {
return undef;
}

sub stage2 {
my $self = shift;
$self->render(text => $self->some_plugin);
}
sub stage2 { return shift->some_plugin }

sub syntaxerror { shift->render('syntaxerror', format => 'html') }

Expand Down

0 comments on commit 8b72d27

Please sign in to comment.