From: Devin Austin Date: Sat, 9 Jan 2010 00:20:03 +0000 (+0000) Subject: 05template now passes X-Git-Tag: v0.14~1^2~7 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-View-Email.git;a=commitdiff_plain;h=ce1977b02b6645d2e78fbda46c259891caf8579b 05template now passes --- diff --git a/lib/Catalyst/View/Email.pm b/lib/Catalyst/View/Email.pm index 70991c2..7c77b18 100644 --- a/lib/Catalyst/View/Email.pm +++ b/lib/Catalyst/View/Email.pm @@ -331,10 +331,9 @@ sub generate_message { # setup the attributes (merge with defaultis) $attr->{attributes} = $self->setup_attributes($c, $attr->{attributes}); - return Email::Simple->create( - header => $attr->{header}, - body => $attr->{body} - ); + Email::MIME->create( + %$attr + ); } =back diff --git a/lib/Catalyst/View/Email/Template.pm b/lib/Catalyst/View/Email/Template.pm index 4a428fb..72aa085 100644 --- a/lib/Catalyst/View/Email/Template.pm +++ b/lib/Catalyst/View/Email/Template.pm @@ -1,15 +1,10 @@ package Catalyst::View::Email::Template; -use warnings; -use strict; - -use Class::C3; +use Moose; use Carp; use Scalar::Util qw/ blessed /; -use Email::MIME::Creator; - -use base qw/ Catalyst::View::Email /; +extends 'Catalyst::View::Email'; our $VERSION = '0.13'; @@ -98,10 +93,31 @@ See L. # here the defaults of Catalyst::View::Email are extended by the additional # ones Template.pm needs. -__PACKAGE__->config( - template_prefix => '', +has 'stash_key' => ( + is => 'rw', + isa => 'Str', + default => sub { "email" }, + lazy => 1, ); +has 'template_prefix' => ( + is => 'rw', + isa => 'Str', + default => sub { '' }, + lazy => 1, +); + +has 'default' => ( + is => 'rw', + isa => 'HashRef', + default => sub { + { + view => 'TT', + content_type => 'text/html', + }; + }, + lazy => 1, +); # This view hitches into your default view and will call the render function # on the templates provided. This means that you have a layer of abstraction @@ -121,16 +137,18 @@ __PACKAGE__->config( #multipart/alternative sub _validate_view { - my ($self, $view) = @_; - + my ( $self, $view ) = @_; + croak "C::V::Email::Template's configured view '$view' isn't an object!" - unless (blessed($view)); + unless ( blessed($view) ); - croak "C::V::Email::Template's configured view '$view' isn't an Catalyst::View!" - unless ($view->isa('Catalyst::View')); + croak + "C::V::Email::Template's configured view '$view' isn't an Catalyst::View!" + unless ( $view->isa('Catalyst::View') ); - croak "C::V::Email::Template's configured view '$view' doesn't have a render method!" - unless ($view->can('render')); + croak +"C::V::Email::Template's configured view '$view' doesn't have a render method!" + unless ( $view->can('render') ); } =head1 METHODS @@ -145,46 +163,65 @@ every template piece is a separate part that is included in the email. =cut sub generate_part { - my ($self, $c, $attrs) = @_; + my ( $self, $c, $attrs ) = @_; - my $template_prefix = $self->{template_prefix}; - my $default_view = $self->{default}->{view}; - my $default_content_type = $self->{default}->{content_type}; - my $default_charset = $self->{default}->{charset}; + my $template_prefix = $self->template_prefix; + my $default_view = $self->default->{view}; + my $default_content_type = $self->default->{content_type}; + my $default_charset = $self->default->{charset}; my $view; + # use the view specified for the email part - if (exists $attrs->{view} && defined $attrs->{view} && $attrs->{view} ne '') { - $view = $c->view($attrs->{view}); - $c->log->debug("C::V::Email::Template uses specified view $view for rendering.") if $c->debug; + if ( exists $attrs->{view} + && defined $attrs->{view} + && $attrs->{view} ne '' ) + { + $view = $c->view( $attrs->{view} ); + $c->log->debug( + "C::V::Email::Template uses specified view $view for rendering.") + if $c->debug; } + # if none specified use the configured default view elsif ($default_view) { $view = $c->view($default_view); - $c->log->debug("C::V::Email::Template uses default view $view for rendering.") if $c->debug;; + $c->log->debug( + "C::V::Email::Template uses default view $view for rendering.") + if $c->debug; } + # else fallback to Catalysts default view else { $view = $c->view; - $c->log->debug("C::V::Email::Template uses Catalysts default view $view for rendering.") if $c->debug;; + $c->log->debug( +"C::V::Email::Template uses Catalysts default view $view for rendering." + ) if $c->debug; } # validate the per template view $self->_validate_view($view); - + # prefix with template_prefix if configured - my $template = $template_prefix ne '' ? join('/', $template_prefix, $attrs->{template}) : $attrs->{template}; - + my $template = + $template_prefix ne '' + ? join( '/', $template_prefix, $attrs->{template} ) + : $attrs->{template}; + # setup the attributes (merge with defaults) - my $e_m_attrs = $self->setup_attributes($c, $attrs); + my $e_m_attrs = $self->SUPER::setup_attributes( $c, $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}, - }); - + my $output = $view->render( + $c, + $template, + { + content_type => $e_m_attrs->{content_type}, + stash_key => $self->stash_key, + %{ $c->stash }, + } + ); + if ( ref $output ) { croak $output->can('as_string') ? $output->as_string : $output; } @@ -204,57 +241,55 @@ L. =cut -sub process { - my ( $self, $c, @args ) = @_; - - # don't validate template_prefix +around 'process' => sub { + my ( $orig, $self, $c, @args ) = @_; + my $stash_key = $self->stash_key; + return $self->SUPER::process( $c, @args ) + unless $c->stash->{$stash_key}->{template} + or $c->stash->{$stash_key}->{templates}; + warn "Stash: " . $stash_key; - # the default view is validated if used - - # the content type should be validated by Email::MIME::Creator - - my $stash_key = $self->{stash_key}; - - # Go upstream if we don't have a template - $self->next::method($c, @args) - unless $c->stash->{$stash_key}->{template} - or $c->stash->{$stash_key}->{templates}; - - # this array holds the Email::MIME objects # in case of the simple api only one - my @parts = (); + my @parts = (); # now find out if the single or multipart api was used # prefer the multipart one - + # multipart api - if ($c->stash->{$stash_key}->{templates} + if ( $c->stash->{$stash_key}->{templates} && ref $c->stash->{$stash_key}->{templates} eq 'ARRAY' - && ref $c->stash->{$stash_key}->{templates}[0] eq 'HASH') { + && 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, { - view => $part->{view}, - template => $part->{template}, - content_type => $part->{content_type}, - charset => $part->{charset}, - }); + foreach my $part ( @{ $c->stash->{$stash_key}->{templates} } ) { + 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, { - template => $c->stash->{$stash_key}->{template}, - }); + elsif ( $c->stash->{$stash_key}->{template} ) { + push @parts, + $self->generate_part( $c, + { template => $c->stash->{$stash_key}->{template}, } ); } - + delete $c->stash->{$stash_key}->{body}; $c->stash->{$stash_key}->{parts} ||= []; - push @{$c->stash->{$stash_key}->{parts}}, @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); -} + return $self->SUPER::process($c); + +}; =back diff --git a/t/05template.t b/t/05template.t index 2e450f8..8f9e5d8 100644 --- a/t/05template.t +++ b/t/05template.t @@ -1,9 +1,12 @@ use strict; use warnings; + +BEGIN { $ENV{EMAIL_SENDER_TRANSPORT} = 'Test' } use Test::More; -use Email::Send::Test; +use Email::Sender::Simple; use FindBin; +use Data::Dumper; use lib "$FindBin::Bin/lib"; eval "use Catalyst::View::TT"; @@ -11,7 +14,6 @@ if ( $@ ) { plan skip_all => 'Catalyst::View::TT required for Template tests'; exit; } -plan tests => 11; use_ok('Catalyst::Test', 'TestApp'); @@ -19,15 +21,14 @@ my $response; my $time = time; ok( ( $response = request("/template_email?time=$time"))->is_success, 'request ok' ); +my @emails = Email::Sender::Simple->default_transport->deliveries; like( $response->content, qr/Template Email Ok/, 'controller says ok' ); -my @emails = Email::Send::Test->emails; - cmp_ok(@emails, '==', 1, 'got emails'); -isa_ok( $emails[0], 'Email::MIME', 'email is ok' ); +isa_ok( $emails[0]->{'email'}, 'Email::Abstract', 'email is ok' ); -like($emails[0]->content_type, qr#^multipart/alternative#, 'Multipart email'); +like($emails[0]->{'email'}[0]->header("Content-type"), qr#^multipart/alternative#, 'Multipart email'); -my @parts = $emails[0]->parts; +my @parts = $emails[0]->{'email'}[0]->parts; cmp_ok(@parts, '==', 2, 'got parts'); is($parts[0]->content_type, 'text/plain', 'text/plain part ok'); @@ -36,4 +37,4 @@ like($parts[0]->body, qr/test-email\@example.com on $time/, 'got content back'); is($parts[1]->content_type, 'text/html', 'text/html ok'); like($parts[1]->body, qr{test-email\@example.com on $time}, 'got content back'); #like($emails[0]->body, qr/$time/, 'Got our email'); - +done_testing();