[Catalyst-commits] r9552 - in Catalyst-Runtime/5.80/trunk: . lib lib/Catalyst t/aggregate t/lib t/lib/TestApp/Controller/Action

t0m at dev.catalyst.perl.org t0m at dev.catalyst.perl.org
Sun Mar 22 01:28:46 GMT 2009


Author: t0m
Date: 2009-03-22 01:28:46 +0000 (Sun, 22 Mar 2009)
New Revision: 9552

Modified:
   Catalyst-Runtime/5.80/trunk/
   Catalyst-Runtime/5.80/trunk/Changes
   Catalyst-Runtime/5.80/trunk/lib/Catalyst.pm
   Catalyst-Runtime/5.80/trunk/lib/Catalyst/Dispatcher.pm
   Catalyst-Runtime/5.80/trunk/t/aggregate/live_component_controller_action_go.t
   Catalyst-Runtime/5.80/trunk/t/aggregate/live_component_controller_action_visit.t
   Catalyst-Runtime/5.80/trunk/t/lib/TestApp.pm
   Catalyst-Runtime/5.80/trunk/t/lib/TestApp/Controller/Action/Go.pm
   Catalyst-Runtime/5.80/trunk/t/lib/TestApp/Controller/Action/Visit.pm
Log:
 r9541 at dhcp-68 (orig r9539):  t0m | 2009-03-21 15:03:21 +0000
 Visit currently does nothing with CaptureArgs, as failing tests in trunk demonstrate.
 Branch as fixing this involves multiple commits and changing the visit api, etc..
 
 r9542 at dhcp-68 (orig r9540):  t0m | 2009-03-21 15:04:33 +0000
 Make CaptureArgs get passed, this makes the test less fail, but not perfect yet. Also, needs docs..
 r9543 at dhcp-68 (orig r9541):  t0m | 2009-03-21 15:17:38 +0000
 Fix the go test
 r9544 at dhcp-68 (orig r9542):  t0m | 2009-03-21 15:54:33 +0000
 Doc changes for passing CaptureArgs
 r9545 at dhcp-68 (orig r9543):  t0m | 2009-03-21 15:56:08 +0000
 Hack _invoke_as_component in a horrible way, so that it gives us back a path Action if it can, which makes >visit work correctly with qw/MyApp::Controller::Chained method_name/. I feel dirty, but it makes the test pass..
 r9546 at dhcp-68 (orig r9544):  t0m | 2009-03-21 18:04:06 +0000
 Much cleaner. The visit class test wasn't actually testing visiting the class, so I fixed that and made the English in the error message more correct (IMO)
 r9547 at dhcp-68 (orig r9545):  t0m | 2009-03-21 18:11:04 +0000
 Unfuck last commit, fix go tests, remove unneeded crud from TestApp, add FIXME for more cleanup
 r9548 at dhcp-68 (orig r9546):  t0m | 2009-03-21 20:39:52 +0000
 Cleanup, changelog



Property changes on: Catalyst-Runtime/5.80/trunk
___________________________________________________________________
Name: svk:merge
   - 1c72fc7c-9ce4-42af-bf25-3bfe470ff1e8:/local/Catalyst/trunk/Catalyst-Runtime:9763
4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-Runtime/5.70/branches/compres:7999
4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-Runtime/5.70/branches/context_go:8001
4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-Runtime/5.70/trunk:8533
4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-Runtime/5.80/branches/Catalyst-Test-Updates:8363
4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-Runtime/5.80/branches/moose:7911
4ad37cd2-5fec-0310-835f-b3785c72a374:/branches/Catalyst-ChildOf:4443
4ad37cd2-5fec-0310-835f-b3785c72a374:/branches/Catalyst-Runtime-jrockway:5857
4ad37cd2-5fec-0310-835f-b3785c72a374:/branches/Catalyst-component-setup:4320
4ad37cd2-5fec-0310-835f-b3785c72a374:/branches/Catalyst-docs:4325
4ad37cd2-5fec-0310-835f-b3785c72a374:/branches/current/Catalyst-Runtime:5142
4ad37cd2-5fec-0310-835f-b3785c72a374:/trunk/Catalyst:4483
4ad37cd2-5fec-0310-835f-b3785c72a374:/trunk/Catalyst-Runtime:6165
d7608cd0-831c-0410-93c0-e5b306c3c028:/local/Catalyst/Catalyst-Runtime:8339
d7608cd0-831c-0410-93c0-e5b306c3c028:/local/Catalyst/Catalyst-Runtime-jrockway:8342
e56d974f-7718-0410-8b1c-b347a71765b2:/local/Catalyst-Runtime:6511
e56d974f-7718-0410-8b1c-b347a71765b2:/local/Catalyst-Runtime-current:10442
   + 1c72fc7c-9ce4-42af-bf25-3bfe470ff1e8:/local/Catalyst/trunk/Catalyst-Runtime:9763
