[Catalyst] Catalyst::View::Email::Template config issue

Daniel Westermann-Clark dwc at pobox.com
Wed Jul 25 16:29:56 GMT 2007


On 2007-07-25 11:01:24 -0400, Alan Spector wrote:
> I'm trying to use Catalyst::View::Email::Template but am having some
> trouble with it picking up my yml configuration options.

Oops, that's my fault.  Attached is a patch to fix the config for
View::Email::Template as in r6525 and to use the configured stash_key
when building the email (otherwise it won't find a body).

Additional tests included.  :)

-- 
Daniel Westermann-Clark
-------------- next part --------------
==== Patch <cvet_config> level 1
Source: 99935cb0-661f-0410-81ef-b6fa668ad953:/local/Catalyst-View-Email:15404 [local]
Target: 4ad37cd2-5fec-0310-835f-b3785c72a374:/trunk/Catalyst-View-Email:6580 [mirrored]
        (http://dev.catalyst.perl.org/repos/Catalyst)
Log:
Fix config as in r6525 for ::Template and add tests for this
=== t/root/text_plain/test.tt
==================================================================
--- t/root/text_plain/test.tt	(revision 6580)
+++ t/root/text_plain/test.tt	(patch cvet_config level 1)
@@ -1,3 +1,3 @@
 I am plain text.  I have no style.
 
-I was sent to [% email.to %] on [% time %]
+I was sent to [% $stash_key.to %] on [% time %]
=== t/root/text_html/test.tt
==================================================================
--- t/root/text_html/test.tt	(revision 6580)
+++ t/root/text_html/test.tt	(patch cvet_config level 1)
@@ -1,7 +1,7 @@
 <html>
  <body>
   <h1>Look at my style</h1>
-  <p>I was sent to <em>[% email.to %]</em> on [% time %]</p>
+  <p>I was sent to <em>[% $stash_key.to %]</em> on [% time %]</p>
  </body>
 </html>
 
=== t/lib/TestApp/Controller/Root.pm
==================================================================
--- t/lib/TestApp/Controller/Root.pm	(revision 6580)
+++ t/lib/TestApp/Controller/Root.pm	(patch cvet_config level 1)
@@ -79,4 +79,30 @@
     }
 }
 
+sub template_email_app_config : Global('template_email_app_config') {
+    my ($self, $c, @args) = @_;
+
+    $c->stash->{time} = $c->req->params->{time} || time;
+
+    $c->stash->{template_email} = {
+        to      => 'test-email at example.com',
+        from    => 'no-reply at example.com',
+        subject => 'Just a test',
+        content_type => 'multipart/alternative',
+        templates => [
+            qw{text_plain/test.tt},
+            qw{text_html/test.tt}
+        ]
+    };
+
+    $c->forward('TestApp::View::Email::Template::AppConfig');
+
+    if ( scalar( @{ $c->error } ) ) {
+        $c->res->status(500);
+        $c->res->body('Template Email Failed');
+    } else {
+        $c->res->body('Template Email Ok');
+    }
+}
+
 1;
=== t/lib/TestApp/View/Email/Template	(new directory)
==================================================================
=== t/lib/TestApp/View/Email/Template/AppConfig.pm
==================================================================
--- t/lib/TestApp/View/Email/Template/AppConfig.pm	(revision 6580)
+++ t/lib/TestApp/View/Email/Template/AppConfig.pm	(patch cvet_config level 1)
@@ -0,0 +1,8 @@
+package # Hide from PAUSE
+    TestApp::View::Email::Template::AppConfig;
+
+use Email::Send::Test;
+
+use base 'Catalyst::View::Email::Template';
+
+1;
=== t/lib/TestApp.pm
==================================================================
--- t/lib/TestApp.pm	(revision 6580)
+++ t/lib/TestApp.pm	(patch cvet_config level 1)
@@ -12,6 +12,12 @@
             method => 'Test',
         },
     },
+    'View::Email::Template::AppConfig' => {
+        stash_key => 'template_email',
+        sender => {
+            method => 'Test',
+        },
+    },
 );
 
 TestApp->setup;
=== t/06config.t
==================================================================
--- t/06config.t	(revision 6580)
+++ t/06config.t	(patch cvet_config level 1)
@@ -1,6 +1,6 @@
 use strict;
 use warnings;
-use Test::More tests => 5;
+use Test::More tests => 13;
 
 use Email::Send::Test;
 use FindBin;
