[Rt-commit] rt branch 5.0/template-split-headers-and-body created. rt-5.0.5-159-gf0f258cea8

BPS Git Server git at git.bestpractical.com
Fri Feb 9 19:24:07 UTC 2024


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/template-split-headers-and-body has been created
        at  f0f258cea8b8cc34ecdfae7f817b407955f2d8be (commit)

- Log -----------------------------------------------------------------
commit f0f258cea8b8cc34ecdfae7f817b407955f2d8be
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Fri Feb 9 14:16:40 2024 -0500

    Avoid the newline after <textarea> tag to make WWW::Mechanize happy
    
    Like the removed $TODO says: WWW::Mechanize doesn't strip newline following
    <textarea> tag like browsers do.

diff --git a/share/html/Admin/Elements/ModifyTemplate b/share/html/Admin/Elements/ModifyTemplate
index 91beda1393..946ed51190 100644
--- a/share/html/Admin/Elements/ModifyTemplate
+++ b/share/html/Admin/Elements/ModifyTemplate
@@ -85,8 +85,7 @@
     <&|/l&>Headers</&>:
   </div>
   <div class="value col-8">
-    <textarea name="Headers" class="form-control" rows="10" cols="80" wrap="soft">
-<%$Headers||''%></textarea>
+    <textarea name="Headers" class="form-control" rows="10" cols="80" wrap="soft"><%$Headers||''%></textarea>
   </div>
 </div>
 <div class="form-row">
@@ -94,8 +93,7 @@
     <&|/l&>Body</&>:
   </div>
   <div class="value col-8">
-    <textarea name="Body" class="form-control" rows="25" cols="80" wrap="soft">
-<%$Body||''%></textarea>
+    <textarea name="Body" class="form-control" rows="25" cols="80" wrap="soft"><%$Body||''%></textarea>
   </div>
 </div>
 </&>
