[Rt-commit] rt branch, 4.2/pluggable-cf-types, updated. rt-4.0.1rc1-43-ga0b0b5c

Chia-liang Kao clkao at bestpractical.com
Fri Jun 10 07:15:57 EDT 2011


The branch, 4.2/pluggable-cf-types has been updated
       via  a0b0b5c1e9f85efc7987f67eb1865401f14f5331 (commit)
       via  b7ac078e16e30cb1b44605cdf99d6902affb5d4d (commit)
       via  e582434919cb695fc26b0a95a36561cec04f4605 (commit)
       via  eb6e55669eabb81dc4c03761f491cc6385988571 (commit)
      from  b2ee7b383a53be963d1bb46dca393da7cab0d8b1 (commit)

Summary of changes:
 lib/RT/CustomField/Type.pm |   49 +++++++-----
 lib/RT/Interface/Web.pm    |  184 +++++++++++++++++++-------------------------
 2 files changed, 110 insertions(+), 123 deletions(-)

- Log -----------------------------------------------------------------
commit eb6e55669eabb81dc4c03761f491cc6385988571
Author: Chia-liang Kao <clkao at bestpractical.com>
Date:   Fri Jun 10 17:50:10 2011 +0800

    remove special case for ImageWithCatpion, which is handled by the plugin now

