Skip to content

Commit

Permalink
improve helper method in Mojolicious to replace already existing help…
Browse files Browse the repository at this point in the history
…ers silently
  • Loading branch information
kraih committed Nov 19, 2015
1 parent 839a9fe commit 2df1437
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 18 deletions.
2 changes: 2 additions & 0 deletions Changes
@@ -1,6 +1,8 @@

6.33 2015-11-19
- Updated IO::Socket::IP requirement to 0.37 for certain bug fixes.
- Improved helper method in Mojolicious to replace already existing helpers
silently.

6.32 2015-11-18
- Deprecated Mojolicious::Routes::Pattern::format_regex.
Expand Down
8 changes: 1 addition & 7 deletions lib/Mojolicious.pm
Expand Up @@ -130,13 +130,7 @@ sub handler {
unless $c->tx->is_writing;
}

sub helper {
my ($self, $name, $cb) = @_;
my $r = $self->renderer;
$self->log->debug(qq{Helper "$name" already exists, replacing})
if exists $r->helpers->{$name};
$r->add_helper($name => $cb);
}
sub helper { shift->renderer->add_helper(@_) }

sub hook { shift->plugins->on(@_) }

Expand Down
13 changes: 2 additions & 11 deletions t/mojolicious/app.t
Expand Up @@ -140,15 +140,6 @@ ok !!$t->app->plugins->emit_hook('does_not_exist'), 'hook has been emitted';
ok !!$t->app->plugins->emit_hook_reverse('does_not_exist'),
'hook has been emitted';

# Replaced helper
my $log = '';
my $cb = $t->app->log->on(message => sub { $log .= pop });
$t->app->helper(replaced_helper => sub { });
$t->app->helper(replaced_helper => sub { });
like $log, qr/Helper "replaced_helper" already exists, replacing/,
'right message';
$t->app->log->unsubscribe(message => $cb);

# Custom hooks
my $custom;
$t->app->hook('custom_hook' => sub { $custom += shift });
Expand All @@ -169,8 +160,8 @@ $t->get_ok('/plugin-test-some_plugin2/register')->status_isnt(500)
->content_unlike(qr/Something/)->content_like(qr/Page not found/);

# Plugin::Test::SomePlugin2::register (security violation again)
$log = '';
$cb = $t->app->log->on(message => sub { $log .= pop });
my $log = '';
my $cb = $t->app->log->on(message => sub { $log .= pop });
$t->get_ok('/plugin-test-some_plugin2/register')->status_isnt(500)
->status_is(404)->header_is(Server => 'Mojolicious (Perl)')
->content_unlike(qr/Something/)->content_like(qr/Page not found/);
Expand Down

0 comments on commit 2df1437

Please sign in to comment.