Skip to content

Commit

Permalink
Improve route->to('controller#action') shortcuts
Browse files Browse the repository at this point in the history
Now you can say to('controller#') and to('#action') for short.
  • Loading branch information
mvuets authored and kraih committed Feb 7, 2010
1 parent 641d5e0 commit bee2086
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/MojoX/Routes.pm
Expand Up @@ -273,9 +273,9 @@ sub to {
}

# Controller and action
if ($shortcut && $shortcut =~ /^([\w\-]+)\#(\w+)$/) {
$defaults->{controller} = $1;
$defaults->{action} = $2;
if ($shortcut && $shortcut =~ /^([\w\-]+)?\#(\w+)?$/) {
$defaults->{controller} = $1 if defined $1;
$defaults->{action} = $2 if defined $2;
}

# Defaults
Expand Down
16 changes: 15 additions & 1 deletion t/mojolicious/app.t
Expand Up @@ -11,7 +11,7 @@ use Test::More;
# Make sure sockets are working
plan skip_all => 'working sockets required for this test!'
unless Mojo::IOLoop->new->generate_port;
plan tests => 141;
plan tests => 156;

use FindBin;
use lib "$FindBin::Bin/lib";
Expand Down Expand Up @@ -218,3 +218,17 @@ $t->get_ok('/staged')->status_is(200)->header_is(Server => 'Mojo (Perl)')
$t->get_ok('/stash_config')->status_is(200)
->header_is(Server => 'Mojo (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')->content_is('123');

# Sweet shortcuts to controller#action
$t->get_ok('/shortcut/ctrl-act')->status_is(200)
->header_is(Server => 'Mojo (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_is('ctrl-act');
$t->get_ok('/shortcut/ctrl')->status_is(200)
->header_is(Server => 'Mojo (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_is('ctrl');
$t->get_ok('/shortcut/act')->status_is(200)
->header_is(Server => 'Mojo (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_is('act');
10 changes: 10 additions & 0 deletions t/mojolicious/lib/MojoliciousTest.pm
Expand Up @@ -68,6 +68,16 @@ sub startup {
$r->bridge('/staged')->to(controller => 'foo', action => 'stage1');
$b->route->to(action => 'stage2');

# /shortcut/ctrl-act
# /shortcut/ctrl
# /shortcut/act - sweet shortcuts to controller#action
$r->route('/shortcut/ctrl-act')
->to('foo#config', config => {test => 'ctrl-act'});
$r->route('/shortcut/ctrl')
->to('foo#', action => 'config', config => {test => 'ctrl'});
$r->route('/shortcut/act')
->to('#config', controller => 'foo', config => {test => 'act'});

# /*/* - the default route
$r->route('/(controller)/(action)')->to(action => 'index');
}
Expand Down

0 comments on commit bee2086

Please sign in to comment.