Skip to content

Commit

Permalink
improved Mojolicious::Controller attribute default values
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 19, 2011
1 parent 6d5bedb commit e1370fa
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,6 +1,7 @@
This file documents the revision history for Perl extension Mojolicious.

2.02 2011-10-20 00:00:00
- Improved Mojolicious::Controller attribute default values.
- Improved documentation.

2.01 2011-10-19 00:00:00
Expand Down
26 changes: 13 additions & 13 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -8,12 +8,17 @@ use Mojo::Home;
use Mojo::Transaction::HTTP;
use Mojo::URL;
use Mojo::Util;
use Mojolicious;
use Mojolicious::Routes::Match;

require Carp;
require Scalar::Util;

# "Scalpel... blood bucket... priest."
has [qw/app match/];
has app => sub { Mojolicious->new };
has match => sub {
Mojolicious::Routes::Match->new(get => '/')->root(shift->app->routes);
};
has tx => sub { Mojo::Transaction::HTTP->new };

# Bundled files
Expand Down Expand Up @@ -227,7 +232,7 @@ sub render {
}

# Try the route name if we don't have controller and action
elsif ($self->match && $self->match->endpoint) {
elsif ($self->match->endpoint) {
$self->stash->{template} = $self->match->endpoint->name;
}
}
Expand Down Expand Up @@ -575,13 +580,6 @@ sub url_for {
# Absolute URL
return Mojo::URL->new($target) if $target =~ /^\w+\:\/\//;

# Make sure we have a match for named routes
my $match;
unless ($match = $self->match) {
$match = Mojolicious::Routes::Match->new(get => '/');
$match->root($self->app->routes);
}

# Base
my $url = Mojo::URL->new;
my $req = $self->req;
Expand All @@ -606,7 +604,7 @@ sub url_for {

# Route
else {
my ($p, $ws) = $match->path_for($target, @_);
my ($p, $ws) = $self->match->path_for($target, @_);
$path->parse($p) if $p;

# Fix trailing slash
Expand Down Expand Up @@ -704,18 +702,20 @@ implements the following new ones.
$c = $c->app(Mojolicious->new);
A reference back to the L<Mojolicious> application that dispatched to this
controller.
controller, defaults to a L<Mojolicious> object.
=head2 C<match>
my $m = $c->match;
$c = $c->match(Mojolicious::Routes::Match->new);
A L<Mojolicious::Routes::Match> object containing the routes results for the
current request.
Routes results for the current request, defaults to a
L<Mojolicious::Routes::Match> object.
=head2 C<tx>
my $tx = $c->tx;
$c = $c->tx(Mojo::Transaction::HTTP->new);
The transaction that is currently being processed, usually a
L<Mojo::Transaction::HTTP> or L<Mojo::Transaction::WebSocket> object.
Expand Down
8 changes: 5 additions & 3 deletions t/mojolicious/dispatch.t
Expand Up @@ -27,16 +27,15 @@ use Mojo::Base -strict;

use utf8;

use Test::More tests => 67;
use Test::More tests => 68;

use Mojolicious;
use Mojo::Transaction::HTTP;
use Mojolicious::Controller;
use Mojolicious::Routes;

my $c = Mojolicious::Controller->new;

# Set
my $c = Mojolicious::Controller->new;
$c->stash(foo => 'bar');
is $c->stash('foo'), 'bar', 'set and return a stash value';

Expand Down Expand Up @@ -75,6 +74,9 @@ $c->stash({a => 1, b => 2});
$stash = $c->stash;
is_deeply $stash, {a => 1, b => 2}, 'set via hashref';

# Rendering
is $c->render(text => 'works', partial => 1), 'works', 'rendering works';

$c = Test::Controller->new(app => Mojolicious->new);
$c->app->log->path(undef);
$c->app->log->level('fatal');
Expand Down

0 comments on commit e1370fa

Please sign in to comment.