From 135814622c3e25d7a899166a1d14cbb138a672db Mon Sep 17 00:00:00 2001 From: Sebastian Riedel Date: Wed, 10 Oct 2012 15:13:02 +0200 Subject: [PATCH] improved router and renderer to allow camel case controllers --- Changes | 3 +-- lib/Mojolicious/Controller.pm | 3 ++- t/mojolicious/app.t | 12 +++++++++--- t/mojolicious/lib/MojoliciousTest.pm | 5 ++++- t/mojolicious/lib/MojoliciousTest/Foo/Bar.pm | 2 +- t/mojolicious/templates/foo/bar/test.html.ep | 1 + 6 files changed, 18 insertions(+), 8 deletions(-) create mode 100644 t/mojolicious/templates/foo/bar/test.html.ep diff --git a/Changes b/Changes index 9ee81c4e47..929bf17c76 100644 --- a/Changes +++ b/Changes @@ -1,7 +1,6 @@ 3.46 2012-10-11 - - Improved to method in Mojolicious::Routes::Route to allow controller - classes containing "::" in shortcuts. + - Improved router and renderer to allow camel case controllers. 3.45 2012-10-10 - Added multi_accept attribute to Mojo::IOLoop. diff --git a/lib/Mojolicious/Controller.pm b/lib/Mojolicious/Controller.pm index 50d4852162..3c18ce45ad 100644 --- a/lib/Mojolicious/Controller.pm +++ b/lib/Mojolicious/Controller.pm @@ -166,7 +166,8 @@ sub render { my $controller = $args->{controller} || $stash->{controller}; my $action = $args->{action} || $stash->{action}; if ($controller && $action) { - $stash->{template} = join '/', split(/-/, $controller), $action; + $stash->{template} = join '/', + split(/-/, Mojo::Util::decamelize($controller)), $action; } # Try the route name if we don't have controller and action diff --git a/t/mojolicious/app.t b/t/mojolicious/app.t index b1eb79671c..5149e910a2 100644 --- a/t/mojolicious/app.t +++ b/t/mojolicious/app.t @@ -7,7 +7,7 @@ BEGIN { $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll'; } -use Test::More tests => 344; +use Test::More tests => 349; use FindBin; use lib "$FindBin::Bin/lib"; @@ -226,11 +226,17 @@ $t->get_ok('/test6' => {'X-Test' => 'Hi there!'})->status_is(200) ->header_is(Server => 'Mojolicious (Perl)') ->header_is('X-Powered-By' => 'Mojolicious (Perl)')->content_is('/test6'); -# MojoliciousTest::Foo::Bar::test (controller class) +# MojoliciousTest::Foo::Bar::test (controller class shortcut) $t->get_ok('/test7' => {'X-Test' => 'Hi there!'})->status_is(200) ->header_is(Server => 'Mojolicious (Perl)') ->header_is('X-Powered-By' => 'Mojolicious (Perl)') - ->content_is('Class works!'); + ->content_is("Class works!\n"); + +# MojoliciousTest::Foo::Bar::test (controller class) +$t->get_ok('/test8' => {'X-Test' => 'Hi there!'})->status_is(200) + ->header_is(Server => 'Mojolicious (Perl)') + ->header_is('X-Powered-By' => 'Mojolicious (Perl)') + ->content_is("Class works!\n"); # 404 $t->get_ok('/' => {'X-Test' => 'Hi there!'})->status_is(404) diff --git a/t/mojolicious/lib/MojoliciousTest.pm b/t/mojolicious/lib/MojoliciousTest.pm index 48df894f48..51af96fef4 100644 --- a/t/mojolicious/lib/MojoliciousTest.pm +++ b/t/mojolicious/lib/MojoliciousTest.pm @@ -118,9 +118,12 @@ sub startup { action => 'test' ); - # /test7 (controller class) + # /test7 (controller class shortcut) $r->route('/test7')->to('Foo::Bar#test'); + # /test8 (controller class) + $r->route('/test8')->to(controller => 'Foo::Bar', action => 'test'); + # /withblock (template with blocks) $r->route('/withblock')->to('foo#withblock'); diff --git a/t/mojolicious/lib/MojoliciousTest/Foo/Bar.pm b/t/mojolicious/lib/MojoliciousTest/Foo/Bar.pm index 02d32ecfd4..3ab29079bd 100644 --- a/t/mojolicious/lib/MojoliciousTest/Foo/Bar.pm +++ b/t/mojolicious/lib/MojoliciousTest/Foo/Bar.pm @@ -3,6 +3,6 @@ use Mojolicious::Controller -base; sub index {1} -sub test { shift->render(text => 'Class works!') } +sub test { shift->stash(message => 'works') } 1; diff --git a/t/mojolicious/templates/foo/bar/test.html.ep b/t/mojolicious/templates/foo/bar/test.html.ep new file mode 100644 index 0000000000..808cb69d52 --- /dev/null +++ b/t/mojolicious/templates/foo/bar/test.html.ep @@ -0,0 +1 @@ +Class <%= $message %>! \ No newline at end of file