allow uri_for_action to be called with captures and args in a single arrayref
Lian Wan Situ [Mon, 12 Apr 2010 20:49:01 +0000 (20:49 +0000)]
lib/Catalyst.pm
lib/Catalyst/Action.pm
lib/Catalyst/ActionChain.pm
t/aggregate/unit_core_uri_for_action.t

index dab2c8f..bcf26a2 100644 (file)
@@ -1297,7 +1297,15 @@ sub uri_for {
         }
 
         my $action = $path;
-        $path = $c->dispatcher->uri_for_action($action, $captures);
+        # ->uri_for( $action, \@captures_and_args, \%query_values? )
+        if( !@args && $action->number_of_args ) {
+            my $expanded_action = $c->dispatcher->expand_action( $action );
+
+            my $num_captures = $expanded_action->number_of_captures;
+            unshift @args, splice @$captures, $num_captures;
+        }
+
+       $path = $c->dispatcher->uri_for_action($action, $captures);
         if (not defined $path) {
             $c->log->debug(qq/Can't find uri_for action '$action' @$captures/)
                 if $c->debug;
index 87b37fd..09f81a8 100644 (file)
@@ -88,6 +88,19 @@ sub compare {
     return $a1_args <=> $a2_args;
 }
 
+sub number_of_args {
+    my ( $self ) = @_;
+    return 0 unless exists $self->attributes->{Args};
+    return $self->attributes->{Args}[0];
+}
+
+sub number_of_captures {
+    my ( $self ) = @_;
+
+    return 0 unless exists $self->attributes->{CaptureArgs};
+    return $self->attributes->{CaptureArgs}[0] || 0;
+}
+
 __PACKAGE__->meta->make_immutable;
 
 1;
@@ -147,6 +160,14 @@ C<private_path> of an action is always suitable for passing to C<forward>.
 
 Returns the sub name of this action.
 
+=head2 number_of_args
+
+Returns the number of args this action expects. This is 0 if the action doesn't take any arguments and undef if it will take any number of arguments.
+
+=head2 number_of_captures
+
+Returns the number of captures this action expects for L<Chained|Catalyst::DispatchType::Chained> actions.
+
 =head2 meta
 
 Provided by Moose.
index cf48342..496000b 100644 (file)
@@ -4,7 +4,6 @@ use Moose;
 extends qw(Catalyst::Action);
 
 has chain => (is => 'rw');
-
 no Moose;
 
 =head1 NAME
@@ -30,8 +29,8 @@ sub dispatch {
     my $last = pop(@chain);
     foreach my $action ( @chain ) {
         my @args;
-        if (my $cap = $action->attributes->{CaptureArgs}) {
-          @args = splice(@captures, 0, $cap->[0]);
+        if (my $cap = $action->number_of_captures) {
+          @args = splice(@captures, 0, $cap);
         }
         local $c->request->{arguments} = \@args;
         $action->dispatch( $c );
@@ -45,6 +44,15 @@ sub from_chain {
     return $self->new({ %$final, chain => $actions });
 }
 
+sub number_of_captures {
+    my ( $self ) = @_;
+    my $chain = $self->chain;
+    my $captures = 0;
+
+    $captures += $_->number_of_captures for @$chain;
+    return $captures;
+}
+
 __PACKAGE__->meta->make_immutable;
 1;
 
@@ -67,6 +75,10 @@ actions in order.
 Takes a list of Catalyst::Action objects and constructs and returns a
 Catalyst::ActionChain object representing a chain of these actions
 
+=head2 number_of_captures
+
+Returns the total number of captures for the entire chain of actions.
+
 =head2 meta
 
 Provided by Moose
index 89079f9..c8925d8 100644 (file)
@@ -8,7 +8,7 @@ use lib "$FindBin::Bin/../lib";
 
 use Test::More;
 
-plan tests => 30;
+plan tests => 39;
 
 use_ok('TestApp');
 
@@ -130,10 +130,18 @@ is($context->uri_for($chained_action, [ 1 ], 2, { q => 1 }),
         'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4?x=5',
         'uri_for_action correct for chained with multiple captures and args' );
 
+    is( $context->uri_for_action( '/action/chained/endpoint2', [1,2,3,4], { x => 5 } ),
+        'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4?x=5',
+        'uri_for_action correct for chained with multiple captures and args combined' );
+
     is( $context->uri_for_action( '/action/chained/three_end', [1,2,3], (4,5,6) ),
         'http://127.0.0.1/foo/chained/one/1/two/2/3/three/4/5/6',
         'uri_for_action correct for chained with multiple capturing actions' );
 
+    is( $context->uri_for_action( '/action/chained/three_end', [1,2,3,4,5,6] ),
+        'http://127.0.0.1/foo/chained/one/1/two/2/3/three/4/5/6',
+        'uri_for_action correct for chained with multiple capturing actions and args combined' );
+
     my $action_needs_two = '/action/chained/endpoint2';
     
     ok( ! defined( $context->uri_for_action($action_needs_two, [1],     (2,3)) ),
@@ -142,7 +150,11 @@ is($context->uri_for($chained_action, [ 1 ], 2, { q => 1 }),
     is( $context->uri_for_action($action_needs_two,            [1,2],   (2,3)),
         'http://127.0.0.1/foo/chained/foo2/1/2/end2/2/3',
         'uri_for_action returns correct uri for correct captures' );
-        
+
+    is( $context->uri_for_action($action_needs_two,            [1,2,2,3]),
+        'http://127.0.0.1/foo/chained/foo2/1/2/end2/2/3',
+        'uri_for_action returns correct uri for correct captures and args combined' );
+
     ok( ! defined( $context->uri_for_action($action_needs_two, [1,2,3], (2,3)) ),
         'uri_for_action returns undef for too many captures' );
     
@@ -150,26 +162,49 @@ is($context->uri_for($chained_action, [ 1 ], 2, { q => 1 }),
         'http://127.0.0.1/foo/chained/foo2/1/2/end2/3',
         'uri_for_action returns uri with lesser args than specified on action' );
 
+    is( $context->uri_for_action($action_needs_two, [1,2,3]),
+        'http://127.0.0.1/foo/chained/foo2/1/2/end2/3',
+        'uri_for_action returns uri with lesser args than specified on action with captures combined' );
+
     is( $context->uri_for_action($action_needs_two, [1,2],   (3,4,5)),
         'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4/5',
         'uri_for_action returns uri with more args than specified on action' );
 
+    is( $context->uri_for_action($action_needs_two, [1,2,3,4,5]),
+        'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4/5',
+        'uri_for_action returns uri with more args than specified on action with captures combined' );
+
     is( $context->uri_for_action($action_needs_two, [1,''], (3,4)),
         'http://127.0.0.1/foo/chained/foo2/1//end2/3/4',
         'uri_for_action returns uri with empty capture on undef capture' );
 
+    is( $context->uri_for_action($action_needs_two, [1,'',3,4]),
+        'http://127.0.0.1/foo/chained/foo2/1//end2/3/4',
+        'uri_for_action returns uri with empty capture on undef capture and args combined' );
+
     is( $context->uri_for_action($action_needs_two, [1,2], ('',3)),
         'http://127.0.0.1/foo/chained/foo2/1/2/end2//3',
         'uri_for_action returns uri with empty arg on undef argument' );
 
+    is( $context->uri_for_action($action_needs_two, [1,2,'',3]),
+        'http://127.0.0.1/foo/chained/foo2/1/2/end2//3',
+        'uri_for_action returns uri with empty arg on undef argument and args combined' );
+
     is( $context->uri_for_action($action_needs_two, [1,2], (3,'')),
         'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/',
         'uri_for_action returns uri with empty arg on undef last argument' );
 
+    is( $context->uri_for_action($action_needs_two, [1,2,3,'']),
+        'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/',
+        'uri_for_action returns uri with empty arg on undef last argument with captures combined' );
+
     my $complex_chained = '/action/chained/empty_chain_f';
     is( $context->uri_for_action( $complex_chained, [23], (13), {q => 3} ),
         'http://127.0.0.1/foo/chained/empty/23/13?q=3',
         'uri_for_action returns correct uri for chain with many empty path parts' );
+    is( $context->uri_for_action( $complex_chained, [23,13], {q => 3} ),
+        'http://127.0.0.1/foo/chained/empty/23/13?q=3',
+        'uri_for_action returns correct uri for chain with many empty path parts with captures and args combined' );
 
     eval { $context->uri_for_action( '/does/not/exist' ) };
     like $@, qr{^Can't find action for path '/does/not/exist'},