current_view patch.
Marcus Ramberg [Wed, 11 Oct 2006 08:49:23 +0000 (08:49 +0000)]
Changes
lib/Catalyst.pm
t/unit_core_mvc.t

diff --git a/Changes b/Changes
index 4b70c08..fef5c0c 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,6 +1,7 @@
 This file documents the revision history for Perl extension Catalyst.
 
 ?        ?
+       - Support current_view
        - Allow use Catalyst::Test without app name (Ton Voon, Altinity)
 
 5.7003   2006-09-21 16:29:45
index dd4607a..fc8f49e 100644 (file)
@@ -501,8 +501,11 @@ Gets a L<Catalyst::Model> instance by name.
 
     $c->model('Foo')->do_stuff;
 
-If the name is omitted, it will look for a config setting 'default_model',
-or check if there is only one view, and return it if that's the case.
+If the name is omitted, it will look for 
+ - a model object in $c->stash{current_model_instance}, then
+ - a model name in $c->stash->{current_model}, then
+ - a config setting 'default_model', or
+ - check if there is only one model, and return it if that's the case.
 
 =cut
 
@@ -511,8 +514,14 @@ sub model {
     return $c->_filter_component( $c->_comp_prefixes( $name, qw/Model M/ ),
         @args )
       if $name;
-    return $c->component( $c->config->{default_model} )
-      if $c->config->{default_model};
+    if (ref $c) {
+        return $c->stash->{current_model_instance} 
+          if $c->stash->{current_model_instance};
+        return $c->model( $c->stash->{current_model} )
+          if $c->stash->{current_model};
+        return $c->model( $c->config->{default_model} )
+          if $c->config->{default_model};
+    }
     return $c->_filter_component( $c->_comp_singular(qw/Model M/), @args );
 
 }
@@ -535,9 +544,11 @@ Gets a L<Catalyst::View> instance by name.
 
     $c->view('Foo')->do_stuff;
 
-If the name is omitted, it will look for a config setting
-'default_view', or check if there is only one view, and forward to it if
-that's the case.
+If the name is omitted, it will look for 
+ - a view object in $c->stash{current_view_instance}, then
+ - a view name in $c->stash->{current_view}, then
+ - a config setting 'default_view', or
+ - check if there is only one view, and return it if that's the case.
 
 =cut
 
@@ -546,8 +557,14 @@ sub view {
     return $c->_filter_component( $c->_comp_prefixes( $name, qw/View V/ ),
         @args )
       if $name;
-    return $c->component( $c->config->{default_view} )
-      if $c->config->{default_view};
+    if (ref $c) {
+        return $c->stash->{current_view_instance} 
+          if $c->stash->{current_view_instance};
+        return $c->view( $c->stash->{current_view} )
+          if $c->stash->{current_view};
+        return $c->view( $c->config->{default_view} )
+          if $c->config->{default_view};
+    }
     return $c->_filter_component( $c->_comp_singular(qw/View V/) );
 }
 
index d79653e..d5dfb7e 100644 (file)
@@ -1,4 +1,4 @@
-use Test::More tests => 13;
+use Test::More tests => 23;
 use strict;
 use warnings;
 
@@ -50,3 +50,29 @@ is_deeply( [ sort MyApp->controllers ],
 is_deeply( [ sort MyApp->models ],
            [ qw/Dummy::Model M Model Test::Object/ ],
            'models ok');
+
+is (MyApp->view , 'MyApp::V::View', 'view() with no defaults ok');
+
+is ( bless ({stash=>{current_view=>'V'}}, 'MyApp')->view , 'MyApp::View::V', 'current_view ok');
+
+my $view = bless {} , 'MyApp::View::V'; 
+is ( bless ({stash=>{current_view_instance=> $view }}, 'MyApp')->view , $view, 'current_view_instance ok');
+
+is ( bless ({stash=>{current_view_instance=> $view, current_view=>'MyApp::V::View' }}, 'MyApp')->view , $view, 
+  'current_view_instance precedes current_view ok');
+
+is (MyApp->model , 'MyApp::M::Model', 'model() with no defaults ok');
+
+is ( bless ({stash=>{current_model=>'M'}}, 'MyApp')->model , 'MyApp::Model::M', 'current_model ok');
+
+my $model = bless {} , 'MyApp::Model::M'; 
+is ( bless ({stash=>{current_model_instance=> $model }}, 'MyApp')->model , $model, 'current_model_instance ok');
+
+is ( bless ({stash=>{current_model_instance=> $model, current_model=>'MyApp::M::Model' }}, 'MyApp')->model , $model, 
+  'current_model_instance precedes current_model ok');
+
+MyApp->config->{default_view} = 'V';
+is ( bless ({stash=>{}}, 'MyApp')->view , 'MyApp::View::V', 'default_view ok');
+
+MyApp->config->{default_model} = 'M';
+is ( bless ({stash=>{}}, 'MyApp')->model , 'MyApp::Model::M', 'default_model ok');