prevent actions named index from registering as an index dispatchtype if they can...
Rafael Kitover [Fri, 19 Jun 2009 23:47:02 +0000 (23:47 +0000)]
lib/Catalyst/DispatchType/Index.pm
lib/Catalyst/Dispatcher.pm
t/aggregate/live_component_controller_action_named_index.t [moved from t/aggregate/live_component_controller_action_index_or_default_names.t with 90% similarity]
t/lib/TestAppIndexActionName.pm [moved from t/lib/TestAppIndexDefault.pm with 67% similarity]
t/lib/TestAppIndexActionName/Controller/IndexChained.pm [moved from t/lib/TestAppIndexDefault/Controller/IndexChained.pm with 80% similarity]

index 86e45ee..d55ca00 100644 (file)
@@ -2,7 +2,7 @@ package Catalyst::DispatchType::Index;
 
 use Moose;
 extends 'Catalyst::DispatchType';
-no Moose;
+use namespace::clean -except => 'meta';
 
 =head1 NAME
 
@@ -25,6 +25,12 @@ dispatch types, see:
 
 =back
 
+=cut
+
+has _actions => (
+    is => 'rw', isa => 'HashRef', default => sub { +{} }
+);
+
 =head1 METHODS
 
 =head2 $self->match( $c, $path )
@@ -40,6 +46,8 @@ sub match {
     return if @{ $c->req->args };
     my $result = $c->get_action( 'index', $path );
 
+    return 0 unless $result && exists $self->_actions->{ $result->reverse };
+
     if ($result && $result->match($c)) {
         $c->action($result);
         $c->namespace( $result->namespace );
@@ -50,6 +58,20 @@ sub match {
     return 0;
 }
 
+=head2 $self->register( $c, $action )
+
+Register an action with this DispatchType.
+
+=cut
+
+sub register {
+    my ( $self, $c, $action ) = @_;
+
+    $self->_actions->{ $action->reverse } = $action;
+
+    return 1;
+}
+
 =head2 $self->uri_for_action( $action, $captures )
 
 get a URI part for an action; always returns undef is $captures is set
index 3a97c87..cb22e63 100644 (file)
@@ -531,9 +531,29 @@ sub register {
         }
     }
 
+    my @dtypes = @{ $self->_dispatch_types };
+    my @normal_dtypes;
+    my @low_precedence_dtypes;
+
+    while (my $type = shift @dtypes) {
+        if ($type->isa('Catalyst::DispatchType::Index') ||  
+            $type->isa('Catalyst::DispatchType::Default')) {
+            push @low_precedence_dtypes, $type;
+        } else {
+            push @normal_dtypes, $type;
+        }
+    }
+
     # Pass the action to our dispatch types so they can register it if reqd.
-    foreach my $type ( @{ $self->_dispatch_types } ) {
-        $type->register( $c, $action );
+    my $was_registered = 0;
+    foreach my $type ( @normal_dtypes ) {
+        $was_registered = 1 if $type->register( $c, $action );
+    }
+
+    if (not $was_registered) {
+        foreach my $type ( @low_precedence_dtypes ) {
+            $type->register( $c, $action );
+        }
     }
 
     my $namespace = $action->namespace;
@@ -12,7 +12,7 @@ BEGIN { $iters = $ENV{CAT_BENCH_ITERS} || 1; }
 
 use Test::More tests => 1*$iters;
 
-use Catalyst::Test 'TestAppIndexDefault';
+use Catalyst::Test 'TestAppIndexActionName';
 
 if ( $ENV{CAT_BENCHMARK} ) {
     require Benchmark;
similarity index 67%
rename from t/lib/TestAppIndexDefault.pm
rename to t/lib/TestAppIndexActionName.pm
index 9a129cb..977693e 100644 (file)
@@ -1,4 +1,4 @@
-package TestAppIndexDefault;
+package TestAppIndexActionName;
 use strict;
 use warnings;
 use Catalyst;
@@ -1,4 +1,4 @@
-package TestAppIndexDefault::Controller::IndexChained;
+package TestAppIndexActionName::Controller::IndexChained;
 
 use base 'Catalyst::Controller';