@@ -9,11 +9,32 @@
 use_ok('Catalyst::Test', 'TestApp');
 
 my $response;
-my $time = time;
+my $time;
+my @emails;
+
+$time = time;
 ok( ($response = request("/email_app_config?time=$time"))->is_success, 'request ok');
 
-my @emails = Email::Send::Test->emails;
+ at emails = Email::Send::Test->emails;
 
 is(@emails, 1, "got emails");
 isa_ok( $emails[0], 'Email::MIME', 'email is ok' );
 like($emails[0]->body, qr/$time/, 'Got our email');
+
+Email::Send::Test->clear;
+
+
+$time = time;
+ok( ($response = request("/template_email_app_config?time=$time"))->is_success, 'request ok');
+
+ at emails = Email::Send::Test->emails;
+
+is(@emails, 1, "got emails");
+isa_ok( $emails[0], 'Email::MIME', 'email is ok' );
+my @parts = $emails[0]->parts;
+cmp_ok(@parts, '==', 2, 'got parts');
+
+is($parts[0]->content_type, 'text/plain; charset="us-ascii"', 'text/plain ok');
+like($parts[0]->body, qr/test-email\@example.com on $time/, 'got content back');
+is($parts[1]->content_type, 'text/html; charset="us-ascii"', 'text/html ok');
+like($parts[1]->body, qr{<em>test-email\@example.com</em> on $time}, 'got content back');
=== lib/Catalyst/View/Email.pm
==================================================================
--- lib/Catalyst/View/Email.pm	(revision 6580)
+++ lib/Catalyst/View/Email.pm	(patch cvet_config level 1)
@@ -11,7 +11,7 @@
 
 use base qw|Catalyst::View|;
 
-our $VERSION = '0.04';
+our $VERSION = '0.05';
 
 __PACKAGE__->mk_accessors(qw(sender stash_key content_type mailer));
 
=== lib/Catalyst/View/Email/Template.pm
==================================================================
--- lib/Catalyst/View/Email/Template.pm	(revision 6580)
+++ lib/Catalyst/View/Email/Template.pm	(patch cvet_config level 1)
@@ -10,8 +10,10 @@
 
 use base qw|Catalyst::View::Email|;
 
-our $VERSION = '0.02';
+our $VERSION = '0.05';
 
+__PACKAGE__->mk_accessors(qw(default_view template_prefix));
+
 =head1 NAME
 
 Catalyst::View::Email::Template - Send Templated Email from Catalyst
@@ -82,14 +84,14 @@
 sub process {
     my ( $self, $c ) = @_;
 
-    my $stash_key       = $self->config->{stash_key} || 'email';
+    my $stash_key = $self->stash_key || 'email';
 
     croak "No template specified for rendering"
         unless $c->stash->{$stash_key}->{template} or
                 $c->stash->{$stash_key}->{templates};
 
     # Where to look
-    my $template_prefix = $self->config->{template_prefix};
+    my $template_prefix = $self->template_prefix;
     my @templates = ();
     if ( $c->stash->{$stash_key}->{templates} ) {
         push @templates, map {
@@ -101,7 +103,7 @@
             $c->stash->{$stash_key}->{template});
     }
    
-    my $default_view = $c->view( $self->config->{default_view} );
+    my $default_view = $c->view( $self->default_view );
 
     unless ( $default_view->can('render') ) {
         croak "Email::Template's configured view does not have a render method!";
@@ -122,8 +124,11 @@
         } else {
             $content_type = 'text/plain';
         }
-        my $output = $default_view->render( $c, $template,
-            { content_type => $content_type, %{$c->stash} });
+        my $output = $default_view->render( $c, $template, {
+            content_type => $content_type,
+            stash_key => $self->stash_key,
+            %{$c->stash},
+        });
         # Got a ref, not a scalar.  An error!
         if ( ref $output ) {
             croak $output->can("as_string") ? $output->as_string : $output;
@@ -135,9 +140,9 @@
             body => $output
         );
     }
-    delete $c->stash->{email}->{body};
-    $c->stash->{email}->{parts} ||= [];
-    push @{$c->stash->{email}->{parts}}, @parts;
+    delete $c->stash->{$stash_key}->{body};
+    $c->stash->{$stash_key}->{parts} ||= [];
+    push @{$c->stash->{$stash_key}->{parts}}, @parts;
 
     # Let C::V::Email do the actual sending.  We just assemble the tasty bits.
     return $self->next::method($c);


More information about the Catalyst mailing list