[Rt-commit] r18928 - in rt/3.999/branches/lorzy: lib/RT lib/RT/Lorzy/Package lib/RT/ScripAction share/html/Ticket/Elements t/lorzy

clkao at bestpractical.com clkao at bestpractical.com
Wed Mar 25 09:47:55 EDT 2009


Author: clkao
Date: Wed Mar 25 09:47:54 2009
New Revision: 18928

Modified:
   rt/3.999/branches/lorzy/lib/RT/Lorzy.pm
   rt/3.999/branches/lorzy/lib/RT/Lorzy/Package/RT.pm
   rt/3.999/branches/lorzy/lib/RT/Rule.pm
   rt/3.999/branches/lorzy/lib/RT/ScripAction/SendEmail.pm
   rt/3.999/branches/lorzy/share/html/Ticket/Elements/PreviewScrips
   rt/3.999/branches/lorzy/t/api/rules-preview.t
   rt/3.999/branches/lorzy/t/lorzy/action.t

Log:
- Lorzy rule now has prepare that does hinting.
- prepare and commit shares a context object in lorzy space.


Modified: rt/3.999/branches/lorzy/lib/RT/Lorzy.pm
==============================================================================
--- rt/3.999/branches/lorzy/lib/RT/Lorzy.pm	(original)
+++ rt/3.999/branches/lorzy/lib/RT/Lorzy.pm	Wed Mar 25 09:47:54 2009
@@ -52,23 +52,27 @@
 
     my $condition = $builder->defun(
         ops => [ $tree ],
-        signature => $sigs,
+        signature => { %$sigs },
     );
 
