[Catalyst-commits] r12550 - in
Catalyst-View-Email/branches/email-sender-porting:
lib/Catalyst/View lib/Catalyst/View/Email t
dhoss at dev.catalyst.perl.org
dhoss at dev.catalyst.perl.org
Sat Jan 9 00:20:04 GMT 2010
Author: dhoss
Date: 2010-01-09 00:20:03 +0000 (Sat, 09 Jan 2010)
New Revision: 12550
Modified:
Catalyst-View-Email/branches/email-sender-porting/lib/Catalyst/View/Email.pm
Catalyst-View-Email/branches/email-sender-porting/lib/Catalyst/View/Email/Template.pm
Catalyst-View-Email/branches/email-sender-porting/t/05template.t
Log:
05template now passes
Modified: Catalyst-View-Email/branches/email-sender-porting/lib/Catalyst/View/Email/Template.pm
===================================================================
--- Catalyst-View-Email/branches/email-sender-porting/lib/Catalyst/View/Email/Template.pm 2010-01-08 22:44:59 UTC (rev 12549)
+++ Catalyst-View-Email/branches/email-sender-porting/lib/Catalyst/View/Email/Template.pm 2010-01-09 00:20:03 UTC (rev 12550)
@@ -1,16 +1,11 @@
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;
+extends 'Catalyst::View::Email';
-use base qw/ Catalyst::View::Email /;
-
our $VERSION = '0.13';
=head1 NAME
@@ -98,11 +93,32 @@
# 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
# and you aren't required to modify your templates based on your desired engine
@@ -121,16 +137,18 @@
#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 @@
=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,58 +241,56 @@
=cut
-sub process {
- my ( $self, $c, @args ) = @_;
+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;
- # don't validate template_prefix
-
- # 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
=head1 TODO
Modified: Catalyst-View-Email/branches/email-sender-porting/lib/Catalyst/View/Email.pm
===================================================================
--- Catalyst-View-Email/branches/email-sender-porting/lib/Catalyst/View/Email.pm 2010-01-08 22:44:59 UTC (rev 12549)
+++ Catalyst-View-Email/branches/email-sender-porting/lib/Catalyst/View/Email.pm 2010-01-09 00:20:03 UTC (rev 12550)
@@ -331,10 +331,9 @@
# 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
Modified: Catalyst-View-Email/branches/email-sender-porting/t/05template.t
===================================================================
--- Catalyst-View-Email/branches/email-sender-porting/t/05template.t 2010-01-08 22:44:59 UTC (rev 12549)
+++ Catalyst-View-Email/branches/email-sender-porting/t/05template.t 2010-01-09 00:20:03 UTC (rev 12550)
@@ -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 @@
plan skip_all => 'Catalyst::View::TT required for Template tests';
exit;
}
-plan tests => 11;
use_ok('Catalyst::Test', 'TestApp');
@@ -19,15 +21,14 @@
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 @@
is($parts[1]->content_type, 'text/html', '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');
-
+done_testing();
More information about the Catalyst-commits
mailing list