[Catalyst-commits] r7078 - in Catalyst-View-Email/0.10: lib/Catalyst/View lib/Catalyst/View/Email t t/lib/TestApp/Controller

jshirley at dev.catalyst.perl.org jshirley at dev.catalyst.perl.org
Tue Oct 30 16:07:41 GMT 2007


Author: jshirley
Date: 2007-10-30 16:07:41 +0000 (Tue, 30 Oct 2007)
New Revision: 7078

Modified:
   Catalyst-View-Email/0.10/lib/Catalyst/View/Email.pm
   Catalyst-View-Email/0.10/lib/Catalyst/View/Email/Template.pm
   Catalyst-View-Email/0.10/t/05template.t
   Catalyst-View-Email/0.10/t/lib/TestApp/Controller/Root.pm
Log:
Minor refactor of the way generate_part works, having the default attribute merging in a separate method in ::Email.

Modified: Catalyst-View-Email/0.10/lib/Catalyst/View/Email/Template.pm
===================================================================
--- Catalyst-View-Email/0.10/lib/Catalyst/View/Email/Template.pm	2007-10-30 00:47:50 UTC (rev 7077)
+++ Catalyst-View-Email/0.10/lib/Catalyst/View/Email/Template.pm	2007-10-30 16:07:41 UTC (rev 7078)
@@ -141,7 +141,7 @@
         unless ($view->can('render'));
 }
 
