Allow forward with arguments.
Marcus Ramberg [Fri, 24 Jun 2005 12:47:21 +0000 (12:47 +0000)]
Changes
lib/Catalyst/Dispatcher.pm
t/component/controller/action/forward.t
t/lib/TestApp/Controller/Action/Forward.pm

diff --git a/Changes b/Changes
index 631d665..54b4433 100644 (file)
--- a/Changes
+++ b/Changes
@@ -2,6 +2,7 @@ This file documents the revision history for Perl extension Catalyst.
 
 5.30  2005-00-00 00:00:00
 
+        - Allow forward with arguments.
         - Updated cookbook
         - Allow overriding home/root in config.
         - make module build cons README automatically.
index 69c8400..af4f7ce 100644 (file)
@@ -104,10 +104,13 @@ sub dispatch {
     }
 }
 
-=item $c->forward($command)
+=item $c->forward($command [args]))
 
 Forward processing to a private action or a method from a class.
 If you define a class without method it will default to process().
+also takes an optional arrayref containing arguments to be passed
+to the new function. $c->req->args will be reset upon returning 
+from the function.
 
     $c->forward('/foo');
     $c->forward('index');
@@ -127,6 +130,15 @@ sub forward {
 
     my $caller    = caller(0);
     my $namespace = '/';
+    my $args;
+
+    if ( ref( $_[0] ) eq 'ARRAY' ) {
+        $args=$c->req->args();
+        $c->req->args( shift );
+    } elsif ( ref( $_[1] ) eq 'ARRAY' ) {
+        $args=$c->req->args();
+        $c->req->args( $_[1] );
+    }
 
     if ( $command =~ /^\// ) {
         $command =~ /^\/(.*)\/(\w+)$/;
@@ -184,10 +196,12 @@ sub forward {
 
     for my $result ( @{$results} ) {
         $c->execute( @{ $result->[0] } );
-        return if scalar @{ $c->error };
+        last if scalar @{ $c->error };
         last unless $c->state;
     }
+    $c->req->args( $args ) if $args;
 
+    return if scalar @{ $c->error };
     return $c->state;
 }
 
index cb0f924..58355af 100644 (file)
@@ -6,7 +6,7 @@ use warnings;
 use FindBin;
 use lib "$FindBin::Bin/../../../lib";
 
-use Test::More tests => 18;
+use Test::More tests => 24;
 use Catalyst::Test 'TestApp';
 
 
@@ -64,3 +64,15 @@ use Catalyst::Test 'TestApp';
     is( $response->header('X-Catalyst-Executed'), $expected, 'Executed actions' );
     like( $response->content, qr/^bless\( .* 'Catalyst::Request' \)$/s, 'Content is a serialized Catalyst::Request' );
 }
+
+{
+    ok( my $response = request('http://localhost/action/forward/with_args/old'), 'Request with args' );
+    ok( $response->is_success, 'Response Successful 2xx' );
+    is( $response->content,'old');
+}
+
+{
+    ok( my $response = request('http://localhost/action/forward/with_method_and_args/old'), 'Request with args and method' );
+    ok( $response->is_success, 'Response Successful 2xx' );
+    is( $response->content,'old');
+}
index 1d65d14..4fdf5ea 100644 (file)
@@ -3,7 +3,7 @@ package TestApp::Controller::Action::Forward;
 use strict;
 use base 'TestApp::Controller::Action';
 
-sub one : Relative {
+sub one : Local {
     my ( $self, $c ) = @_;
     $c->forward('two');
 }
@@ -13,7 +13,7 @@ sub two : Private {
     $c->forward('three');
 }
 
-sub three : Relative {
+sub three : Local {
     my ( $self, $c ) = @_;
     $c->forward('four');
 }
@@ -23,29 +23,46 @@ sub four : Private {
     $c->forward('/action/forward/five');
 }
 
-sub five : Relative {
+sub five : Local {
     my ( $self, $c ) = @_;
     $c->forward('TestApp::View::Dump::Request');
 }
 
 
-sub jojo : Relative {
+sub jojo : Local {
     my ( $self, $c ) = @_;
     $c->forward('one');
     $c->forward('three');
 }
 
 
-sub inheritance : Relative {
+sub inheritance : Local {
     my ( $self, $c ) = @_;
     $c->forward('/action/inheritance/a/b/default');
     $c->forward('five');
 }
 
-sub global : Relative {
+sub global : Local {
     my ( $self, $c ) = @_;
-    $c->forward('/global_action');
+    $c->forward( '/global_action' );
 }
 
+sub with_args : Local {
+    my ( $self, $c, $orig ) = @_;
+    $c->forward( 'args',[qq/new/] );
+    $c->res->body( $c->req->args->[0] );
+}
+
+sub with_method_and_args : Local {
+    my ( $self, $c, $orig ) = @_;
+    $c->forward( qw/TestApp::Controller::Action::Forward args/, [qq/new/] );
+    $c->res->body( $c->req->args->[0] );
+}
+
+sub args : Local {
+    my ( $self, $c, $val ) = @_;
+    die "Expected argument 'new', got '$val'" unless $val eq 'new';
+    die "passed argument does not match args" unless $val eq $c->req->args->[0];
+}
 
 1;