Skip to content

Commit

Permalink
add without_formats method to Mojolicious::Routes::Route
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 24, 2015
1 parent 618c7b4 commit fd93399
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 48 deletions.
15 changes: 6 additions & 9 deletions lib/Mojolicious/Guides/Routing.pod
Expand Up @@ -588,22 +588,19 @@ allowed formats.
# /foo.xml -> {controller => 'foo', action => 'bar', format => 'xml'}
$r->get('/foo' => [format => [qw(rss xml)]])->to('foo#bar');

Or you can just disable format detection, which gets inherited by nested
routes and allows selective re-enabling.

# /foo -> {controller => 'foo', action => 'bar'}
# /foo.html -> undef
$r->get('/foo' => [format => 0])->to('foo#bar');
Or you can just disable format detection with
L<Mojolicious::Routes::Route/"without_formats">, and then re-enable it
selectively on demand.

# /foo -> {controller => 'foo', action => 'bar'}
# /foo.html -> undef
# /baz -> undef
# /baz.txt -> {controller => 'baz', action => 'yada', format => 'txt'}
# /baz.html -> {controller => 'baz', action => 'yada', format => 'html'}
# /baz.xml -> undef
my $inactive = $r->under([format => 0]);
$inactive->get('/foo')->to('foo#bar');
$inactive->get('/baz' => [format => [qw(txt html)]])->to('baz#yada');
$r->without_formats;
$r->get('/foo')->to('foo#bar');
$r->get('/baz' => [format => [qw(txt html)]])->to('baz#yada');

=head2 WebSockets

Expand Down
10 changes: 4 additions & 6 deletions lib/Mojolicious/Guides/Tutorial.pod
Expand Up @@ -590,15 +590,13 @@ possible values.

app->start;

Or you can just disable format detection.
Or you can just disable format detection with
L<Mojolicious::Routes::Route/"without_formats">, and then re-enable it
selectively on demand.

use Mojolicious::Lite;

# /hello
get '/hello' => [format => 0] => {text => 'No format detection.'};

# Disable detection and allow the following routes selective re-enabling
under [format => 0];
app->routes->without_formats;

# /foo
get '/foo' => {text => 'No format detection again.'};
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Lite.pm
Expand Up @@ -204,7 +204,7 @@ variations.
my $route = under '/:foo' => sub {...};
my $route = under '/:foo' => {foo => 'bar'};
my $route = under '/:foo' => [foo => qr/\w+/];
my $route = under [format => 0];
my $route = under [format => ['html', 'json']];
Generate nested route with L<Mojolicious::Routes::Route/"under">, to which all
following routes are automatically appended. See also
Expand Down
14 changes: 0 additions & 14 deletions lib/Mojolicious/Routes.pm
Expand Up @@ -97,10 +97,6 @@ sub match {
"$method:$path:$ws" => {endpoint => $route, stack => $match->stack});
}

sub route {
shift->add_child(Mojolicious::Routes::Route->new(@_))->children->[-1];
}

sub _action { shift->plugins->emit_chain(around_action => @_) }