-sub _generate_part {
+sub generate_part {
     my ($self, $c, $attrs) = @_;
     
     my $template_prefix         = $self->{template_prefix};
@@ -149,8 +149,6 @@
     my $default_content_type    = $self->{default}->{content_type};
     my $default_charset         = $self->{default}->{charset};
 
-    my $e_m_attrs = {};
-
     my $view;
     # use the view specified for the email part
     if (exists $attrs->{view} && defined $attrs->{view} && $attrs->{view} ne '') {
@@ -173,35 +171,24 @@
     
     # prefix with template_prefix if configured
     my $template = $template_prefix ne '' ? join('/', $template_prefix, $attrs->{template}) : $attrs->{template};
-    
-    if (exists $attrs->{content_type} && defined $attrs->{content_type} && $attrs->{content_type} ne '') {
-        $e_m_attrs->{content_type} = $attrs->{content_type};
-    }
-    elsif (defined $default_content_type && $default_content_type ne '') {
-        $e_m_attrs->{content_type} = $default_content_type;
-    }
    
-    if (exists $attrs->{charset} && defined $attrs->{charset} && $attrs->{charset} ne '') {
-        $e_m_attrs->{charset} = $attrs->{charset};
-    }
-    elsif (defined $default_charset && $default_charset ne '') {
-        $e_m_attrs->{charset} = $default_charset;
-    }
+    # setup the attributes (merge with defaults)
+    my $e_m_attrs = $self->setup_attributes($attrs);
 
     # render the email part
     my $output = $view->render( $c, $template, { 
         content_type    => $e_m_attrs->{content_type},
         stash_key       => $self->{stash_key},
-	%{$c->stash},
+        %{$c->stash},
     });
-				
-    if (ref $output) {
-	croak $output->can('as_string') ? $output->as_string : $output;
+    
+    if ( ref $output ) {
+        croak $output->can('as_string') ? $output->as_string : $output;
     }
-	
+
     return Email::MIME->create(
         attributes => $e_m_attrs,
-	body => $output,
+        body       => $output,
     );
 }
 
@@ -233,17 +220,17 @@
         && ref $c->stash->{$stash_key}->{templates}[0] eq 'HASH') {
         # loop through all parts of the mail
         foreach my $part (@{$c->stash->{$stash_key}->{templates}}) {
-            push @parts, $self->_generate_part($c, {
+            push @parts, $self->generate_part($c, {
                 view            => $part->{view},
                 template        => $part->{template},
                 content_type    => $part->{content_type},
                 charset         => $part->{charset},
             });
-	}
+        }
     }
     # single part api
     elsif($c->stash->{$stash_key}->{template}) {
-        push @parts, $self->_generate_part($c, {
+        push @parts, $self->generate_part($c, {
             template    => $c->stash->{$stash_key}->{template},
         });
     }

Modified: Catalyst-View-Email/0.10/lib/Catalyst/View/Email.pm
===================================================================
--- Catalyst-View-Email/0.10/lib/Catalyst/View/Email.pm	2007-10-30 00:47:50 UTC (rev 7077)
+++ Catalyst-View-Email/0.10/lib/Catalyst/View/Email.pm	2007-10-30 16:07:41 UTC (rev 7078)
@@ -41,6 +41,14 @@
             # Defines the default content type (mime type).
             # mandatory
             content_type: text/plain
+            # Defines the default charset for every MIME part with the content
+            # type text.
+            # According to RFC2049 a MIME part without a charset should
+            # be treated as US-ASCII by the mail client.
+            # If the charset is not set it won't be set for all MIME parts
+            # without an overridden one.
+            # Default: none
+            charset: utf-8
         # Setup how to send the email
         # all those options are passed directly to Email::Send
         sender:
@@ -207,9 +215,17 @@
     } else {
         $mime{body} = $body;
     }
+    
+    if ( $mime{attributes} and not $mime{attributes}->{charset} and
+         $self->{default}->{charset} )
+    {
+        $mime{attributes}->{charset} = $self->{default}->{charset};
+    }
 
-    my $message = Email::MIME->create(%mime);
+    my $message = $self->generate_part( \%mime );
 
+    #my $message = Email::MIME->create(%mime);
+
     if ( $message ) {
         my $return = $self->mailer->send($message);
         croak "$return" if !$return;
@@ -218,6 +234,39 @@
     }
 }
 
+sub setup_attributes {
+    my ( $self, $c, $attrs ) = @_;
+    
+    my $default_content_type    = $self->{default}->{content_type};
+    my $default_charset         = $self->{default}->{charset};
+
+    my $e_m_attrs = {};
+
+    if (exists $attrs->{content_type} && defined $attrs->{content_type} && $attrs->{content_type} ne '') {
+        $e_m_attrs->{content_type} = $attrs->{content_type};
+    }
+    elsif (defined $default_content_type && $default_content_type ne '') {
+        $e_m_attrs->{content_type} = $default_content_type;
+    }
+   
+    if (exists $attrs->{charset} && defined $attrs->{charset} && $attrs->{charset} ne '') {
+        $e_m_attrs->{charset} = $attrs->{charset};
+    }
+    elsif (defined $default_charset && $default_charset ne '') {
+        $e_m_attrs->{charset} = $default_charset;
+    }
+
+    return $e_m_attrs;
+}
+
+sub generate_part {
+    my ( $self, $attr ) = @_;
+
+    # setup the attributes (merge with defaults)
+    $attr->{attributes} = $self->setup_attributes($attr->{attributes});
+    return Email::MIME->create(%$attr);
+}
+
 =head1 SEE ALSO
 
 =head2 L<Catalyst::View::Email::Template> - Send fancy template emails with Cat

Modified: Catalyst-View-Email/0.10/t/05template.t
===================================================================
--- Catalyst-View-Email/0.10/t/05template.t	2007-10-30 00:47:50 UTC (rev 7077)
+++ Catalyst-View-Email/0.10/t/05template.t	2007-10-30 16:07:41 UTC (rev 7078)
@@ -29,6 +29,7 @@
 
 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');
 #like($emails[0]->body, qr/$time/, 'Got our email');

Modified: Catalyst-View-Email/0.10/t/lib/TestApp/Controller/Root.pm
===================================================================
--- Catalyst-View-Email/0.10/t/lib/TestApp/Controller/Root.pm	2007-10-30 00:47:50 UTC (rev 7077)
+++ Catalyst-View-Email/0.10/t/lib/TestApp/Controller/Root.pm	2007-10-30 16:07:41 UTC (rev 7078)
@@ -126,6 +126,7 @@
         to      => 'test-email at example.com',
         from    => 'no-reply at example.com',
         subject => 'Just a test',
+        view    => 'Mason',
         templates => [
             {
                 template        => 'text_plain/test.m',




More information about the Catalyst-commits mailing list