4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-Runtime/5.70/branches/compres:7999
4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-Runtime/5.70/branches/context_go:8001
4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-Runtime/5.70/trunk:8533
4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-Runtime/5.80/branches/Catalyst-Test-Updates:8363
4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-Runtime/5.80/branches/add_captures_to_visit:9546
4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-Runtime/5.80/branches/moose:7911
4ad37cd2-5fec-0310-835f-b3785c72a374:/branches/Catalyst-ChildOf:4443
4ad37cd2-5fec-0310-835f-b3785c72a374:/branches/Catalyst-Runtime-jrockway:5857
4ad37cd2-5fec-0310-835f-b3785c72a374:/branches/Catalyst-component-setup:4320
4ad37cd2-5fec-0310-835f-b3785c72a374:/branches/Catalyst-docs:4325
4ad37cd2-5fec-0310-835f-b3785c72a374:/branches/current/Catalyst-Runtime:5142
4ad37cd2-5fec-0310-835f-b3785c72a374:/trunk/Catalyst:4483
4ad37cd2-5fec-0310-835f-b3785c72a374:/trunk/Catalyst-Runtime:6165
d7608cd0-831c-0410-93c0-e5b306c3c028:/local/Catalyst/Catalyst-Runtime:8339
d7608cd0-831c-0410-93c0-e5b306c3c028:/local/Catalyst/Catalyst-Runtime-jrockway:8342
e56d974f-7718-0410-8b1c-b347a71765b2:/local/Catalyst-Runtime:6511
e56d974f-7718-0410-8b1c-b347a71765b2:/local/Catalyst-Runtime-current:10442

Modified: Catalyst-Runtime/5.80/trunk/Changes
===================================================================
--- Catalyst-Runtime/5.80/trunk/Changes	2009-03-22 01:18:31 UTC (rev 9551)
+++ Catalyst-Runtime/5.80/trunk/Changes	2009-03-22 01:28:46 UTC (rev 9552)
@@ -3,6 +3,13 @@
         - Throw an exception rather than loading an app if an action
           tries to chain to itself (t0m)
           - Tests for this
+        - Change the $c->visit and $c->go methods to optionally take
+          CaptureArgs, making them useful to call ActionChains with (t0m)
+          - Tests for this (radek)
+        - Fix _invoke_as_component method to find the proper action instance
+          for dispatchable actions so that ->visit or ->going to ActionChains
+          with qw/Class::Name method_name/ works correctly (t0m)
+          - Tests for this (radek)
         - Added Catalyst::Test::ctx_request to be able to inspect
           the context object after a request is made (Jos Boumans)
         - debug() POD rewrite (jhannah)

Modified: Catalyst-Runtime/5.80/trunk/lib/Catalyst/Dispatcher.pm
===================================================================
--- Catalyst-Runtime/5.80/trunk/lib/Catalyst/Dispatcher.pm	2009-03-22 01:18:31 UTC (rev 9551)
+++ Catalyst-Runtime/5.80/trunk/lib/Catalyst/Dispatcher.pm	2009-03-22 01:28:46 UTC (rev 9552)
@@ -119,7 +119,8 @@
 }
 
 # $self->_command2action( $c, $command [, \@arguments ] )