diff --git a/lib/RT/Interface/Web.pm b/lib/RT/Interface/Web.pm
index b03b78a..ffd20e0 100644
--- a/lib/RT/Interface/Web.pm
+++ b/lib/RT/Interface/Web.pm
@@ -2245,7 +2245,7 @@ sub _ProcessObjectCustomFieldUpdates {
             next if defined $args{'ARGS'}->{'Values'} && length $args{'ARGS'}->{'Values'};
 
             # "Empty" values does not mean anything for Image and Binary fields
-            next if $cf_type =~ /^(?:Image|Binary|ImageWithCaption)$/;
+            next if $cf_type =~ /^(?:Image|Binary)$/;
 
             $arg = 'Values';
             $args{'ARGS'}->{'Values'} = undef;

commit e582434919cb695fc26b0a95a36561cec04f4605
Author: Chia-liang Kao <clkao at bestpractical.com>
Date:   Fri Jun 10 18:32:48 2011 +0800

    unroll the core loop in _ProcessObjectCustomFieldUpdates for more deterministic-looking flow.

diff --git a/lib/RT/Interface/Web.pm b/lib/RT/Interface/Web.pm
index ffd20e0..96b7362 100644
--- a/lib/RT/Interface/Web.pm
+++ b/lib/RT/Interface/Web.pm
@@ -2167,14 +2167,15 @@ sub _ProcessObjectCustomFieldUpdates {
     my %args    = @_;
     my $cf      = $args{'CustomField'};
     my $cf_type = $cf->Type || '';
+    my $_args   = $args{'ARGS'};
 
     # Remove blank Values since the magic field will take care of this. Sometimes
     # the browser gives you a blank value which causes CFs to be processed twice
-    if (   defined $args{'ARGS'}->{'Values'}
-        && !length $args{'ARGS'}->{'Values'}
-        && $args{'ARGS'}->{'Values-Magic'} )
+    if (   defined $_args->{'Values'}
+        && !length $_args->{'Values'}
+        && $_args->{'Values-Magic'} )
     {
-        delete $args{'ARGS'}->{'Values'};
+        delete $_args->{'Values'};
     }
 
     my $_arg_values = sub {
@@ -2196,75 +2197,80 @@ sub _ProcessObjectCustomFieldUpdates {
     };
 
     my @results;
+    my $_add_ocfv = sub {
+        my ( $val, $msg ) = $args{'Object'}->AddCustomFieldValue(
+            Field => $cf->id,
+            @_
+        );
+        push @results, $msg;
+        return $val;
+    };
+
+    my $_del_ocfv = sub {
+        my ( $val, $msg ) = $args{'Object'}->DeleteCustomFieldValue(
+            Field => $cf->id,
+            @_
+        );
+        push @results, $msg;
+        return $val;
+    };
 
-    if (my $arg = delete $args{'ARGS'}{'DeleteValues'}) {
+    if (my $arg = delete $_args->{'DeleteValues'}) {
         foreach my $value ($_arg_values->($arg)) {
-            my ( $val, $msg ) = $args{'Object'}->DeleteCustomFieldValue(
-                Field => $cf,
-                Value => $value,
-            );
-            push( @results, $msg );
+            $_del_ocfv->(Value => $value);
         }
     }
-    if (my $arg = delete $args{'ARGS'}{'DeleteValueIds'}) {
+    if (my $arg = delete $_args->{'DeleteValueIds'}) {
         foreach my $value ($_arg_values->($arg)) {
-            my ( $val, $msg ) = $args{'Object'}->DeleteCustomFieldValue(
-                Field   => $cf,
-                ValueId => $value,
-            );
-            push( @results, $msg );
+            $_del_ocfv->(Value => $value);
         }
     }
 
     my $class = $cf->GetTypeClass;
 
     if ($class && $class->can('UpdateArgsFromWebArgs')) {
-        my $args = $class->UpdateArgsFromWebArgs($cf, $args{'ARGS'});
+        my $args = $class->UpdateArgsFromWebArgs($cf, $_args);
         if ($args) {
-            my ( $val, $msg ) = $args{'Object'}->AddCustomFieldValue(
-                Field => $cf->id,
-                %$args,
-            );
-            push( @results, $msg );
+            $_add_ocfv->(%$args);
         }
         return @results;
     }
 
 
-    foreach my $arg ( keys %{ $args{'ARGS'} } ) {
-
-        # skip category argument
-        next if $arg eq 'Category';
-
-        # since http won't pass in a form element with a null value, we need
-        # to fake it
-        if ( $arg eq 'Values-Magic' ) {
-
-            # We don't care about the magic, if there's really a values element;
-            next if defined $args{'ARGS'}->{'Value'}  && length $args{'ARGS'}->{'Value'};
-            next if defined $args{'ARGS'}->{'Values'} && length $args{'ARGS'}->{'Values'};
-
-            # "Empty" values does not mean anything for Image and Binary fields
-            next if $cf_type =~ /^(?:Image|Binary)$/;
-
-            $arg = 'Values';
-            $args{'ARGS'}->{'Values'} = undef;
+    # since http won't pass in a form element with a null value, we need
+    # to fake it
+    if ( $_args->{'Values-Magic'} ) {
+        # We don't care about the magic, if there's really a values element;
+        if ((defined $_args->{'Value'}  && length $_args->{'Value'})  ||
+            (defined $_args->{'Values'} && length $_args->{'Values'}) ||
+        # "Empty" values does not mean anything for Image and Binary fields
+            $cf_type =~ /^(?:Image|Binary)$/) {
         }
+        else {
+            $_args->{'Values'} = undef;
+        }
+    }
 
-        my @values = $_arg_values->($args{'ARGS'}->{$arg});
-        if ( $arg eq 'AddValue' || $arg eq 'Value' ) { # tested by t/web/cf_onqueue.t 
-            foreach my $value (@values) {
-                my ( $val, $msg ) = $args{'Object'}->AddCustomFieldValue(
-                    Field => $cf->id,
-                    Value => $value
-                );
-                push( @results, $msg );
-            }
-        } elsif ( $arg eq 'Upload' ) { # untested
-            my $value_hash = _UploadedFile( $args{'Prefix'} . $arg ) or next;
-            my ( $val, $msg ) = $args{'Object'}->AddCustomFieldValue( %$value_hash, Field => $cf, );
-            push( @results, $msg );
-        } elsif ( $arg eq 'Values' && !$cf->Repeated ) { # tested by t/web/cf_select_one.t
+    if ( exists $_args->{'AddValue'} ) { # tested by t/web/cf_onqueue.t
+        my @values = $_arg_values->($_args->{'AddValue'});
+        foreach my $value (@values) {
+            $_add_ocfv->(Value => $value);
+        }
+    }
+    elsif ( exists $_args->{'Value'} ) { # tested by t/web/cf_onqueue.t
+        my @values = $_arg_values->($_args->{'Value'});
+        foreach my $value (@values) {
+            $_add_ocfv->(Value => $value);
+        }
+    }
+    elsif ( exists $_args->{Upload} ) {
+        if ( my $value_hash = _UploadedFile( $args{'Prefix'} . 'Upload' ) ) {
+            $_add_ocfv->( %$value_hash);
+        }
+    }
+    elsif ( exists $_args->{'Values'} ) {
+        my @values = $_arg_values->($_args->{'Values'});
+        if (!$cf->Repeated) { # tested by t/web/cf_select_one.t
             my $cf_values = $args{'Object'}->CustomFieldValues( $cf->id );
 
             my %values_hash;
@@ -2274,28 +2280,21 @@ sub _ProcessObjectCustomFieldUpdates {
                     next;
                 }
 
-                my ( $val, $msg ) = $args{'Object'}->AddCustomFieldValue(
-                    Field => $cf,
-                    Value => $value
-                );
-                push( @results, $msg );
+                my $val = $_add_ocfv->( Value => $value );
                 $values_hash{$val} = 1 if $val;
             }
 
             # For Date Cfs, @values is empty when there is no changes (no datas in form input)
-            return @results if ( $cf->Type =~ /^Date(?:Time)?$/ && ! @values );
-
-            $cf_values->RedoSearch;
-            while ( my $cf_value = $cf_values->Next ) {
-                next if $values_hash{ $cf_value->id };
+            unless ( $cf->Type =~ /^Date(?:Time)?$/ && ! @values ) {
+                $cf_values->RedoSearch;
+                while ( my $cf_value = $cf_values->Next ) {
+                    next if $values_hash{ $cf_value->id };
 
-                my ( $val, $msg ) = $args{'Object'}->DeleteCustomFieldValue(
-                    Field   => $cf,
-                    ValueId => $cf_value->id
-                );
-                push( @results, $msg );
+                    $_del_ocfv->(ValueId => $cf_value->id);
+                }
             }
-        } elsif ( $arg eq 'Values' ) { # untested
+        }
+        else { # not repeated.  untested
             my $cf_values = $args{'Object'}->CustomFieldValues( $cf->id );
 
             # keep everything up to the point of difference, delete the rest
@@ -2312,21 +2311,18 @@ sub _ProcessObjectCustomFieldUpdates {
 
             # now add/replace extra things, if any
             foreach my $value (@values) {
-                my ( $val, $msg ) = $args{'Object'}->AddCustomFieldValue(
-                    Field => $cf,
-                    Value => $value
-                );
-                push( @results, $msg );
+                $_add_ocfv->( Value => $value );
             }
-        } else {
-            push(
-                @results,
-                loc("User asked for an unknown update type for custom field [_1] for [_2] object #[_3]",
-                    $cf->Name, ref $args{'Object'},
-                    $args{'Object'}->id
-                )
-            );
         }
+    } else {
+
+        push(
+            @results,
+            loc("User asked for an unknown update type for custom field [_1] for [_2] object #[_3]",
+                $cf->Name, ref $args{'Object'},
+                $args{'Object'}->id
+            )
+        );
     }
     return @results;
 }

commit b7ac078e16e30cb1b44605cdf99d6902affb5d4d
Author: Chia-liang Kao <clkao at bestpractical.com>
Date:   Fri Jun 10 18:58:32 2011 +0800

    abstract cf values parsing into ValuesFromWeb

diff --git a/lib/RT/CustomField/Type.pm b/lib/RT/CustomField/Type.pm
index 55d0e44..0b8f1f9 100644
--- a/lib/RT/CustomField/Type.pm
+++ b/lib/RT/CustomField/Type.pm
@@ -33,28 +33,39 @@ sub CreateArgsFromWebArgs {
             return HTML::Mason::Commands::_UploadedFileArgs($web_args->{Upload});
         }
 
-        my $type = $cf->Type;
-
-        my @values = ();
-        if ( ref $web_args->{$arg} eq 'ARRAY' ) {
-            @values = @{ $web_args->{$arg} };
-        } elsif ( $type =~ /text/i ) {
-            @values = ( $web_args->{$arg} );
-        } else {
-            no warnings 'uninitialized';
-            @values = split /\r*\n/, $web_args->{$arg};
-        }
-        @values = grep length, map {
-            s/\r+\n/\n/g;
-            s/^\s+//;
-            s/\s+$//;
-            $_;
-        } grep defined, @values;
-
-        return \@values;
+        return [$self->ValuesFromWeb($cf, $web_args->{$arg})];
+    }
+}
+
+
+=head2 ValuesFromWeb C<$args>
+
+Parse the args passed in from web
+
+=cut
+
+sub ValuesFromWeb {
+    my ($self, $cf, $args) = @_;
+
+    my $type = $cf->Type || '';
+
+    my @values = ();
+    if ( ref $args eq 'ARRAY' ) {
+        @values = @$args;
+    } elsif ( $type =~ /text/i ) {    # Both Text and Wikitext
+        @values = $args;
+    } else {
+        @values = split /\r*\n/, $args if defined $args;
     }
+    return grep length, map {
+        s/\r+\n/\n/g;
+        s/^\s+//;
+        s/\s+$//;
+        $_;
+    } grep defined, @values;
 }
 
+
 sub Limit {
     return;
 }
diff --git a/lib/RT/Interface/Web.pm b/lib/RT/Interface/Web.pm
index 96b7362..b541c0b 100644
--- a/lib/RT/Interface/Web.pm
+++ b/lib/RT/Interface/Web.pm
@@ -2178,22 +2178,10 @@ sub _ProcessObjectCustomFieldUpdates {
         delete $_args->{'Values'};
     }
 
+    my $class = $cf->GetTypeClass;
+
     my $_arg_values = sub {
-        my @values = ();
-        my $args = shift;
-        if ( ref $args eq 'ARRAY' ) {
-            @values = @$args;
-        } elsif ( $cf_type =~ /text/i ) {    # Both Text and Wikitext
-            @values = $args;
-        } else {
-            @values = split /\r*\n/, $args if defined $args;
-        }
-        return grep length, map {
-            s/\r+\n/\n/g;
-            s/^\s+//;
-            s/\s+$//;
-            $_;
-        } grep defined, @values;
+        return $class->ValuesFromWeb( $cf, @_ );
     };
 
     my @results;
@@ -2226,8 +2214,6 @@ sub _ProcessObjectCustomFieldUpdates {
         }
     }
 
-    my $class = $cf->GetTypeClass;
-
     if ($class && $class->can('UpdateArgsFromWebArgs')) {
         my $args = $class->UpdateArgsFromWebArgs($cf, $_args);
         if ($args) {

commit a0b0b5c1e9f85efc7987f67eb1865401f14f5331
Author: Chia-liang Kao <clkao at bestpractical.com>
Date:   Fri Jun 10 19:04:36 2011 +0800

    streamline caller

diff --git a/lib/RT/Interface/Web.pm b/lib/RT/Interface/Web.pm
index b541c0b..9a485e5 100644
--- a/lib/RT/Interface/Web.pm
+++ b/lib/RT/Interface/Web.pm
@@ -2180,10 +2180,6 @@ sub _ProcessObjectCustomFieldUpdates {
 
     my $class = $cf->GetTypeClass;
 
-    my $_arg_values = sub {
-        return $class->ValuesFromWeb( $cf, @_ );
-    };
-
     my @results;
     my $_add_ocfv = sub {
         my ( $val, $msg ) = $args{'Object'}->AddCustomFieldValue(
@@ -2204,12 +2200,12 @@ sub _ProcessObjectCustomFieldUpdates {
     };
 
     if (my $arg = delete $_args->{'DeleteValues'}) {
-        foreach my $value ($_arg_values->($arg)) {
+        foreach my $value ($class->ValuesFromWeb( $cf, $arg )) {
             $_del_ocfv->(Value => $value);
         }
     }
     if (my $arg = delete $_args->{'DeleteValueIds'}) {
-        foreach my $value ($_arg_values->($arg)) {
+        foreach my $value ($class->ValuesFromWeb( $cf, $arg )) {
             $_del_ocfv->(Value => $value);
         }
     }
@@ -2238,14 +2234,12 @@ sub _ProcessObjectCustomFieldUpdates {
     }
 
     if ( exists $_args->{'AddValue'} ) { # tested by t/web/cf_onqueue.t
-        my @values = $_arg_values->($_args->{'AddValue'});
-        foreach my $value (@values) {
+        foreach my $value ($class->ValuesFromWeb( $cf, $_args->{'AddValue'} )) {
             $_add_ocfv->(Value => $value);
         }
     }
     elsif ( exists $_args->{'Value'} ) { # tested by t/web/cf_onqueue.t
-        my @values = $_arg_values->($_args->{'Value'});
-        foreach my $value (@values) {
+        foreach my $value ($class->ValuesFromWeb( $cf, $_args->{'Value'} )) {
             $_add_ocfv->(Value => $value);
         }
     }
@@ -2255,7 +2249,7 @@ sub _ProcessObjectCustomFieldUpdates {
         }
     }
     elsif ( exists $_args->{'Values'} ) {
-        my @values = $_arg_values->($_args->{'Values'});
+        my @values = $class->ValuesFromWeb( $cf, $_args->{'Values'} );
         if (!$cf->Repeated) { # tested by t/web/cf_select_one.t
             my $cf_values = $args{'Object'}->CustomFieldValues( $cf->id );
 

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


More information about the Rt-commit mailing list