sub _callback {
Expand Down Expand Up @@ -344,16 +340,6 @@ results for future lookups.
Match routes with L<Mojolicious::Routes::Match>.
=head2 route
my $route = $r->route;
my $route = $r->route('/:action');
my $route = $r->route('/:action', action => qr/\w+/);
my $route = $r->route(format => 0);
Low-level generator for routes matching all HTTP request methods, returns a
L<Mojolicious::Routes::Route> object.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Routes/Pattern.pm
Expand Up @@ -356,7 +356,7 @@ disabled by default.
my $pattern = Mojolicious::Routes::Pattern->new('/:action');
my $pattern
= Mojolicious::Routes::Pattern->new('/:action', action => qr/\w+/);
my $pattern = Mojolicious::Routes::Pattern->new(format => 0);
my $pattern = Mojolicious::Routes::Pattern->new(format => ['html', 'json']);
Construct a new L<Mojolicious::Routes::Pattern> object and L</"parse"> pattern
if necessary.
Expand All @@ -365,7 +365,7 @@ if necessary.
$pattern = $pattern->parse('/:action');
$pattern = $pattern->parse('/:action', action => qr/\w+/);
$pattern = $pattern->parse(format => 0);
$pattern = $pattern->parse(format => ['html', 'json']);
Parse pattern.
Expand Down
21 changes: 16 additions & 5 deletions lib/Mojolicious/Routes/Route.pm
Expand Up @@ -122,7 +122,7 @@ sub root { shift->_chain->[0] }

sub route {
my $self = shift;
my $route = $self->add_child($self->new(@_))->children->[-1];
my $route = $self->add_child(__PACKAGE__->new(@_))->children->[-1];
my $format = $self->pattern->constraints->{format};
$route->pattern->constraints->{format} //= 0 if defined $format && !$format;
return $route;
Expand Down Expand Up @@ -174,6 +174,8 @@ sub websocket {
return $route;
}

sub without_formats { !($_[0]->pattern->constraints->{format} = 0) and $_[0] }

sub _chain {
my @chain = (my $parent = shift);
unshift @chain, $parent while $parent = $parent->parent;
Expand Down Expand Up @@ -385,7 +387,7 @@ the current route.
my $r = Mojolicious::Routes::Route->new;
my $r = Mojolicious::Routes::Route->new('/:action');
my $r = Mojolicious::Routes::Route->new('/:action', action => qr/\w+/);
my $r = Mojolicious::Routes::Route->new(format => 0);
my $r = Mojolicious::Routes::Route->new(format => ['html', 'json']);
Construct a new L<Mojolicious::Routes::Route> object and L</"parse"> pattern
if necessary.
Expand Down Expand Up @@ -419,7 +421,7 @@ routing cache, since conditions are too complex for caching.
$r = $r->parse('/:action');
$r = $r->parse('/:action', action => qr/\w+/);
$r = $r->parse(format => 0);
$r = $r->parse(format => ['html', 'json']);
Parse pattern.
Expand Down Expand Up @@ -488,7 +490,7 @@ The L<Mojolicious::Routes> object this route is a descendant of.
my $route = $r->route;
my $route = $r->route('/:action');
my $route = $r->route('/:action', action => qr/\w+/);
my $route = $r->route(format => 0);
my $route = $r->route(format => ['html', 'json']);
Low-level generator for routes matching all HTTP request methods, returns a
L<Mojolicious::Routes::Route> object.
Expand Down Expand Up @@ -522,7 +524,7 @@ Stringify the whole route.
my $route = $r->under('/:foo' => sub {...});
my $route = $r->under('/:foo' => {foo => 'bar'});
my $route = $r->under('/:foo' => [foo => qr/\w+/]);
my $route = $r->under([format => 0]);
my $route = $r->under([format => ['html', 'json']]);
Generate L<Mojolicious::Routes::Route> object for a nested route with its own
intermediate destination. See also L<Mojolicious::Guides::Tutorial> for many
Expand Down Expand Up @@ -557,6 +559,15 @@ variations.
$r->websocket('/echo')->to('example#echo');
=head2 without_formats
$r = $r->without_formats;
Disable format detection for this route and its descendants.
# Longer version
$r->pattern->constraints->{format} = 0;
=head1 AUTOLOAD
In addition to the L</"ATTRIBUTES"> and L</"METHODS"> above you can also call
Expand Down
14 changes: 3 additions & 11 deletions t/mojolicious/group_lite_app.t
Expand Up @@ -10,6 +10,9 @@ use Test::Mojo;

app->secrets(['test1']);

# Disable format detection
app->routes->without_formats;

get '/multi' => sub {
my $c = shift;
$c->cookie(unsigned1 => 'one');
Expand Down Expand Up @@ -171,9 +174,6 @@ group {

get '/noauthgroup' => {inline => 'Whatever <%= $foo %>.'};

# Disable format detection
under [format => 0];

get '/no_format' => {text => 'No format detection.'};

get '/some_formats' => [format => [qw(txt json)]] =>
Expand Down Expand Up @@ -399,14 +399,6 @@ $t->get_ok('/authgroup?ok=1')->status_is(200)
$t->get_ok('/authgroup')->status_is(200)
->content_type_is('text/html;charset=UTF-8')->content_is("You're not ok.");

# Authenticated by group (with format)
$t->get_ok('/authgroup.txt?ok=1')->status_is(200)
->content_type_is('text/plain;charset=UTF-8')->content_is("You're ok.");

# Not authenticated by group (with format)
$t->get_ok('/authgroup.txt')->status_is(200)
->content_type_is('text/plain;charset=UTF-8')->content_is("You're not ok.");

# Bypassed group authentication
$t->get_ok('/noauthgroup')->status_is(200)->content_is("Whatever one.\n");

Expand Down

0 comments on commit fd93399

Please sign in to comment.