-# Search for an action, from the command and returns C<($action, $args)> on
+# $self->_command2action( $c, $command [, \@captures, \@arguments ] )
+# Search for an action, from the command and returns C<($action, $args, $captures)> on
 # success. Returns C<(0)> on error.
 
 sub _command2action {
@@ -130,8 +131,12 @@
         return 0;
     }
 
-    my @args;
+    my (@args, @captures);
 
+    if ( ref( $extra_params[-2] ) eq 'ARRAY' ) {
+        @captures = @{ pop @extra_params };
+    }
+
     if ( ref( $extra_params[-1] ) eq 'ARRAY' ) {
         @args = @{ pop @extra_params }
     } else {
@@ -158,7 +163,7 @@
         $action = $self->_invoke_as_component( $c, $command, $method );
     }
 
-    return $action, \@args;
+    return $action, \@args, \@captures;
 }
 
 =head2 $self->visit( $c, $command [, \@arguments ] )
@@ -176,7 +181,7 @@
     my $self = shift;
     my $opname = shift;
     my ( $c, $command ) = @_;
-    my ( $action, $args ) = $self->_command2action(@_);
+    my ( $action, $args, $captures ) = $self->_command2action(@_);
     my $error = qq/Couldn't $opname("$command"): /;
 
     if (!$action) {
@@ -185,7 +190,7 @@
     }
     elsif (!defined $action->namespace) {
         $error .= qq/Action has no namespace: cannot $opname() to a plain /
-                 .qq/method or component, must be a :Action or some sort./
+                 .qq/method or component, must be an :Action of some sort./
     }
     elsif (!$action->class->can('_DISPATCH')) {
         $error .= qq/Action cannot _DISPATCH. /
@@ -204,6 +209,7 @@
     $action = $self->expand_action($action);
 
     local $c->request->{arguments} = $args;
+    local $c->request->{captures}  = $captures;
     local $c->{namespace} = $action->{'namespace'};
     local $c->{action} = $action;
 
@@ -237,7 +243,7 @@
     my $self = shift;
     my $opname = shift;
     my ( $c, $command ) = @_;
-    my ( $action, $args ) = $self->_command2action(@_);
+    my ( $action, $args, $captures ) = $self->_command2action(@_);
 
     if (!$action) {
         my $error .= qq/Couldn't $opname to command "$command": /
@@ -301,35 +307,44 @@
     }
 }
 
