[Rt-commit] rt branch 5.0/add-documented-api-for-scrip-stage-data-sharing created. rt-5.0.3-126-g0d4f93a3ba

BPS Git Server git at git.bestpractical.com
Tue Oct 4 01:12:24 UTC 2022


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "rt".

The branch, 5.0/add-documented-api-for-scrip-stage-data-sharing has been created
        at  0d4f93a3ba80aa3a56e4d5ef4d96e0b5938f6592 (commit)

- Log -----------------------------------------------------------------
commit 0d4f93a3ba80aa3a56e4d5ef4d96e0b5938f6592
Author: Brian Conry <bconry at bestpractical.com>
Date:   Mon Oct 3 19:28:31 2022 -0500

    Add RT::Scrip::DataCache
    
    This change adds the DataCache accessor method to RT::Scrip objects for
    use in safely passing information from one phase of scrip processing to
    others, e.g. from Condtion code to a template.
    
    Each Scrip object is created with a new empty hashref for its DataCache.
    This is a transient cache and no data is preserved when the object is
    destroyed.  This cache is not used for any other purpose so that there
    is no concern about conflict with current or future RT Scrip attributes
    or methods.

diff --git a/docs/customizing/scrip_conditions_and_action.pod b/docs/customizing/scrip_conditions_and_action.pod
index 2e7de72111..6c40b861c2 100644
--- a/docs/customizing/scrip_conditions_and_action.pod
+++ b/docs/customizing/scrip_conditions_and_action.pod
@@ -262,6 +262,28 @@ transactions, as is the case with C<SetOwner>. When this action runs,
 the set owner transaction will fire the default On Owner Change Notify
 Owner scrip, if it is enabled.
 
+=head1 Passing data from Conditions to Actions and Templates
+
+Sometimes data is calculated in a condition that needs to be used in an action,
+or a template that it references.
+
+All of these objects are associated with a single instance of an RT::Scrip
+object that is unique to that instance of that scrip on that transaction.
+
+This can be referenced from conditions and actions as:
+
+    $self->ScripObj
+
+It can also be referenced from templates loaded by any of the core RT Actions,
+including C<Create Tickets>, C<Send Forward>, and all of the C<Notify> actions,
+as:
+
+    $ScripObj
+
+The Scrip object has a C<DataCache> method that returns a hashref that can be
+used safely to pass arbitrary data from one phase of Scrip processing to
+another.
+
 =head1 ADDITIONAL INFORMATION
 
 When writing actions and conditions, it's helpful to look at the actions
diff --git a/lib/RT/Scrip.pm b/lib/RT/Scrip.pm
index dd488f6730..6d3e11f004 100644
--- a/lib/RT/Scrip.pm
+++ b/lib/RT/Scrip.pm
@@ -199,6 +199,8 @@ sub Create {
     );
     return ( $id, $msg ) unless $id;
 
+    $self->{_data_cache} = {};
+
     (my $status, $msg) = RT::ObjectScrip->new( $self->CurrentUser )->Add(
         Scrip     => $self,
         Stage     => $args{'Stage'},
@@ -1072,6 +1074,18 @@ Returns the current value of LastUpdated.
 =cut
 
 
+=head2 DataCache
+
+Returns the hashref for the instance datacache, which can be used to share data
+between the Condtion, Action, and Template associated with this Scrip instance
+being applied to a transaction.
+
+=cut
+
+sub DataCache {
+    my $self = shift;
+    return $self->{_data_cache};
+}
 
 sub _CoreAccessible {
     {

commit dcfa8170a5e6e75f20e1fc3f0f1d640f4fe22cbd
Author: Brian Conry <bconry at bestpractical.com>
Date:   Mon Oct 3 18:05:31 2022 -0500

    Pass ScripObj to Action template parsing
    
    This change makes the ScripObj available to tempaltes parsed by the
    RT::Action subclasses CreateTickets, SendEmail, SendForward, and their
    subclasses.
    
    This allows IsApplicable code, whether from CustomIsApplicableCode or
    from Conditions added by plugins/extensions, to store data on the
    ScripObj for use by a template, as it can already be used by
    CustomPrepareCode, CustomCommitCode, or the respective methods on
    Action subclasses.

diff --git a/lib/RT/Action/CreateTickets.pm b/lib/RT/Action/CreateTickets.pm
index 9e1cf5e6bf..22bcf1fac9 100644
--- a/lib/RT/Action/CreateTickets.pm
+++ b/lib/RT/Action/CreateTickets.pm
@@ -308,6 +308,7 @@ sub Prepare {
 
     $self->Parse(
         Content        => $self->TemplateObj->Content,
+        ScripObj       => $self->ScripObj,
         _ActiveContent => $active,
     );
     return 1;
diff --git a/lib/RT/Action/SendEmail.pm b/lib/RT/Action/SendEmail.pm
index 2c54a86598..5eae6dcfe8 100644
--- a/lib/RT/Action/SendEmail.pm
+++ b/lib/RT/Action/SendEmail.pm
@@ -139,6 +139,7 @@ sub Prepare {
     unless ( $self->TemplateObj->MIMEObj ) {
         my ( $result, $message ) = $self->TemplateObj->Parse(
             Argument       => $self->Argument,
+            ScripObj       => $self->ScripObj,
             TicketObj      => $self->TicketObj,
             TransactionObj => $self->TransactionObj
         );
diff --git a/lib/RT/Action/SendForward.pm b/lib/RT/Action/SendForward.pm
index ac5d6d04fd..33296fbe63 100644
--- a/lib/RT/Action/SendForward.pm
+++ b/lib/RT/Action/SendForward.pm
@@ -73,6 +73,7 @@ sub Prepare {
 
     my ( $result, $message ) = $self->TemplateObj->Parse(
         Argument           => $self->Argument,
+        ScripObj           => $self->ScripObj,
         Ticket             => $self->TicketObj,
         Transaction        => $self->ForwardedTransactionObj,
         ForwardTransaction => $self->TransactionObj,

-----------------------------------------------------------------------


hooks/post-receive
-- 
rt


More information about the rt-commit mailing list