[Jifty-commit] r4482 - in apps/spensive: . lib/Spensive/Action lib/Spensive/Model

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Tue Nov 20 12:10:28 EST 2007


Author: jesse
Date: Tue Nov 20 12:10:27 2007
New Revision: 4482

Added:
   apps/spensive/lib/Spensive/Model/Email.pm
   apps/spensive/lib/Spensive/Model/EmailAttachment.pm
   apps/spensive/t/00-model-Email.t
   apps/spensive/t/00-model-EmailAttachment.t
   apps/spensive/t/mailbox
Removed:
   apps/spensive/lib/Spensive/Model/Attachment.pm
   apps/spensive/t/00-model-Attachment.t
Modified:
   apps/spensive/   (props changed)
   apps/spensive/lib/Spensive/Action/EmailDispatch.pm
   apps/spensive/lib/Spensive/Model/Expense.pm

Log:
 r67145 at pinglin (orig r7316):  jesse | 2007-09-12 20:20:01 -0400
 


Modified: apps/spensive/lib/Spensive/Action/EmailDispatch.pm
==============================================================================
--- apps/spensive/lib/Spensive/Action/EmailDispatch.pm	(original)
+++ apps/spensive/lib/Spensive/Action/EmailDispatch.pm	Tue Nov 20 12:10:27 2007
@@ -98,7 +98,7 @@
                     title       => $summary,
                     description   => $body,
                     incurred_by => $pa->id,
-                    #email_content => $self->argument_value('email'),
+                    email_content => $self->argument_value('email'),
                 },
                 # We clobber the current_user for group tasks so
                 # anyone can create group tasks, which is not usually

Added: apps/spensive/lib/Spensive/Model/Email.pm
==============================================================================
--- (empty file)
+++ apps/spensive/lib/Spensive/Model/Email.pm	Tue Nov 20 12:10:27 2007
@@ -0,0 +1,52 @@
+use strict;
+use warnings;
+
+package Spensive::Model::Email;
+use Jifty::DBI::Schema;
+
+use Spensive::Record schema {
+    column expense => references Spensive::Model::Expense;
+    column message => type is 'text';
+};
+
+# Your model-specific methods go here.
+
+
+
+sub attachments {
+    my $self = shift;
+    my $c = Spensive::Model::EmailAttachmentCollection->new();
+    $c->limit(column => 'email', value => $self->id);
+
+}
+sub after_create {
+    my $self = shift;
+      my $insert_return_value_ref = shift;
+
+      return unless $$insert_return_value_ref;    # bail if insert failed
+      $self->load($$insert_return_value_ref);     # load ourselves from db
+
+    my $mime = Email::MIME->new($self->__value('message'));
+
+    $self->_create_subparts($mime);
+
+}
+
+sub _create_subparts {
+    my $self = shift;
+    my $top_part = shift;
+    my @subparts = $top_part->subparts;
+    if (@subparts) {
+        foreach my $part (@subparts) {
+            $self->_create_subparts($part);
+        }
+    } else {
+        my $attachment = Spensive::Model::EmailAttachment->new();
+        $attachment->create(mime_obj => $top_part);
+    }
+
+
+}
+
+1;
+

Added: apps/spensive/lib/Spensive/Model/EmailAttachment.pm
==============================================================================
--- (empty file)
+++ apps/spensive/lib/Spensive/Model/EmailAttachment.pm	Tue Nov 20 12:10:27 2007
@@ -0,0 +1,28 @@
+use strict;
+use warnings;
+
+package Spensive::Model::EmailAttachment;
+use Jifty::DBI::Schema;
+
+use Spensive::Record schema {
+    column content_type => type is 'text';
+    column filename => type is 'text';
+    column content => type is 'blob';
+};
+
+# Your model-specific methods go here.
+
+sub before_create {
+    my $self = shift;
+    my $args = shift;
+    my $mime_obj = delete $args->{'mime_obj'};
+
+
+    $args->{content_type} = $mime_obj->content_type;
+    $args->{filename} =$mime_obj->filename;
+    $args->{content} = $mime_obj->body;
+}
+
+
+1;
+

