[Catalyst-dev] [Patch] Catalyst::View::Email configuration

Daniel Westermann-Clark dwc at pobox.com
Thu Jul 12 03:02:14 GMT 2007


Hi,

I was playing around with Catalyst::View::Email and ran into some
problems configuring it via a configuration file (as opposed to
__PACKAGE__->config).

The problem was that View::Email was not storing the configuration
passed in from ConfigLoader.  The attached patch simply stores the
configuration at the beginning of new.  (This seems to be a relatively
common idiom, but I'd be happy to correct it if it's wrong.)  There
are some additional tests to verify this behavior.

I also made a couple of minor POD updates.  There were two places
where example code was not separated from the text, making the output
(on search.cpan.org at least) confusing.  I also updated the bit about
the default_view option; the key in the example did not match the
code.

Other than that, Email::View has been a cinch to use... Thanks!

-- 
Daniel Westermann-Clark
-------------- next part --------------
==== Patch <catalyst-view-email-config> level 1
Source: 99935cb0-661f-0410-81ef-b6fa668ad953:/local/Catalyst-View-Email:15204 [local]
Target: 4ad37cd2-5fec-0310-835f-b3785c72a374:/trunk/Catalyst-View-Email:6454 [mirrored]
        (http://dev.catalyst.perl.org/repos/Catalyst)
Log:
* Allow configuration from application or ConfigLoader
* Minor POD fixes
=== t/lib/TestApp/Controller/Root.pm
==================================================================
--- t/lib/TestApp/Controller/Root.pm	(revision 6454)
+++ t/lib/TestApp/Controller/Root.pm	(patch catalyst-view-email-config level 1)
@@ -31,6 +31,28 @@
     }
 }
 
+sub email_app_config : Global('email_app_config') {
+    my ($self, $c, @args) = @_;
+
+    my $time = $c->req->params->{time} || time;
+
+    $c->stash->{email} = {
+        to      => 'test-email at example.com',
+        from    => 'no-reply at example.com',
+        subject => 'Email Test',
+        body    => "Email Sent at: $time"
+    };
+
+    $c->forward('TestApp::View::Email::AppConfig');
+
+    if ( scalar( @{ $c->error } ) ) {
+        $c->res->status(500);
+        $c->res->body('Email Failed');
+    } else {
+        $c->res->body('Plain Email Ok');
+    }
+}
+
 sub template_email : Global('template_email') {
     my ($self, $c, @args) = @_;
 
=== t/lib/TestApp/View/Email/AppConfig.pm
==================================================================
--- t/lib/TestApp/View/Email/AppConfig.pm	(revision 6454)
+++ t/lib/TestApp/View/Email/AppConfig.pm	(patch catalyst-view-email-config level 1)
@@ -0,0 +1,8 @@
+package # Hide from PAUSE
+    TestApp::View::Email::AppConfig;
+
+use Email::Send::Test;
+
+use base 'Catalyst::View::Email';
+
+1;
=== t/lib/TestApp.pm
==================================================================
--- t/lib/TestApp.pm	(revision 6454)
+++ t/lib/TestApp.pm	(patch catalyst-view-email-config level 1)
@@ -6,7 +6,12 @@
 
 TestApp->config(
     root => "$FindBin::Bin/root",
-    default_view => 'TT'
+    default_view => 'TT',
+    'View::Email::AppConfig' => {
+        sender => {
+            method => 'Test',
+        },
+    },
 );
 
 TestApp->setup;
=== t/06config.t
==================================================================
--- t/06config.t	(revision 6454)
+++ t/06config.t	(patch catalyst-view-email-config level 1)
@@ -0,0 +1,19 @@
+use strict;
+use warnings;
+use Test::More tests => 5;
+
+use Email::Send::Test;
+use FindBin;
+use lib "$FindBin::Bin/lib";
+
+use_ok('Catalyst::Test', 'TestApp');
+
+my $response;
+my $time = time;
+ok( ($response = request("/email_app_config?time=$time"))->is_success, 'request ok');
+
+my @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');
=== lib/Catalyst/View/Email.pm
==================================================================
--- lib/Catalyst/View/Email.pm	(revision 6454)
+++ lib/Catalyst/View/Email.pm	(patch catalyst-view-email-config level 1)
@@ -11,7 +11,7 @@
 
 use base qw|Catalyst::View|;
 
-our $VERSION = '0.01';
+our $VERSION = '0.02';
 
 __PACKAGE__->mk_accessors('mailer');
 
@@ -27,6 +27,7 @@
 =head1 CONFIGURATION
 
 In your app configuration (example in L<YAML>):
+
     View::Email:
         stash_key: email
         content_type: text/plain 
@@ -96,21 +97,23 @@
 
 =head1 OTHER MAILERS
 
-Now, it's no fun to just send out email using plain strings.  We also have
-L<Catalyst::View::Email::TT> for use.  You can also toggle this as being used
-by setting up your configuration to look like this:
+Now, it's no fun to just send out email using plain strings.  We also
+have L<Catalyst::View::Email::Template> for use.  You can also toggle
+this as being used by setting up your configuration to look like this:
 
     View::Email:
-        default: TT
+        default_view: TT
 
-Then, Catalyst::View::Email will forward to View::Email::TT by default.
+Then, Catalyst::View::Email will forward to your View::TT by default.
 
 =cut
 
 sub new {
-    my ( $class ) = shift;
-    my $self = $class->next::method(@_);
+    my $self = shift->next::method(@_);
 
+    my ( $c, $arguments ) = @_;
+    $self->config($arguments);
+
     my $mailer = Email::Send->new;
 
     if ( my $method = $self->config->{sender}->{method} ) {
=== lib/Catalyst/View/Email/Template.pm
==================================================================
--- lib/Catalyst/View/Email/Template.pm	(revision 6454)
+++ lib/Catalyst/View/Email/Template.pm	(patch catalyst-view-email-config level 1)
@@ -10,7 +10,7 @@
 
 use base qw|Catalyst::View::Email|;
 
-our $VERSION = '0.01';
+our $VERSION = '0.02';
 
 =head1 NAME
 
@@ -149,6 +149,7 @@
 
 There needs to be a method to support attachments.  What I am thinking is
 something along these lines:
+
     attachments => [
         # Set the body to a file handle object, specify content_type and
         # the file name. (name is what it is sent at, not the file)
=== Changes
==================================================================
--- Changes	(revision 6454)
+++ Changes	(patch catalyst-view-email-config level 1)
@@ -0,0 +1,8 @@
+Revision history for Perl extension Catalyst::View::Email.
+
+0.02
+        - Allow configuration from application or ConfigLoader
+        - Minor POD updates
+
+0.01  2007-06-03
+        - Initial release

Property changes on: 
___________________________________________________________________
Name: svn:ignore
 +Makefile
 +MANIFEST
 +MANIFEST.bak
 +META.yml
 +README
 +pm_to_blib
 +blib
 +Build
 +_build
 +inc
 +cover_db
 +*.bak
 +*.gz
 +



More information about the Catalyst-dev mailing list