[Catalyst-commits] r11682 - in Catalyst-View-TD/trunk: lib/Catalyst/View t t/lib/TestApp/TD t/lib/TestApp/TD/HTML t/lib/TestApp/View

theory at dev.catalyst.perl.org theory at dev.catalyst.perl.org
Thu Oct 29 01:25:28 GMT 2009


Author: theory
Date: 2009-10-29 01:25:27 +0000 (Thu, 29 Oct 2009)
New Revision: 11682

Added:
   Catalyst-View-TD/trunk/t/06append_dispatchto.t
   Catalyst-View-TD/trunk/t/12tdstuff.t
   Catalyst-View-TD/trunk/t/lib/TestApp/TD/HTML.pm
   Catalyst-View-TD/trunk/t/lib/TestApp/TD/HTML/
   Catalyst-View-TD/trunk/t/lib/TestApp/TD/HTML/Util.pm
   Catalyst-View-TD/trunk/t/lib/TestApp/TD/NoAlias.pm
   Catalyst-View-TD/trunk/t/lib/TestApp/View/HTML.pm
   Catalyst-View-TD/trunk/t/lib/TestApp/View/NoAlias.pm
Modified:
   Catalyst-View-TD/trunk/lib/Catalyst/View/TD.pm
   Catalyst-View-TD/trunk/t/lib/TestApp/TD/Appconfig.pm
Log:
Added tests for TD-specific features:

* Added missing `t/06append_dispatchto.t`, which I forgot to include in the
  previous commit.
* Added tests to ensure the proper class is passed to templates.
* Added `auto_alias` parameter, which can be used to disable auto-aliasing.
* Fixed auto-aliasing, now that I have some tests for it.
* Started some docs on auto-aliasing. More to come (plus some more tests).
* Fixed a warning in `render()` when `$args` is `undef`. What a PITA.



Modified: Catalyst-View-TD/trunk/lib/Catalyst/View/TD.pm
===================================================================
--- Catalyst-View-TD/trunk/lib/Catalyst/View/TD.pm	2009-10-29 00:43:07 UTC (rev 11681)
+++ Catalyst-View-TD/trunk/lib/Catalyst/View/TD.pm	2009-10-29 01:25:27 UTC (rev 11682)
@@ -9,6 +9,7 @@
 
 __PACKAGE__->mk_accessors('init');
 __PACKAGE__->mk_accessors('dispatch_to');
+__PACKAGE__->mk_accessors('auto_alias');
 
 =head1 Name
 
@@ -66,12 +67,14 @@
         %{ $args },
     };
 
+    my $auto_alias = exists $config->{auto_alias} ? delete $config->{auto_alias} : 1;
+
     if (my $roots = $config->{dispatch_to}) {
         for my $root (@{ $roots }) {
             eval "require $root" or die $@ || "$root did not return a true value";
         }
     } else {
-        $config->{dispatch_to} = _load_templates( $class );
+        $config->{dispatch_to} = _load_templates( $class, $auto_alias );
     }
 
     my $self = $class->next::method( $c, { init => $config } );
@@ -79,22 +82,22 @@
     # Set base template casses. Local'd in render if needed
     $self->dispatch_to( delete $config->{dispatch_to} );
 