-sub _find_component_class {
+sub _find_component {
     my ( $self, $c, $component ) = @_;
 
-    return ref($component)
-      || ref( $c->component($component) )
-      || $c->component($component);
+    # fugly, why doesn't ->component('MyApp') work?
+    return $c if ($component eq blessed($c));
+
+    return blessed($component)
+        ? $component
+        : $c->component($component);
 }
 
 sub _invoke_as_component {
-    my ( $self, $c, $component, $method ) = @_;
+    my ( $self, $c, $component_or_class, $method ) = @_;
 
-    my $class = $self->_find_component_class( $c, $component ) || return 0;
+    my $component = $self->_find_component($c, $component_or_class);
+    my $component_class = blessed $component || return 0;
 
-    if ( my $code = $class->can($method) ) {
+    if (my $code = $component_class->can('action_for')) {
+        my $possible_action = $component->$code($method);
+        return $possible_action if $possible_action;
+    }
+
+    if ( my $code = $component_class->can($method) ) {
         return $self->_method_action_class->new(
             {
                 name      => $method,
                 code      => $code,
-                reverse   => "$class->$method",
-                class     => $class,
+                reverse   => "$component_class->$method",
+                class     => $component_class,
                 namespace => Catalyst::Utils::class2prefix(
-                    $class, $c->config->{case_sensitive}
+                    $component_class, $c->config->{case_sensitive}
                 ),
             }
         );
     }
     else {
         my $error =
-          qq/Couldn't forward to "$class". Does not implement "$method"/;
+          qq/Couldn't forward to "$component_class". Does not implement "$method"/;
         $c->error($error);
         $c->log->debug($error)
           if $c->debug;

Modified: Catalyst-Runtime/5.80/trunk/lib/Catalyst.pm
===================================================================
--- Catalyst-Runtime/5.80/trunk/lib/Catalyst.pm	2009-03-22 01:18:31 UTC (rev 9551)
+++ Catalyst-Runtime/5.80/trunk/lib/Catalyst.pm	2009-03-22 01:28:46 UTC (rev 9552)
@@ -361,9 +361,9 @@
 
 sub detach { my $c = shift; $c->dispatcher->detach( $c, @_ ) }
 
-=head2 $c->visit( $action [, \@arguments ] )
+=head2 $c->visit( $action [, \@captures, \@arguments ] )
 
-=head2 $c->visit( $class, $method, [, \@arguments ] )
+=head2 $c->visit( $class, $method, [, \@captures, \@arguments ] )
 
 Almost the same as C<forward>, but does a full dispatch, instead of just
 calling the new C<$action> / C<$class-E<gt>$method>. This means that C<begin>,
@@ -387,9 +387,9 @@
 
 sub visit { my $c = shift; $c->dispatcher->visit( $c, @_ ) }
 
-=head2 $c->go( $action [, \@arguments ] )
+=head2 $c->go( $action [, \@captures, \@arguments ] )
 
-=head2 $c->go( $class, $method, [, \@arguments ] )
+=head2 $c->go( $class, $method, [, \@captures, \@arguments ] )
 
 Almost the same as C<detach>, but does a full dispatch like C<visit>,
 instead of just calling the new C<$action> /

Modified: Catalyst-Runtime/5.80/trunk/t/aggregate/live_component_controller_action_go.t
===================================================================
--- Catalyst-Runtime/5.80/trunk/t/aggregate/live_component_controller_action_go.t	2009-03-22 01:18:31 UTC (rev 9551)
+++ Catalyst-Runtime/5.80/trunk/t/aggregate/live_component_controller_action_go.t	2009-03-22 01:28:46 UTC (rev 9552)
@@ -242,7 +242,7 @@
         );
         ok( !$response->is_success, 'Response Fails' );
         is( $response->content,
-            q(FATAL ERROR: Couldn't go("TestApp"): Action has no namespace: cannot go() to a plain method or component, must be a :Action or some sort.),
+            q(FATAL ERROR: Couldn't go("TestApp"): Action has no namespace: cannot go() to a plain method or component, must be an :Action of some sort.),
             'Error message'
         );
     }
@@ -262,7 +262,7 @@
         ok( my $response = request('http://localhost/action/go/go_chained'), 'go to chained + subcontroller endpoint' );
         is( $response->header('X-Catalyst-Executed'),
             $expected, 'Executed actions' );
-        is( $response->content, '; 1', 'Content OK' );
+        is( $response->content, 'arg1, arg2; captureme', 'Content OK' );
     }
 
 }

Modified: Catalyst-Runtime/5.80/trunk/t/aggregate/live_component_controller_action_visit.t
===================================================================
--- Catalyst-Runtime/5.80/trunk/t/aggregate/live_component_controller_action_visit.t	2009-03-22 01:18:31 UTC (rev 9551)
+++ Catalyst-Runtime/5.80/trunk/t/aggregate/live_component_controller_action_visit.t	2009-03-22 01:28:46 UTC (rev 9552)
@@ -253,7 +253,7 @@
         );
         ok( !$response->is_success, 'Response Fails' );
         is( $response->content,
-            q[FATAL ERROR: Couldn't visit("TestApp"): Action has no namespace: cannot visit() to a plain method or component, must be a :Action or some sort.],
+            q{FATAL ERROR: Couldn't visit("TestApp"): Action has no namespace: cannot visit() to a plain method or component, must be an :Action of some sort.},
             "Cannot visit app namespace"
         );
     }
@@ -272,11 +272,12 @@
         my $expected = join( ", ", @expected );
 
         for my $i ( 1..3 ) {
-            ok( my $response = request("http://localhost/action/visit/visit_chained/$i"),
+            ok( my $response = request("http://localhost/action/visit/visit_chained/$i/becomescapture/arg1/arg2"),
                 "visit to chained + subcontroller endpoint for $i" );
             is( $response->header('X-Catalyst-Executed'),
                 $expected, "Executed actions for $i" );
-            is( $response->content, "; $i", "Content OK for $i" );
+            is( $response->content, "arg1, arg2; becomescapture",
+                "Content OK for $i" );
         }
     }
 

Modified: Catalyst-Runtime/5.80/trunk/t/lib/TestApp/Controller/Action/Go.pm
===================================================================
--- Catalyst-Runtime/5.80/trunk/t/lib/TestApp/Controller/Action/Go.pm	2009-03-22 01:18:31 UTC (rev 9551)
+++ Catalyst-Runtime/5.80/trunk/t/lib/TestApp/Controller/Action/Go.pm	2009-03-22 01:28:46 UTC (rev 9552)
@@ -63,7 +63,7 @@
 
 sub go_chained : Local {
     my ( $self, $c, $val ) = @_;
-    $c->go('/action/chained/foo/spoon',[1]);
+    $c->go('/action/chained/foo/spoon', ['captureme'], [qw/arg1 arg2/]);
 }
 
 sub view : Local {
@@ -96,7 +96,7 @@
 
 sub class_go_test_action : Local {
     my ( $self, $c ) = @_;
-    $c->go(qw/TestApp class_go_test_method/);
+    $c->go(qw/TestApp/);
 }
 
 1;

Modified: Catalyst-Runtime/5.80/trunk/t/lib/TestApp/Controller/Action/Visit.pm
===================================================================
--- Catalyst-Runtime/5.80/trunk/t/lib/TestApp/Controller/Action/Visit.pm	2009-03-22 01:18:31 UTC (rev 9551)
+++ Catalyst-Runtime/5.80/trunk/t/lib/TestApp/Controller/Action/Visit.pm	2009-03-22 01:28:46 UTC (rev 9552)
@@ -61,10 +61,11 @@
 }
 
 sub visit_chained : Local {
-    my ( $self, $c, $val ) = @_;
-      $val eq 1 ? $c->visit( '/action/chained/foo/spoon',                                 [$val] )
-    : $val eq 2 ? $c->visit( qw/ Action::Chained::Foo spoon /,                            [$val] )
-    :             $c->visit( $c->controller('Action::Chained::Foo')->action_for('spoon'), [$val] )
+    my ( $self, $c, $val, $capture, @args ) = @_;
+    my @cap_and_args = ([$capture], [@args]);
+      $val eq 1 ? $c->visit( '/action/chained/foo/spoon',                                 @cap_and_args)
+    : $val eq 2 ? $c->visit( qw/ Action::Chained::Foo spoon /,                            @cap_and_args)
+    :             $c->visit( $c->controller('Action::Chained::Foo')->action_for('spoon'), @cap_and_args)
 }
 
 sub view : Local {
@@ -97,7 +98,7 @@
 
 sub class_visit_test_action : Local {
     my ( $self, $c ) = @_;
-    $c->visit(qw/TestApp class_visit_test_method/);
+    $c->visit(qw/TestApp/);
 }
 
 1;

Modified: Catalyst-Runtime/5.80/trunk/t/lib/TestApp.pm
===================================================================
--- Catalyst-Runtime/5.80/trunk/t/lib/TestApp.pm	2009-03-22 01:18:31 UTC (rev 9551)
+++ Catalyst-Runtime/5.80/trunk/t/lib/TestApp.pm	2009-03-22 01:28:46 UTC (rev 9552)
@@ -73,16 +73,6 @@
     $c->response->headers->header( 'X-Class-Forward-Test-Method' => 1 );
 }
 
-sub class_go_test_method :Private {
-    my ( $self, $c ) = @_;
-    $c->response->headers->header( 'X-Class-Go-Test-Method' => 1 );
-}
-
-sub class_visit_test_method :Private {
-    my ( $self, $c ) = @_;
-    $c->response->headers->header( 'X-Class-Visit-Test-Method' => 1 );
-}
-
 sub loop_test : Local {
     my ( $self, $c ) = @_;
 




More information about the Catalyst-commits mailing list