Permalink
Browse files

Improve route->to('controller#action') shortcuts

Now you can say to('controller#') and to('#action') for short.
  • Loading branch information...
1 parent 641d5e0 commit bee2086ca22bd5ae5a7e0b4aa851ebcb8cf8ce65 @mvuets mvuets committed with Sebastian Riedel Feb 7, 2010
Showing with 28 additions and 4 deletions.
  1. +3 −3 lib/MojoX/Routes.pm
  2. +15 −1 t/mojolicious/app.t
  3. +10 −0 t/mojolicious/lib/MojoliciousTest.pm
View
@@ -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
View
@@ -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";
@@ -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');
@@ -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');
}

0 comments on commit bee2086

Please sign in to comment.