-    my $action = $builder->defun(
-        ops => [ { name => 'RT.ScripAction.Run',
+    $sigs->{context} = Lorzy::FunctionArgument->new( name => 'context', type => 'HASH' );
+
+    my $prepare = $builder->defun(
+        ops => [ { name => 'RT.ScripAction.Prepare',
                 args => {
                     name     => $scrip_action,
+                    context => { name => 'Symbol', args => { symbol => 'context' } },
                     template => $template,
                     ticket => { name => 'Symbol', args => { symbol => 'ticket' } },
                     transaction => { name => 'Symbol', args => { symbol => 'transaction' } },
                     } } ],
         signature => $sigs );
 
-    my $hints = $builder->defun(
-        ops => [ { name => 'RT.ScripAction.Hints',
+    my $action = $builder->defun(
+        ops => [ { name => 'RT.ScripAction.Run',
                 args => {
                     name     => $scrip_action,
+                    context => { name => 'Symbol', args => { symbol => 'context' } },
                     template => $template,
                     ticket => { name => 'Symbol', args => { symbol => 'ticket' } },
                     transaction => { name => 'Symbol', args => { symbol => 'transaction' } },
@@ -77,7 +81,7 @@
 
     RT::Lorzy::RuleFactory->make_factory(
         { condition     => $condition,
-          collect_hints => $hints,
+          prepare       => $prepare,
           action        => $action,
           description   => $description,
           _stage        => 'transaction_create',
@@ -86,7 +90,7 @@
 
 package RT::Lorzy::RuleFactory;
 use base 'Class::Accessor::Fast';
-__PACKAGE__->mk_accessors(qw(description condition action collect_hints _stage));
+__PACKAGE__->mk_accessors(qw(description condition action prepare _stage));
 
 sub make_factory {
     my $class = shift;
@@ -96,6 +100,7 @@
         $self->action( Lorzy::Lambda::Native->new( body => $self->action,
                                                    signature => 
         { ticket => Lorzy::FunctionArgument->new( name => 'ticket', type => 'RT::Model::Ticket' ),
+          context => Lorzy::FunctionArgument->new( name => 'context', type => 'RT::Model::Ticket' ),
           transaction => Lorzy::FunctionArgument->new( name => 'transaction', type => 'RT::Model::Transaction' ) }
 
                                                ) );
@@ -112,37 +117,44 @@
 use base 'RT::Rule';
 use base 'Class::Accessor::Fast';
 
-__PACKAGE__->mk_accessors(qw(factory));
+__PACKAGE__->mk_accessors(qw(factory context _last_scripaction));
+
 sub _init {
     my $self = shift;
+    Carp::cluck if scalar @_ % 2;
     my %args = @_;
     $self->SUPER::_init(%args);
+    $self->context({});
     $self->factory($args{factory});
 }
 
 sub prepare {
-    my ($self, %args) = @_;
-    return RT::Lorzy->evaluate( $self->factory->condition,
-                                ticket => $self->ticket_obj,
-                                transaction => $self->transaction);
+    my ( $self, %args ) = @_;
+    RT::Lorzy->evaluate( $self->factory->condition,
+        ticket      => $self->ticket_obj,
+        transaction => $self->transaction )
+        or return;
+
+    return 1 unless $self->factory->prepare;
+
+    RT::Lorzy->evaluate( $self->factory->prepare,
+        context     => $self->context,
+        ticket      => $self->ticket_obj,
+        transaction => $self->transaction );
+
 }
 
 sub description { $_[0]->factory->description }
 
 sub hints {
-    my ($self) = @_;
-    return unless $self->factory->collect_hints;
-    my $hints = RT::Lorzy->evaluate( $self->factory->collect_hints,
-                                     ticket => $self->ticket_obj,
-                                     transaction => $self->transaction);
-
-    return { description => $self->description,
-             %$hints };
+    my $self = shift;
+    return $self->context->{hints};
 }
 
 sub commit {
     my ($self, %args) = @_;
     return RT::Lorzy->evaluate( $self->factory->action,
+                                context => $self->context,
                                 ticket => $self->ticket_obj,
                                 transaction => $self->transaction);
 }

Modified: rt/3.999/branches/lorzy/lib/RT/Lorzy/Package/RT.pm
==============================================================================
--- rt/3.999/branches/lorzy/lib/RT/Lorzy/Package/RT.pm	(original)
+++ rt/3.999/branches/lorzy/lib/RT/Lorzy/Package/RT.pm	Wed Mar 25 09:47:54 2009
@@ -30,9 +30,10 @@
     },
 );
 
-__PACKAGE__->defun( 'ScripAction.Run',
+__PACKAGE__->defun( 'ScripAction.Prepare',
     signature => {
         'name'     => Lorzy::FunctionArgument->new( name => 'name' ),
+        'context'  => Lorzy::FunctionArgument->new( name => 'context' ),
         'template' => Lorzy::FunctionArgument->new( name => 'template' ),
         'ticket'   => Lorzy::FunctionArgument->new( name => 'ticket', type => 'RT::Model::Ticket' ),
         'transaction' => Lorzy::FunctionArgument->new( name => 'transaction', type => 'RT::Model::Transaction' ),
@@ -43,25 +44,35 @@
                                   ticket_obj => $args->{ticket},
                                   transaction_obj => $args->{transaction}
                               );
-        $rule->run_scrip_action(@{$args}{qw(name template)});
+        my $action = $rule->get_scrip_action(@{$args}{qw(name template)});
+        $action->prepare or return;
+        $args->{context}{hints} = $action->hints;
+        $args->{context}{action} = $action;
     },
 );
 
-__PACKAGE__->defun( 'ScripAction.Hints',
+__PACKAGE__->defun( 'ScripAction.Run',
     signature => {
         'name'     => Lorzy::FunctionArgument->new( name => 'name' ),
+        'context'  => Lorzy::FunctionArgument->new( name => 'context' ),
         'template' => Lorzy::FunctionArgument->new( name => 'template' ),
         'ticket'   => Lorzy::FunctionArgument->new( name => 'ticket', type => 'RT::Model::Ticket' ),
         'transaction' => Lorzy::FunctionArgument->new( name => 'transaction', type => 'RT::Model::Transaction' ),
     },
     native => sub {
         my $args   = shift;
-        my $rule = RT::Rule->new( current_user => $args->{ticket}->current_user,
-                                  ticket_obj => $args->{ticket},
-                                  transaction_obj => $args->{transaction}
-                              );
-        $rule->scrip_action_hints(@{$args}{qw(name template)});
+        my $action = $args->{context}{action};
+        unless ($action) {
+            my $rule = RT::Rule->new( current_user => $args->{ticket}->current_user,
+                                                ticket_obj => $args->{ticket},
+                                                transaction_obj => $args->{transaction}
+                                            );
+            $action = $rule->get_scrip_action(@{$args}{qw(name template)});
+            $action->prepare or return;
+        }
+        $action->commit;
     },
 );
 
+
 1;

Modified: rt/3.999/branches/lorzy/lib/RT/Rule.pm
==============================================================================
--- rt/3.999/branches/lorzy/lib/RT/Rule.pm	(original)
+++ rt/3.999/branches/lorzy/lib/RT/Rule.pm	Wed Mar 25 09:47:54 2009
@@ -54,6 +54,11 @@
 use constant _stage => 'transaction_create';
 use constant _queue => undef;
 
+sub _init {
+    my $self = shift;
+    $self->SUPER::_init(@_);
+}
+
 sub prepare {
     my $self = shift;
     return (0) if $self->_queue && $self->ticket_obj->queue->name ne $self->_queue;
@@ -80,19 +85,12 @@
 
 sub run_scrip_action {
     my ($self, $scrip_action, $template, %args) = @_;
-    my $action = $self->_get_scrip_action($scrip_action, $template, %args);
+    my $action = $self->get_scrip_action($scrip_action, $template, %args);
     $action->prepare or return;
     $action->commit;
 }
 
-sub scrip_action_hints {
-    my ($self, $scrip_action, $template, %args) = @_;
-    my $action = $self->_get_scrip_action($scrip_action, $template, %args);
-    $action->prepare or return;
-    $action->hints();
-}
-
-sub _get_scrip_action {
+sub get_scrip_action {
     my ($self, $scrip_action, $template, %args) = @_;
 
     my $ScripAction = RT::Model::ScripAction->new( current_user => $self->current_user);
@@ -102,7 +100,7 @@
         #    $template->LoadQueueTemplate( Queue => ..., ) || $template->LoadGlobalTemplate(...)
 
         my $t = RT::Model::Template->new( current_user => $self->current_user);
-        $t->load($template) or die;
+        $t->load($template) or Carp::confess "Can't load template '$template'";
         $template = $t;
     }
 

Modified: rt/3.999/branches/lorzy/lib/RT/ScripAction/SendEmail.pm
==============================================================================
--- rt/3.999/branches/lorzy/lib/RT/ScripAction/SendEmail.pm	(original)
+++ rt/3.999/branches/lorzy/lib/RT/ScripAction/SendEmail.pm	Wed Mar 25 09:47:54 2009
@@ -279,6 +279,7 @@
 sub addresses_from_header {
     my $self      = shift;
     my $field     = shift;
+    Carp::cluck unless $self->template_obj->mime_obj;
     my $header    = $self->template_obj->mime_obj->head->get($field);
     my @addresses = Email::Address->parse($header);
 

Modified: rt/3.999/branches/lorzy/share/html/Ticket/Elements/PreviewScrips
==============================================================================
--- rt/3.999/branches/lorzy/share/html/Ticket/Elements/PreviewScrips	(original)
+++ rt/3.999/branches/lorzy/share/html/Ticket/Elements/PreviewScrips	Wed Mar 25 09:47:54 2009
@@ -149,7 +149,7 @@
 for (@{$txn->rules}) {
     my $hints = $_->hints;
     next unless $hints->{class} eq 'SendEmail';
-    $recipients->{$hints->{description}} = $hints->{recipients};
+    $recipients->{$_->description} = $hints->{recipients};
 }
 
 return (recipients => $recipients, emails => [$ticket_obj->squelch_mail_to]);

Modified: rt/3.999/branches/lorzy/t/api/rules-preview.t
==============================================================================
--- rt/3.999/branches/lorzy/t/api/rules-preview.t	(original)
+++ rt/3.999/branches/lorzy/t/api/rules-preview.t	Wed Mar 25 09:47:54 2009
@@ -31,12 +31,12 @@
 for (@{$txn->rules}) {
     my $hints = $_->hints;
     next unless $hints->{class} eq 'SendEmail';
-    $preview->{$hints->{description}} = $hints->{recipients};
+    $preview->{$_->description} = $hints->{recipients};
 }
 
 is_deeply($preview,
           { 'On Correspond Notify Other Recipients' =>
-            { to => [], cc => [], bcc => [] },
+            { to => ['onetime at example.org'], cc => [], bcc => [] },
             'On Correspond Notify requestors and ccs' =>
             { to => [ 'root at localhost' ],
               cc => [ 'moosecc at example.org' ],
@@ -44,3 +44,4 @@
             'On Correspond Notify admin_ccs' =>
             { to => [], cc => [], bcc => [] } }
       );
+

Modified: rt/3.999/branches/lorzy/t/lorzy/action.t
==============================================================================
--- rt/3.999/branches/lorzy/t/lorzy/action.t	(original)
+++ rt/3.999/branches/lorzy/t/lorzy/action.t	Wed Mar 25 09:47:54 2009
@@ -40,6 +40,7 @@
                args => {
                    name => "Autoreply To requestors",
                    template => "Autoreply",
+                   context => { name => 'Symbol', args => { symbol => 'context' } },
                    ticket => { name => 'Symbol', args => { symbol => 'ticket' }},
                    transaction => { name => 'Symbol', args => { symbol => 'transaction' }},
                } } ];
@@ -47,6 +48,7 @@
     ops => $tree,
     signature =>
         { ticket => Lorzy::FunctionArgument->new( name => 'ticket', type => 'RT::Model::Ticket' ),
+          context => Lorzy::FunctionArgument->new( name => 'context', type => 'HASH' ),
           transaction => Lorzy::FunctionArgument->new( name => 'transaction', type => 'RT::Model::Transaction' ) }
 );
 


More information about the Rt-commit mailing list