+    # Set other attributes.
+    $self->auto_alias( $auto_alias );
     return $self;
 }
 
 sub _load_templates {
-    my $class = shift;
+    my ($class, $auto_alias)  = @_;
 
     (my $root = $class) =~ s/::View::/::TD::/;
 
-    my $classes = [
-        Module::Pluggable::Object->new(
-            require     => 0,
-            search_path => $root,
-        )->plugins
-    ];
+    my @classes = $auto_alias ? Module::Pluggable::Object->new(
+        require     => 0,
+        search_path => $root,
+    )->plugins : ();
 
-    for my $mod ($root, $@{ $classes }) {
+    for my $mod ($root, @classes) {
         next unless $mod;
         # Load it.
         eval "require $mod" or die $@ || "$mod did not return a true value";
@@ -150,9 +153,16 @@
 
     $c->log->debug(qq/Rendering template "$template"/) if $c && $c->debug;
 
-    my $vars = {
-        ref $args eq 'HASH' ? %{ $args || {} } : $c ? %{ $c->stash } : {},
-    };
+    # The do prevents warnings when $args is undef.
+    my $vars = { do {
+        if (ref $args) {
+            %{ $args }
+        } elsif ($c) {
+            %{ $c->stash }
+        } else {
+            ()
+        }
+    } };
 
     my $init = $self->init;
 
@@ -323,6 +333,20 @@
 classes under the C<prepend_template_classes> and C<append_template_classes>
 keys. See L</"Capturing Template Output"> for an example.
 
+=head2 Auto-Aliasing
+
+Unless you specify C<dispatch_to> in your configuration, Catalyst::View::TD
+dispatches to only one template class. That class has the same name as your
+view class, except that C<View> is replaced with C<TD>. Thus, a view named
+C<MyApp::View::Whatever> would dispatch to the template class
+C<MyApp::TD::Whatever>. You can specify your own template class via the
+C<dispatch_to> configuration if you'd like to use a different template class.
+
+But the point is, that one template class (or however many you choose to
+include in the C<dispatch_to> configuration) is the only class who's templates
+will be available. But putting all of your templates in a single template
+class can get messy fast, especially if you have non-root actions.
+
 =head2 Rendering Views
 
 The Catlyst C<view()> method renders the template specified in the C<template>
@@ -421,18 +445,18 @@
 
 =head2 Methods
 
-=head2 C<new>
+=head3 C<new>
 
 The constructor for the TD view. Sets up the template provider and reads the
 application config.
 
-=head2 C<process>
+=head3 C<process>
 
 Renders the template specified in C<< $c->stash->{template} >> or
 C<< $c->action >> (the private name of the matched action). Calls L<render()>
 to perform actual rendering. Output is stored in C<< $c->response->body >>.
 
-=head2 C<render>
+=head3 C<render>
 
 Renders the given template and returns output, or a L<Template::Exception>
 object upon error.
@@ -440,7 +464,7 @@
 The template variables are set to C<$args> if $args is a hashref, or
 C<< $c->stash >> otherwise.
 
-=head2 C<config>
+=head3 C<config>
 
 This method allows your view subclass to pass additional settings to the TD
 configuration hash, or to set the options as below:

Added: Catalyst-View-TD/trunk/t/06append_dispatchto.t
===================================================================
--- Catalyst-View-TD/trunk/t/06append_dispatchto.t	                        (rev 0)
+++ Catalyst-View-TD/trunk/t/06append_dispatchto.t	2009-10-29 01:25:27 UTC (rev 11682)
@@ -0,0 +1,43 @@
+use strict;
+use warnings;
+use Test::More tests => 15;
+
+use FindBin;
+use lib "$FindBin::Bin/lib";
+
+use_ok('Catalyst::Test', 'TestApp');
+my $response;
+
+my $initial_dispatchto = [ @{ TestApp->view('Appconfig')->dispatch_to } ];
+
+ok(($response = request("/test_append_dispatchto?view=Appconfig&template=testclass&additionalclass=TestApp::TD::Additional"))->is_success, 'additional_template_class request');
+is($response->content, "From Additional: " . TestApp->config->{default_message}, 'additional_template_class message');
+
+is_deeply($initial_dispatchto,
+    TestApp->view('Appconfig')->dispatch_to,
+    'dispatchto is unchanged');
+
+ok(($response = request("/test_append_dispatchto?view=Appconfig&template=test&additionalclass=TestApp::TD::Additional"))->is_success, 'additional_template_class request');
+is($response->content, TestApp->config->{default_message}, 'appconfig_template_class message');
+
+is_deeply($initial_dispatchto,
+    TestApp->view('Appconfig')->dispatch_to,
+    'dispatchto is unchanged');
+
+ok(($response = request("/test_append_dispatchto?view=Additional&template=testclass"))->is_success, 'dispatchto set to the alternate class');
+is($response->content, "From Additional: " . TestApp->config->{default_message}, 'request to view using the alternate template class');
+
+ok(($response = request("/test_append_dispatchto?view=Appconfig&template=testclass&addclass=TestApp::TD::Additional"))->is_success, 'add class to the array');
+is($response->content, "From Additional: " . TestApp->config->{default_message}, 'added class template renders');
+
+is_deeply([@$initial_dispatchto, 'TestApp::TD::Additional'],
+    TestApp->view('Appconfig')->dispatch_to,
+    'dispatchto has been changed');
+
+ok(($response = request("/test_append_dispatchto?view=Appconfig&template=testclass&setclass=TestApp::TD::Additional"))->is_success, 'set dispatchto from request');
+is($response->content, "From Additional: " . TestApp->config->{default_message}, 'set class template renders');
+
+is_deeply(['TestApp::TD::Additional'],
+    TestApp->view('Appconfig')->dispatch_to,
+    'dispatchto has been overridden');
+

Added: Catalyst-View-TD/trunk/t/12tdstuff.t
===================================================================
--- Catalyst-View-TD/trunk/t/12tdstuff.t	                        (rev 0)
+++ Catalyst-View-TD/trunk/t/12tdstuff.t	2009-10-29 01:25:27 UTC (rev 11682)
@@ -0,0 +1,34 @@
+use strict;
+use warnings;
+use Test::More tests => 11;
+#use Test::More 'no_plan';
+
+use FindBin;
+use lib "$FindBin::Bin/lib";
+
+BEGIN { use_ok 'TestApp' or die }
+
+# Check basic inheritance.
+ok my $td = TestApp->view('Appconfig'), 'Get Appconfig view object';
+is $td->render(undef, 'test_self'), 'Self is TestApp::TD::Appconfig',
+    'self should be the subclass';
+
+is $td->render(undef, 'test_isa', { what => 'Template::Declare::Catalyst' }),
+    'Self is Template::Declare::Catalyst',
+    'self should be a Template::Declare::Catalyst';
+
+is $td->render(undef, 'test_isa', { what => 'Template::Declare' }),
+    'Self is Template::Declare',
+    'self should be a Template::Declare';
+
+# Check auto-aliasing.
+ok $td = TestApp->view('HTML'), 'Get HTML view object';
+ok $td->auto_alias, 'It should be auto-aliasing';
+is $td->render(undef, 'body'), "header\nbody\nfooter\n",
+    'Utility templates should be aliased';
+
+# Check manual aliasing.
+ok $td = TestApp->view('NoAlias'), 'Get NoAlias view object';
+ok !$td->auto_alias, 'It should not be auto-aliasing';
+is $td->render(undef, 'body'), "header\nbody\nfooter\n",
+    'It should have its own utility templates';

Modified: Catalyst-View-TD/trunk/t/lib/TestApp/TD/Appconfig.pm
===================================================================
--- Catalyst-View-TD/trunk/t/lib/TestApp/TD/Appconfig.pm	2009-10-29 00:43:07 UTC (rev 11681)
+++ Catalyst-View-TD/trunk/t/lib/TestApp/TD/Appconfig.pm	2009-10-29 01:25:27 UTC (rev 11682)
@@ -15,4 +15,14 @@
     outs "I should be a $args->{param} test in ", $self->c->config->{name};
 };
 
+template test_self => sub {
+    my ($self, $args) = @_;
+    outs "Self is $self";
+};
+
+template test_isa => sub {
+    my ($self, $args) = @_;
+    outs "Self is $args->{what}" if $self->isa($args->{what});
+};
+
 1;

Added: Catalyst-View-TD/trunk/t/lib/TestApp/TD/HTML/Util.pm
===================================================================
--- Catalyst-View-TD/trunk/t/lib/TestApp/TD/HTML/Util.pm	                        (rev 0)
+++ Catalyst-View-TD/trunk/t/lib/TestApp/TD/HTML/Util.pm	2009-10-29 01:25:27 UTC (rev 11682)
@@ -0,0 +1,15 @@
+package TestApp::TD::HTML::Util;
+
+use strict;
+use warnings;
+use Template::Declare::Tags;
+
+template header => sub {
+    outs "header\n";
+};
+
+template footer => sub {
+    outs "footer\n";
+};
+
+1;

Added: Catalyst-View-TD/trunk/t/lib/TestApp/TD/HTML.pm
===================================================================
--- Catalyst-View-TD/trunk/t/lib/TestApp/TD/HTML.pm	                        (rev 0)
+++ Catalyst-View-TD/trunk/t/lib/TestApp/TD/HTML.pm	2009-10-29 01:25:27 UTC (rev 11682)
@@ -0,0 +1,13 @@
+package TestApp::TD::HTML;
+
+use strict;
+use warnings;
+use Template::Declare::Tags;
+
+template body => sub {
+    show 'util/header';
+    outs "body\n";
+    show 'util/footer';
+};
+
+1;

Added: Catalyst-View-TD/trunk/t/lib/TestApp/TD/NoAlias.pm
===================================================================
--- Catalyst-View-TD/trunk/t/lib/TestApp/TD/NoAlias.pm	                        (rev 0)
+++ Catalyst-View-TD/trunk/t/lib/TestApp/TD/NoAlias.pm	2009-10-29 01:25:27 UTC (rev 11682)
@@ -0,0 +1,17 @@
+package TestApp::TD::NoAlias;
+
+use strict;
+use warnings;
+use base 'Template::Declare::Catalyst';
+use Template::Declare::Tags;
+use TestApp::TD::HTML::Util;
+
+alias TestApp::TD::HTML::Util under 'here';
+
+template body => sub {
+    show 'here/header';
+    outs "body\n";
+    show 'here/footer';
+};
+
+1;

Added: Catalyst-View-TD/trunk/t/lib/TestApp/View/HTML.pm
===================================================================
--- Catalyst-View-TD/trunk/t/lib/TestApp/View/HTML.pm	                        (rev 0)
+++ Catalyst-View-TD/trunk/t/lib/TestApp/View/HTML.pm	2009-10-29 01:25:27 UTC (rev 11682)
@@ -0,0 +1,7 @@
+package TestApp::View::HTML;
+
+use strict;
+use base 'Catalyst::View::TD';
+
+1;
+

Added: Catalyst-View-TD/trunk/t/lib/TestApp/View/NoAlias.pm
===================================================================
--- Catalyst-View-TD/trunk/t/lib/TestApp/View/NoAlias.pm	                        (rev 0)
+++ Catalyst-View-TD/trunk/t/lib/TestApp/View/NoAlias.pm	2009-10-29 01:25:27 UTC (rev 11682)
@@ -0,0 +1,8 @@
+package TestApp::View::NoAlias;
+
+use strict;
+use base 'Catalyst::View::TD';
+
+__PACKAGE__->config( auto_alias => 0 );
+
+1;




More information about the Catalyst-commits mailing list