diff --git a/t/web/template.t b/t/web/template.t
index 098a91c727..f552e3f170 100644
--- a/t/web/template.t
+++ b/t/web/template.t
@@ -64,13 +64,7 @@ is($m->value('Type'), 'Perl', 'now that we have ExecuteCode we can update Type t
   my $headers = $m->value('Headers');
   my $body    = $m->value('Body');
 
-  TODO: {
-
-    local $TODO = "WWW::Mechanize doesn't strip newline following <textarea> tag like browsers do";
-    # this test fails because earlier tests add newlines when using Mech
-    like($headers, qr/^Subject: Resolved/, 'got expected Content');
-
-  }
+  like($headers, qr/^Subject: Resolved/, 'got expected Content');
 
   $m->field(Body => "$body\n\n\n$body");
   $m->submit;
@@ -81,12 +75,6 @@ is($m->value('Type'), 'Perl', 'now that we have ExecuteCode we can update Type t
   $m->form_name('ModifyTemplate');
   $m->submit;
 
-  TODO: {
-
-    local $TODO = "WWW::Mechanize doesn't strip newline following <textarea> tag like browsers do";
-    # this test fails because the template change makes Mech continuously add newlines where browsers dont
-    $m->content_unlike(qr{Template \d+: Content updated});
-
-  }
+  $m->content_unlike(qr{Template \d+: Content updated});
 }
 

commit 5d9e605279dbda769ffff96e23cefc153337efd8
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Fri Feb 9 12:31:32 2024 -0500

    Abstract ProcessTemplateUpdate to reduce duplicate code

diff --git a/lib/RT/Interface/Web.pm b/lib/RT/Interface/Web.pm
index e47fa96069..49ddaf17d1 100644
--- a/lib/RT/Interface/Web.pm
+++ b/lib/RT/Interface/Web.pm
@@ -5887,6 +5887,44 @@ sub PreprocessTransactionSearchQuery {
     return join ' AND ', @limits;
 }
 
+
+=head2 ProcessTemplateUpdate ( TemplateObj => $Template, ARGSRef => \%ARGS );
+
+Returns an array of results messages.
+
+=cut
+
+sub ProcessTemplateUpdate {
+    my %args = (
+        ARGSRef     => undef,
+        TemplateObj => undef,
+        @_
+    );
+
+    if ( !$args{ARGSRef}{Content} && ( $args{ARGSRef}{Headers} || $args{ARGSRef}{Body} ) ) {
+
+        if ( $args{ARGSRef}{Headers} =~ /\S/ ) {
+            $args{ARGSRef}{Headers} =~ s!^\s+!!;
+            $args{ARGSRef}{Headers} =~ s!\s+$!!;
+            $args{ARGSRef}{Content} = join "\n\n", $args{ARGSRef}{Headers}, $args{ARGSRef}{Body} // ();
+        }
+        elsif ( $args{ARGSRef}{Body} =~ /\S/ ) {
+            $args{ARGSRef}{Content} = "\n$args{ARGSRef}{Body}";
+        }
+    }
+
+    my @attribs = qw( Name Description Queue Type Content );
+    my @results = UpdateRecordObject(
+        AttributesRef => \@attribs,
+        Object        => $args{TemplateObj},
+        ARGSRef       => $args{ARGSRef},
+    );
+
+    my ( $ok, $msg ) = $args{TemplateObj}->CompileCheck;
+    push @results, $msg if !$ok;
+    return @results;
+}
+
 package RT::Interface::Web;
 RT::Base->_ImportOverlays();
 
diff --git a/share/html/Admin/Global/Template.html b/share/html/Admin/Global/Template.html
index 3f81b4dda0..e24efc6f45 100644
--- a/share/html/Admin/Global/Template.html
+++ b/share/html/Admin/Global/Template.html
@@ -87,27 +87,7 @@ if (!$Create) {
 }
 
 if ($TemplateObj->Id()) {
-    my @attribs = qw( Name Description Queue Type Content );
-
-    if ( !$ARGS{Content} && ( $ARGS{Headers} || $ARGS{Body} ) ) {
-
-        if ( $ARGS{Headers} =~ /\S/ ) {
-            $ARGS{Headers} =~ s!^\s+!!;
-            $ARGS{Headers} =~ s!\s+$!!;
-            $ARGS{Content} = join "\n\n", $ARGS{Headers}, $ARGS{Body} // ();
-        }
-        elsif ( $ARGS{Body} =~ /\S/ ) {
-            $ARGS{Content} = "\n$ARGS{Body}";
-        }
-    }
-
-    my @aresults = UpdateRecordObject( AttributesRef => \@attribs,
-                                       Object => $TemplateObj,
-                                       ARGSRef => \%ARGS);
-    push @results, @aresults;
-
-    my ($ok, $msg) = $TemplateObj->CompileCheck;
-    push @results, $msg if !$ok;
+    push @results, ProcessTemplateUpdate( TemplateObj => $TemplateObj, ARGSRef => \%ARGS );
 } else {
     $Create = 1;
 }
diff --git a/share/html/Admin/Queues/Template.html b/share/html/Admin/Queues/Template.html
index 4b9c2b3b3e..5478a57c82 100644
--- a/share/html/Admin/Queues/Template.html
+++ b/share/html/Admin/Queues/Template.html
@@ -92,30 +92,7 @@ if ( !$Create ) {
 }
 
 if ( $TemplateObj->Id() ) {
-    $Queue    = $TemplateObj->Queue;
-    $QueueObj = $TemplateObj->QueueObj;
-
-    if ( !$ARGS{Content} && ( $ARGS{Headers} || $ARGS{Body} ) ) {
-
-        if ( $ARGS{Headers} =~ /\S/ ) {
-            $ARGS{Headers} =~ s!^\s+!!;
-            $ARGS{Headers} =~ s!\s+$!!;
-            $ARGS{Content} = join "\n\n", $ARGS{Headers}, $ARGS{Body} // ();
-        }
-        elsif ( $ARGS{Body} =~ /\S/ ) {
-            $ARGS{Content} = "\n$ARGS{Body}";
-        }
-    }
-
-    my @attribs = qw( Name Description Queue Type Content );
-    my @aresults = UpdateRecordObject( AttributesRef => \@attribs,
-                                       Object        => $TemplateObj,
-                                       ARGSRef       => \%ARGS
-                                     );
-    push @results, @aresults;
-
-    my ( $ok, $msg ) = $TemplateObj->CompileCheck;
-    push @results, $msg if !$ok;
+    push @results, ProcessTemplateUpdate( TemplateObj => $TemplateObj, ARGSRef => \%ARGS );
 } else {
     $Create = 1;
     $QueueObj = RT::Queue->new( $session{'CurrentUser'} );

commit bb9f6d15090b310210ef8ca3055d6ab55467cee8
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Fri Feb 9 11:19:01 2024 -0500

    Split Headers and Body from Content to make template updates less confusing
    
    It's a common mistake to forget the separator newline between headers and
    body, especially when a template doesn't have any headers. By splitting
    them, now we automatically add the separator.

diff --git a/share/html/Admin/Elements/ModifyTemplate b/share/html/Admin/Elements/ModifyTemplate
index 53ddc794cc..91beda1393 100644
--- a/share/html/Admin/Elements/ModifyTemplate
+++ b/share/html/Admin/Elements/ModifyTemplate
@@ -82,11 +82,20 @@
 
 <div class="form-row">
   <div class="label col-2">
-    <&|/l&>Content</&>:
+    <&|/l&>Headers</&>:
   </div>
   <div class="value col-8">
-    <textarea name="Content" class="form-control" rows="25" cols="80" wrap="soft">
-<%$Content||''%></textarea>
+    <textarea name="Headers" class="form-control" rows="10" cols="80" wrap="soft">
+<%$Headers||''%></textarea>
+  </div>
+</div>
+<div class="form-row">
+  <div class="label col-2">
+    <&|/l&>Body</&>:
+  </div>
+  <div class="value col-8">
+    <textarea name="Body" class="form-control" rows="25" cols="80" wrap="soft">
+<%$Body||''%></textarea>
   </div>
 </div>
 </&>
@@ -98,6 +107,9 @@ unless ($Type) {
         'Perl' : 'Simple';
 }
 
+if ( $Content && !( $Headers && $Body ) ) {
+    ( $Headers, $Body ) = split( /^\r?\n/m, $Content, 2 );
+}
 </%INIT>
 
 <%ARGS>
@@ -105,4 +117,6 @@ $Name => ''
 $Description => ''
 $Content => ''
 $Type => ''
+$Headers => ''
+$Body => ''
 </%ARGS>
diff --git a/share/html/Admin/Global/Template.html b/share/html/Admin/Global/Template.html
index dedc21cc02..3f81b4dda0 100644
--- a/share/html/Admin/Global/Template.html
+++ b/share/html/Admin/Global/Template.html
@@ -88,6 +88,19 @@ if (!$Create) {
 
 if ($TemplateObj->Id()) {
     my @attribs = qw( Name Description Queue Type Content );
+
+    if ( !$ARGS{Content} && ( $ARGS{Headers} || $ARGS{Body} ) ) {
+
+        if ( $ARGS{Headers} =~ /\S/ ) {
+            $ARGS{Headers} =~ s!^\s+!!;
+            $ARGS{Headers} =~ s!\s+$!!;
+            $ARGS{Content} = join "\n\n", $ARGS{Headers}, $ARGS{Body} // ();
+        }
+        elsif ( $ARGS{Body} =~ /\S/ ) {
+            $ARGS{Content} = "\n$ARGS{Body}";
+        }
+    }
+
     my @aresults = UpdateRecordObject( AttributesRef => \@attribs,
                                        Object => $TemplateObj,
                                        ARGSRef => \%ARGS);
diff --git a/share/html/Admin/Queues/Template.html b/share/html/Admin/Queues/Template.html
index 86b1759634..4b9c2b3b3e 100644
--- a/share/html/Admin/Queues/Template.html
+++ b/share/html/Admin/Queues/Template.html
@@ -95,6 +95,18 @@ if ( $TemplateObj->Id() ) {
     $Queue    = $TemplateObj->Queue;
     $QueueObj = $TemplateObj->QueueObj;
 
+    if ( !$ARGS{Content} && ( $ARGS{Headers} || $ARGS{Body} ) ) {
+
+        if ( $ARGS{Headers} =~ /\S/ ) {
+            $ARGS{Headers} =~ s!^\s+!!;
+            $ARGS{Headers} =~ s!\s+$!!;
+            $ARGS{Content} = join "\n\n", $ARGS{Headers}, $ARGS{Body} // ();
+        }
+        elsif ( $ARGS{Body} =~ /\S/ ) {
+            $ARGS{Content} = "\n$ARGS{Body}";
+        }
+    }
+
     my @attribs = qw( Name Description Queue Type Content );
     my @aresults = UpdateRecordObject( AttributesRef => \@attribs,
                                        Object        => $TemplateObj,
diff --git a/t/web/template.t b/t/web/template.t
index 4bca733c65..098a91c727 100644
--- a/t/web/template.t
+++ b/t/web/template.t
@@ -61,19 +61,18 @@ is($m->value('Type'), 'Perl', 'now that we have ExecuteCode we can update Type t
 
 { # 21152: Each time you save a Template a newline is chopped off the front
   $m->form_name('ModifyTemplate');
-  my $content;
-
+  my $headers = $m->value('Headers');
+  my $body    = $m->value('Body');
 
   TODO: {
 
     local $TODO = "WWW::Mechanize doesn't strip newline following <textarea> tag like browsers do";
     # this test fails because earlier tests add newlines when using Mech
-    like($content = $m->value('Content'), qr/^Subject: Resolved/, 'got expected Content');
+    like($headers, qr/^Subject: Resolved/, 'got expected Content');
 
   }
 
-  $content = "\n\n\n" . $content;
-  $m->field(Content => $content);
+  $m->field(Body => "$body\n\n\n$body");
   $m->submit;
 
   $m->content_like(qr{Template \d+: Content updated});

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


hooks/post-receive
-- 
rt


More information about the rt-commit mailing list