Modified: apps/spensive/lib/Spensive/Model/Expense.pm
==============================================================================
--- apps/spensive/lib/Spensive/Model/Expense.pm	(original)
+++ apps/spensive/lib/Spensive/Model/Expense.pm	Tue Nov 20 12:10:27 2007
@@ -26,9 +26,22 @@
 
     $args->{'incurred_by'} = $self->current_user->id;
     $args->{'date_incurred'} ||= Jifty::DateTime->now;
+    $self->{'_email_content'} = delete $args->{'email_content'};
     return 1;
 }
 
+sub after_create {
+    my $self = shift;
+      my $insert_return_value_ref = shift;
+
+      return unless $$insert_return_value_ref;    # bail if insert failed
+      $self->load($$insert_return_value_ref);     # load ourselves from db
+    
+if ($self->{'_email_content'}) {
+    my $e = Spensive::Model::Email->new();
+    $e->create( message => $self->{'_email_content'}, expense => $self->id);
+    }
+}
 
 sub autocomplete_title {
     my $self = shift;

Added: apps/spensive/t/00-model-Email.t
==============================================================================
--- (empty file)
+++ apps/spensive/t/00-model-Email.t	Tue Nov 20 12:10:27 2007
@@ -0,0 +1,35 @@
+#!/usr/bin/env perl
+use warnings;
+use strict;
+
+=head1 DESCRIPTION
+
+A basic test harness for the Email model.
+
+=cut
+
+use Jifty::Test tests => 11;
+
+# Make sure we can load the model
+use_ok('Spensive::Model::Email');
+
+# Grab a system user
+my $system_user = Spensive::CurrentUser->superuser;
+ok($system_user, "Found a system user");
+
+# Try testing a create
+
+my $msg = <<'EOF';
+From: jesse at example.com
+To: example at example.com
+Subject: This is a sample message
+
+Testing!
+EOF
+
+my $o = Spensive::Model::Email->new(current_user => $system_user);
+my ($id) = $o->create(message => $msg);
+ok($id, "Email create returned success");
+ok($o->id, "New Email has valid id set");
+is($o->id, $id, "Create returned the right id");
+

Added: apps/spensive/t/00-model-EmailAttachment.t
==============================================================================
--- (empty file)
+++ apps/spensive/t/00-model-EmailAttachment.t	Tue Nov 20 12:10:27 2007
@@ -0,0 +1,49 @@
+#!/usr/bin/env perl
+use warnings;
+use strict;
+
+=head1 DESCRIPTION
+
+A basic test harness for the EmailAttachment model.
+
+=cut
+
+use Jifty::Test tests => 11;
+
+# Make sure we can load the model
+use_ok('Spensive::Model::EmailAttachment');
+
+# Grab a system user
+my $system_user = Spensive::CurrentUser->superuser;
+ok($system_user, "Found a system user");
+
+# Try testing a create
+my $o = Spensive::Model::EmailAttachment->new(current_user => $system_user);
+my ($id) = $o->create();
+ok($id, "EmailAttachment create returned success");
+ok($o->id, "New EmailAttachment has valid id set");
+is($o->id, $id, "Create returned the right id");
+
+# And another
+$o->create();
+ok($o->id, "EmailAttachment create returned another value");
+isnt($o->id, $id, "And it is different from the previous one");
+
+# Searches in general
+my $collection =  Spensive::Model::EmailAttachmentCollection->new(current_user => $system_user);
+$collection->unlimit;
+is($collection->count, 2, "Finds two records");
+
+# Searches in specific
+$collection->limit(column => 'id', value => $o->id);
+is($collection->count, 1, "Finds one record with specific id");
+
+# Delete one of them
+$o->delete;
+$collection->redo_search;
+is($collection->count, 0, "Deleted row is gone");
+
+# And the other one is still there
+$collection->unlimit;
+is($collection->count, 1, "Still one left");
+

Added: apps/spensive/t/mailbox
==============================================================================


More information about the Jifty-commit mailing list