[Rt-commit] rt branch, 4.2/pluggable-cf-types, created. rt-4.0.0rc6-130-g347c6f5

Chia-liang Kao clkao at bestpractical.com
Wed Mar 9 03:19:31 EST 2011


The branch, 4.2/pluggable-cf-types has been created
        at  347c6f561a3bbb1205e27b03fe764ac2579fac5f (commit)

- Log -----------------------------------------------------------------
commit bce886e21b85e4c204556617f75861cfd419b35f
Author: Chia-liang Kao <clkao at bestpractical.com>
Date:   Wed Mar 9 10:47:37 2011 +0800

    Group all IPAddress* CF canonicalizations.

diff --git a/lib/RT/ObjectCustomFieldValue.pm b/lib/RT/ObjectCustomFieldValue.pm
index 4d6942c..9dde495 100644
--- a/lib/RT/ObjectCustomFieldValue.pm
+++ b/lib/RT/ObjectCustomFieldValue.pm
@@ -66,7 +66,87 @@ use base 'RT::Record';
 sub Table {'ObjectCustomFieldValues'}
 
 
+sub _CanonicalizeForCreate {
+    my ($self, $cf_as_sys, $args) = @_;
 
+    if($cf_as_sys->Type eq 'IPAddress') {
+        if ( $args->{'Content'} ) {
+            $args->{'Content'} = $self->ParseIP( $args->{'Content'} );
+        }
+
+        unless ( defined $args->{'Content'} ) {
+            return
+              wantarray
+              ? ( 0, $self->loc("Content is an invalid IP address") )
+              : 0;
+        }
+    }
+
+    if($cf_as_sys->Type eq 'IPAddressRange') {
+        if ($args->{'Content'}) {
+            ($args->{'Content'}, $args->{'LargeContent'}) = $self->ParseIPRange( $args->{'Content'} );
+        }
+        $args->{'ContentType'} = 'text/plain';
+
+        unless ( defined $args->{'Content'} ) {
+            return
+              wantarray
+              ? ( 0, $self->loc("Content is an invalid IP address range") )
+              : 0;
+        }
+    }
+
+    return wantarray ? (1) : 1;
+}
+
+# XXX: not yet, the ipaddressrange belongs to some deeper hook for CFLimit
+sub _CanonicalizeForSearch {
+    my ($self, $cf, $value, $op) = @_;
+
+    if ( $cf && $cf->Type eq 'IPAddress' ) {
+        my $parsed = RT::ObjectCustomFieldValue->ParseIP($value);
+        if ($parsed) {
+            $value = $parsed;
+        }
+        else {
+            $RT::Logger->warn("$value is not a valid IPAddress");
+        }
+    }
+
+    if ( $cf->Type eq 'IPAddressRange' ) {
+
+        if ( $value =~ /^\s*$RE{net}{CIDR}{IPv4}{-keep}\s*$/o ) {
+
+            # convert incomplete 192.168/24 to 192.168.0.0/24 format
+            $value =
+              join( '.', map $_ || 0, ( split /\./, $1 )[ 0 .. 3 ] ) . "/$2"
+              || $value;
+        }
+
+        my ( $start_ip, $end_ip ) =
+          RT::ObjectCustomFieldValue->ParseIPRange($value);
+        if ( $start_ip && $end_ip ) {
+            if ( $op =~ /^([<>])=?$/ ) {
+                my $is_less = $1 eq '<' ? 1 : 0;
+                if ( $is_less ) {
+                    $value = $start_ip;
+                }
+                else {
+                    $value = $end_ip;
+                }
+            }
+            else {
+                $value = join '-', $start_ip, $end_ip;
+            }
+        }
+        else {
+            $RT::Logger->warn("$value is not a valid IPAddressRange");
+        }
+    }
+
+    return $value;
+
+}
 
 sub Create {
     my $self = shift;
@@ -86,30 +166,12 @@ sub Create {
     my $cf_as_sys = RT::CustomField->new(RT->SystemUser);
     $cf_as_sys->Load($args{'CustomField'});
 
-    if($cf_as_sys->Type eq 'IPAddress') {
-        if ( $args{'Content'} ) {
-            $args{'Content'} = $self->ParseIP( $args{'Content'} );
-        }
-
-        unless ( defined $args{'Content'} ) {
-            return
-              wantarray
-              ? ( 0, $self->loc("Content is an invalid IP address") )
-              : 0;
-        }
-    }
-
-    if($cf_as_sys->Type eq 'IPAddressRange') {
-        if ($args{'Content'}) {
-            ($args{'Content'}, $args{'LargeContent'}) = $self->ParseIPRange( $args{'Content'} );
-        }
-        $args{'ContentType'} = 'text/plain';
-
-        unless ( defined $args{'Content'} ) {
-            return
-              wantarray
-              ? ( 0, $self->loc("Content is an invalid IP address range") )
-              : 0;
+    if ( defined $args{'Content'} ) {
+        my ($ret, $msg) = $self->_CanonicalizeForCreate( $cf_as_sys, \%args );
+        unless ( $ret ) {
+            return wantarray
+                ? ( 0, $msg )
+                : 0;
         }
     }
 
diff --git a/lib/RT/Tickets.pm b/lib/RT/Tickets.pm
index 99b43a8..dd73699 100644
--- a/lib/RT/Tickets.pm
+++ b/lib/RT/Tickets.pm
@@ -1443,45 +1443,8 @@ sub _CustomFieldLimit {
         return %args;
     };
 
-    if ( $cf && $cf->Type eq 'IPAddress' ) {
-        my $parsed = RT::ObjectCustomFieldValue->ParseIP($value);
-        if ($parsed) {
-            $value = $parsed;
-        }
-        else {
-            $RT::Logger->warn("$value is not a valid IPAddress");
-        }
-    }
-
-    if ( $cf && $cf->Type eq 'IPAddressRange' ) {
-
-        if ( $value =~ /^\s*$RE{net}{CIDR}{IPv4}{-keep}\s*$/o ) {
-
-            # convert incomplete 192.168/24 to 192.168.0.0/24 format
-            $value =
-              join( '.', map $_ || 0, ( split /\./, $1 )[ 0 .. 3 ] ) . "/$2"
-              || $value;
-        }
-
-        my ( $start_ip, $end_ip ) =
-          RT::ObjectCustomFieldValue->ParseIPRange($value);
-        if ( $start_ip && $end_ip ) {
-            if ( $op =~ /^([<>])=?$/ ) {
-                my $is_less = $1 eq '<' ? 1 : 0;
-                if ( $is_less ) {
-                    $value = $start_ip;
-                }
-                else {
-                    $value = $end_ip;
-                }
-            }
-            else {
-                $value = join '-', $start_ip, $end_ip;
-            }
-        }
-        else {
-            $RT::Logger->warn("$value is not a valid IPAddressRange");
-        }
+    if ($cf) {
+        $value = RT::ObjectCustomFieldValue->_CanonicalizeForSearch( $cf, $value, $op );
     }
 
     my $single_value = !$cf || !$cfid || $cf->SingleValue;
diff --git a/share/html/Elements/ValidateCustomFields b/share/html/Elements/ValidateCustomFields
index 9963039..41ea7cc 100644
--- a/share/html/Elements/ValidateCustomFields
+++ b/share/html/Elements/ValidateCustomFields
@@ -81,29 +81,14 @@ while ( my $CF = $CustomFields->Next ) {
         grep defined, @values;
     @values = ('') unless @values;
 
+    my $ocfv = RT::ObjectCustomFieldValue->new( $session{'CurrentUser'} ); # for now
     for my $value( @values ) {
         if ($value) {
-            if ( $CF->Type eq 'IPAddress' ) {
-                use Regexp::Common qw(RE_net_IPv4);
-                my $ip = RT::ObjectCustomFieldValue->ParseIP( $value );
-                unless ( $ip ) {
-                    my $msg =
-                      loc( "Input can not be parsed as an IP address" );
-                    $m->notes( ( 'InvalidField-' . $CF->Id ) => $msg );
-                    push @res, $msg;
-                    $valid = 0;
-                }
-            }
-            elsif ( $CF->Type eq 'IPAddressRange' ) {
-                my ( $start_ip, $end_ip ) =
-                  RT::ObjectCustomFieldValue->ParseIPRange($value);
-                unless ( $start_ip && $end_ip ) {
-                    my $msg =
-                      loc( "Input can not be parsed as an IP address range" );
-                    $m->notes( ( 'InvalidField-' . $CF->Id ) => $msg );
-                    push @res, $msg;
-                    $valid = 0;
-                }
+            my ($ret, $msg) = $ocfv->_CanonicalizeForCreate( $CF, { 'Content' => $value });
+            unless ($ret) {
+                $m->notes( ( 'InvalidField-' . $CF->Id ) => $msg );
+                push @res, $msg;
+                $valid = 0;
             }
         }
 

commit c9b817c17718b1c22f4ed2babafc7bf2e20a2bab
Author: Chia-liang Kao <clkao at bestpractical.com>
Date:   Wed Mar 9 10:53:30 2011 +0800

    split IPAddressRange ticketsql magic

diff --git a/lib/RT/Tickets.pm b/lib/RT/Tickets.pm
index dd73699..0229641 100644
--- a/lib/RT/Tickets.pm
+++ b/lib/RT/Tickets.pm
@@ -1402,6 +1402,51 @@ Meta Data:
 use Regexp::Common qw(RE_net_IPv4);
 use Regexp::Common::net::CIDR;
 
+sub _CustomFieldLimit_IPAddressRange {
+    my ($self, $field, $cf, $value, $op, %rest) = @_;
+    my ($start_ip, $end_ip) = split /-/, $value;
+
+    $self->_OpenParen;
+
+    if ( $op !~ /NOT|!=|<>/i ) { # positive equation
+        $self->_CustomFieldLimit(
+            'CF', '<=', $end_ip, %rest,
+            SUBKEY => $rest{'SUBKEY'}. '.Content',
+        );
+        $self->_CustomFieldLimit(
+            'CF', '>=', $start_ip, %rest,
+            SUBKEY          => $rest{'SUBKEY'}. '.LargeContent',
+            ENTRYAGGREGATOR => 'AND',
+        );
+        # as well limit borders so DB optimizers can use better
+        # estimations and scan less rows
+        # have to disable this tweak because of ipv6
+        #            $self->_CustomFieldLimit(
+        #                $field, '>=', '000.000.000.000', %rest,
+        #                SUBKEY          => $rest{'SUBKEY'}. '.Content',
+        #                ENTRYAGGREGATOR => 'AND',
+        #            );
+        #            $self->_CustomFieldLimit(
+        #                $field, '<=', '255.255.255.255', %rest,
+        #                SUBKEY          => $rest{'SUBKEY'}. '.LargeContent',
+        #                ENTRYAGGREGATOR => 'AND',
+        #            );
+    }
+    else { # negative equation
+        $self->_CustomFieldLimit($field, '>', $end_ip, %rest);
+        $self->_CustomFieldLimit(
+            $field, '<', $start_ip, %rest,
+            SUBKEY          => $rest{'SUBKEY'}. '.LargeContent',
+            ENTRYAGGREGATOR => 'OR',
+        );
+        # TODO: as well limit borders so DB optimizers can use better
+        # estimations and scan less rows, but it's harder to do
+        # as we have OR aggregator
+    }
+    $self->_CloseParen;
+
+    return 1;
+}
 
 sub _CustomFieldLimit {
     my ( $self, $_field, $op, $value, %rest ) = @_;
@@ -1475,46 +1520,9 @@ sub _CustomFieldLimit {
         $self->_CloseParen;
     }
     elsif ( $op !~ /^[<>]=?$/ && (  $cf && $cf->Type eq 'IPAddressRange')) {
-    
-        my ($start_ip, $end_ip) = split /-/, $value;
-        
-        $self->_OpenParen;
-        if ( $op !~ /NOT|!=|<>/i ) { # positive equation
-            $self->_CustomFieldLimit(
-                'CF', '<=', $end_ip, %rest,
-                SUBKEY => $rest{'SUBKEY'}. '.Content',
-            );
-            $self->_CustomFieldLimit(
-                'CF', '>=', $start_ip, %rest,
-                SUBKEY          => $rest{'SUBKEY'}. '.LargeContent',
-                ENTRYAGGREGATOR => 'AND',
-            ); 
-            # as well limit borders so DB optimizers can use better
-            # estimations and scan less rows
-# have to disable this tweak because of ipv6
-#            $self->_CustomFieldLimit(
-#                $field, '>=', '000.000.000.000', %rest,
-#                SUBKEY          => $rest{'SUBKEY'}. '.Content',
-#                ENTRYAGGREGATOR => 'AND',
-#            );
-#            $self->_CustomFieldLimit(
-#                $field, '<=', '255.255.255.255', %rest,
-#                SUBKEY          => $rest{'SUBKEY'}. '.LargeContent',
-#                ENTRYAGGREGATOR => 'AND',
-#            );  
-        }       
-        else { # negative equation
-            $self->_CustomFieldLimit($field, '>', $end_ip, %rest);
-            $self->_CustomFieldLimit(
-                $field, '<', $start_ip, %rest,
-                SUBKEY          => $rest{'SUBKEY'}. '.LargeContent',
-                ENTRYAGGREGATOR => 'OR',
-            );  
-            # TODO: as well limit borders so DB optimizers can use better
-            # estimations and scan less rows, but it's harder to do
-            # as we have OR aggregator
-        }
-        $self->_CloseParen;
+
+        my $handled = $self->_CustomFieldLimit_IPAddressRange($field, $cf, $value, $op, %rest);
+
     } 
     elsif ( !$negative_op || $single_value ) {
         $cfkey .= '.'. $self->{'_sql_multiple_cfs_index'}++ if !$single_value && !$range_op;

commit 3fceaa888eb71c59093b333e31c2d29918ffca0f
Author: Chia-liang Kao <clkao at bestpractical.com>
Date:   Wed Mar 9 11:07:45 2011 +0800

    update error messages in tests.

diff --git a/t/customfields/ip.t b/t/customfields/ip.t
index f73e63f..7090a32 100644
--- a/t/customfields/ip.t
+++ b/t/customfields/ip.t
@@ -147,7 +147,7 @@ diag "check that we parse correct IPs only" if $ENV{'TEST_VERBOSE'};
             }
         );
 
-        $agent->content_contains( 'can not be parsed as an IP address',
+        $agent->content_contains( 'invalid IP address',
             'ticket fails to create' );
     }
 
diff --git a/t/customfields/iprange.t b/t/customfields/iprange.t
index 118d23c..9e82ca0 100644
--- a/t/customfields/iprange.t
+++ b/t/customfields/iprange.t
@@ -198,7 +198,7 @@ diag "check that we parse correct IPs only" if $ENV{'TEST_VERBOSE'};
             }
         );
 
-        $agent->content_like( qr/can not be parsed as an IP address range/, 'ticket fails to create' );
+        $agent->content_like( qr/invalid IP address range/, 'ticket fails to create' );
     }
 
 }
diff --git a/t/customfields/iprangev6.t b/t/customfields/iprangev6.t
index 06b59e4..cfb08e3 100644
--- a/t/customfields/iprangev6.t
+++ b/t/customfields/iprangev6.t
@@ -193,7 +193,7 @@ diag "check that we parse correct IPs only" if $ENV{'TEST_VERBOSE'};
             }
         );
 
-        $agent->content_like( qr/can not be parsed as an IP address range/,
+        $agent->content_like( qr/invalid IP address range/,
             'ticket fails to create' );
     }
 
diff --git a/t/customfields/ipv6.t b/t/customfields/ipv6.t
index 5054fb2..d9ef14e 100644
--- a/t/customfields/ipv6.t
+++ b/t/customfields/ipv6.t
@@ -149,7 +149,7 @@ diag "check that we parse correct IPs only" if $ENV{'TEST_VERBOSE'};
             }
         );
 
-        $agent->content_contains( 'can not be parsed as an IP address',
+        $agent->content_contains( 'invalid IP address',
             'ticket fails to create' );
     }
 }

commit dcb02933542fdf08d898bfc125c6c983826c571f
Author: Chia-liang Kao <clkao at bestpractical.com>
Date:   Wed Mar 9 11:11:41 2011 +0800

    split date search magic

diff --git a/lib/RT/Tickets.pm b/lib/RT/Tickets.pm
index 0229641..d635510 100644
--- a/lib/RT/Tickets.pm
+++ b/lib/RT/Tickets.pm
@@ -1403,7 +1403,7 @@ use Regexp::Common qw(RE_net_IPv4);
 use Regexp::Common::net::CIDR;
 
 sub _CustomFieldLimit_IPAddressRange {
-    my ($self, $field, $cf, $value, $op, %rest) = @_;
+    my ($self, $field, $cf, $value, $op, $TicketCFs, %rest) = @_;
     my ($start_ip, $end_ip) = split /-/, $value;
 
     $self->_OpenParen;
@@ -1448,6 +1448,57 @@ sub _CustomFieldLimit_IPAddressRange {
     return 1;
 }
 
+
+sub _CustomFieldLimit_Date {
+    my ($self, $field, $cf, $value, $op, $TicketCFs, %rest) = @_;
+
+    return unless $op eq '=';
+    if ( $value =~ /:/ ) {
+        # there is time speccified.
+        my $date = RT::Date->new( $self->CurrentUser );
+        $date->Set( Format => 'unknown', Value => $value );
+        $self->_SQLLimit(
+            ALIAS    => $TicketCFs,
+            FIELD    => 'Content',
+            OPERATOR => "=",
+            VALUE    => $date->ISO,
+            %rest,
+        );
+    }
+    else {
+        # no time specified, that means we want everything on a
+        # particular day.  in the database, we need to check for >
+        # and < the edges of that day.
+        my $date = RT::Date->new( $self->CurrentUser );
+        $date->Set( Format => 'unknown', Value => $value );
+        $date->SetToMidnight( Timezone => 'server' );
+        my $daystart = $date->ISO;
+        $date->AddDay;
+        my $dayend = $date->ISO;
+
+        $self->_OpenParen;
+
+        $self->_SQLLimit(
+            ALIAS    => $TicketCFs,
+            FIELD    => 'Content',
+            OPERATOR => ">=",
+            VALUE    => $daystart,
+            %rest,
+        );
+
+        $self->_SQLLimit(
+            ALIAS    => $TicketCFs,
+            FIELD    => 'Content',
+            OPERATOR => "<=",
+            VALUE    => $dayend,
+            %rest,
+            ENTRYAGGREGATOR => 'AND',
+        );
+
+        $self->_CloseParen;
+    }
+}
+
 sub _CustomFieldLimit {
     my ( $self, $_field, $op, $value, %rest ) = @_;
 
@@ -1521,7 +1572,7 @@ sub _CustomFieldLimit {
     }
     elsif ( $op !~ /^[<>]=?$/ && (  $cf && $cf->Type eq 'IPAddressRange')) {
 
-        my $handled = $self->_CustomFieldLimit_IPAddressRange($field, $cf, $value, $op, %rest);
+        my $handled = $self->_CustomFieldLimit_IPAddressRange($field, $cf, $value, $op, undef, %rest);
 
     } 
     elsif ( !$negative_op || $single_value ) {
@@ -1553,51 +1604,7 @@ sub _CustomFieldLimit {
 
             # need special treatment for Date
             if ( $cf->Type eq 'DateTime' && $op eq '=' ) {
-
-                if ( $value =~ /:/ ) {
-                    # there is time speccified.
-                    my $date = RT::Date->new( $self->CurrentUser );
-                    $date->Set( Format => 'unknown', Value => $value );
-                    $self->_SQLLimit(
-                        ALIAS    => $TicketCFs,
-                        FIELD    => 'Content',
-                        OPERATOR => "=",
-                        VALUE    => $date->ISO,
-                        %rest,
-                    );
-                }
-                else {
-                # no time specified, that means we want everything on a
-                # particular day.  in the database, we need to check for >
-                # and < the edges of that day.
-                    my $date = RT::Date->new( $self->CurrentUser );
-                    $date->Set( Format => 'unknown', Value => $value );
-                    $date->SetToMidnight( Timezone => 'server' );
-                    my $daystart = $date->ISO;
-                    $date->AddDay;
-                    my $dayend = $date->ISO;
-
-                    $self->_OpenParen;
-
-                    $self->_SQLLimit(
-                        ALIAS    => $TicketCFs,
-                        FIELD    => 'Content',
-                        OPERATOR => ">=",
-                        VALUE    => $daystart,
-                        %rest,
-                    );
-
-                    $self->_SQLLimit(
-                        ALIAS    => $TicketCFs,
-                        FIELD    => 'Content',
-                        OPERATOR => "<=",
-                        VALUE    => $dayend,
-                        %rest,
-                        ENTRYAGGREGATOR => 'AND',
-                    );
-
-                    $self->_CloseParen;
-                }
+                my $handled = $self->_CustomFieldLimit_Date($field, $cf, $value, $op, $TicketCFs, %rest);
             }
             elsif ( $op eq '=' || $op eq '!=' || $op eq '<>' ) {
                 if ( length( Encode::encode_utf8($value) ) < 256 ) {

commit 45cc0b10e35e36e6d90342b4278ff104bace139f
Author: Chia-liang Kao <clkao at bestpractical.com>
Date:   Wed Mar 9 12:42:24 2011 +0800

    Expand date range search using recursion like IPAddressRange.

diff --git a/lib/RT/Tickets.pm b/lib/RT/Tickets.pm
index d635510..8642d7c 100644
--- a/lib/RT/Tickets.pm
+++ b/lib/RT/Tickets.pm
@@ -1478,20 +1478,15 @@ sub _CustomFieldLimit_Date {
 
         $self->_OpenParen;
 
-        $self->_SQLLimit(
-            ALIAS    => $TicketCFs,
-            FIELD    => 'Content',
-            OPERATOR => ">=",
-            VALUE    => $daystart,
-            %rest,
+
+        $self->_CustomFieldLimit(
+            'CF', '>=', $daystart, %rest,
+            SUBKEY => $rest{'SUBKEY'}. '.Content',
         );
 
-        $self->_SQLLimit(
-            ALIAS    => $TicketCFs,
-            FIELD    => 'Content',
-            OPERATOR => "<=",
-            VALUE    => $dayend,
-            %rest,
+        $self->_CustomFieldLimit(
+            'CF', '<=', $dayend, %rest,
+            SUBKEY => $rest{'SUBKEY'}. '.Content',
             ENTRYAGGREGATOR => 'AND',
         );
 

commit e6d027b7aad1d92816bc0a914ef68386489ef902
Author: Chia-liang Kao <clkao at bestpractical.com>
Date:   Wed Mar 9 12:48:08 2011 +0800

    avoid using ticketcfs

diff --git a/lib/RT/Tickets.pm b/lib/RT/Tickets.pm
index 8642d7c..10f664b 100644
--- a/lib/RT/Tickets.pm
+++ b/lib/RT/Tickets.pm
@@ -1457,12 +1457,10 @@ sub _CustomFieldLimit_Date {
         # there is time speccified.
         my $date = RT::Date->new( $self->CurrentUser );
         $date->Set( Format => 'unknown', Value => $value );
-        $self->_SQLLimit(
-            ALIAS    => $TicketCFs,
-            FIELD    => 'Content',
-            OPERATOR => "=",
-            VALUE    => $date->ISO,
-            %rest,
+
+        $self->_CustomFieldLimit(
+            'CF', '=', $date->ISO, %rest,
+            SUBKEY => $rest{'SUBKEY'}. '.Content',
         );
     }
     else {

commit 7a68996e971153bd69aff48ddf3ae06ef2291a6d
Author: Chia-liang Kao <clkao at bestpractical.com>
Date:   Wed Mar 9 12:58:48 2011 +0800

    break the _CustomFieldLimit function into parts and let CFType intercept custom query expansion earlier.

diff --git a/lib/RT/Tickets.pm b/lib/RT/Tickets.pm
index 10f664b..1f07290 100644
--- a/lib/RT/Tickets.pm
+++ b/lib/RT/Tickets.pm
@@ -1404,6 +1404,9 @@ use Regexp::Common::net::CIDR;
 
 sub _CustomFieldLimit_IPAddressRange {
     my ($self, $field, $cf, $value, $op, $TicketCFs, %rest) = @_;
+
+    return if $op =~ /^[<>]=?$/;
+
     my ($start_ip, $end_ip) = split /-/, $value;
 
     $self->_OpenParen;
@@ -1490,6 +1493,7 @@ sub _CustomFieldLimit_Date {
 
         $self->_CloseParen;
     }
+    return 1;
 }
 
 sub _CustomFieldLimit {
@@ -1562,13 +1566,32 @@ sub _CustomFieldLimit {
             ENTRYAGGREGATOR => 'AND',
         ) if $CFs;
         $self->_CloseParen;
+
+        return;
     }
-    elsif ( $op !~ /^[<>]=?$/ && (  $cf && $cf->Type eq 'IPAddressRange')) {
 
-        my $handled = $self->_CustomFieldLimit_IPAddressRange($field, $cf, $value, $op, undef, %rest);
+    my $handled = 0;
+    unless ($cf) {
+        $cf = RT::CustomField->new( $self->CurrentUser );
+        $cf->Load($field);
+    }
 
-    } 
-    elsif ( !$negative_op || $single_value ) {
+    if ($cf && !$column) {
+        # if column is not defined, that means the CF type still has
+        # change to expand the query into actual column clauses
+        if ($cf->Type eq 'IPAddressRange') {
+            $handled = $self->_CustomFieldLimit_IPAddressRange($field, $cf, $value, $op, undef, %rest);
+        }
+
+        # need special treatment for Date
+        if ( $cf->Type eq 'DateTime' ) {
+            $handled = $self->_CustomFieldLimit_Date($field, $cf, $value, $op, undef, %rest);
+        }
+    }
+
+    return if $handled;
+
+    if ( !$negative_op || $single_value ) {
         $cfkey .= '.'. $self->{'_sql_multiple_cfs_index'}++ if !$single_value && !$range_op;
         my ($TicketCFs, $CFs) = $self->_CustomFieldJoin( $cfkey, $cfid, $field );
 
@@ -1592,14 +1615,7 @@ sub _CustomFieldLimit {
             $self->_CloseParen;
         }
         else {
-            my $cf = RT::CustomField->new( $self->CurrentUser );
-            $cf->Load($field);
-
-            # need special treatment for Date
-            if ( $cf->Type eq 'DateTime' && $op eq '=' ) {
-                my $handled = $self->_CustomFieldLimit_Date($field, $cf, $value, $op, $TicketCFs, %rest);
-            }
-            elsif ( $op eq '=' || $op eq '!=' || $op eq '<>' ) {
+            if ( $op eq '=' || $op eq '!=' || $op eq '<>' ) {
                 if ( length( Encode::encode_utf8($value) ) < 256 ) {
                     $self->_SQLLimit(
                         ALIAS    => $TicketCFs,

commit 347c6f561a3bbb1205e27b03fe764ac2579fac5f
Author: Chia-liang Kao <clkao at bestpractical.com>
Date:   Wed Mar 9 13:11:44 2011 +0800

    lift _CustomFieldLimit_TypeExpansion

diff --git a/lib/RT/Tickets.pm b/lib/RT/Tickets.pm
index 1f07290..e9c9e62 100644
--- a/lib/RT/Tickets.pm
+++ b/lib/RT/Tickets.pm
@@ -1403,7 +1403,7 @@ use Regexp::Common qw(RE_net_IPv4);
 use Regexp::Common::net::CIDR;
 
 sub _CustomFieldLimit_IPAddressRange {
-    my ($self, $field, $cf, $value, $op, $TicketCFs, %rest) = @_;
+    my ($self, $field, $value, $op, %rest) = @_;
 
     return if $op =~ /^[<>]=?$/;
 
@@ -1453,7 +1453,7 @@ sub _CustomFieldLimit_IPAddressRange {
 
 
 sub _CustomFieldLimit_Date {
-    my ($self, $field, $cf, $value, $op, $TicketCFs, %rest) = @_;
+    my ($self, $field, $value, $op, %rest) = @_;
 
     return unless $op eq '=';
     if ( $value =~ /:/ ) {
@@ -1496,6 +1496,21 @@ sub _CustomFieldLimit_Date {
     return 1;
 }
 
+# returns true if the custom field type expansion has handled the rest of the query
+sub _CustomFieldLimit_TypeExpansion {
+    my ($self, $field, $cf, $value, $op, undef, %rest) = @_;
+
+    if ($cf->Type eq 'IPAddressRange') {
+        return $self->_CustomFieldLimit_IPAddressRange($field, $value, $op, %rest);
+    }
+
+    if ( $cf->Type eq 'DateTime' ) {
+        return $self->_CustomFieldLimit_Date($field, $value, $op, %rest);
+    }
+
+    return;
+}
+
 sub _CustomFieldLimit {
     my ( $self, $_field, $op, $value, %rest ) = @_;
 
@@ -1570,7 +1585,6 @@ sub _CustomFieldLimit {
         return;
     }
 
-    my $handled = 0;
     unless ($cf) {
         $cf = RT::CustomField->new( $self->CurrentUser );
         $cf->Load($field);
@@ -1579,18 +1593,10 @@ sub _CustomFieldLimit {
     if ($cf && !$column) {
         # if column is not defined, that means the CF type still has
         # change to expand the query into actual column clauses
-        if ($cf->Type eq 'IPAddressRange') {
-            $handled = $self->_CustomFieldLimit_IPAddressRange($field, $cf, $value, $op, undef, %rest);
-        }
-
-        # need special treatment for Date
-        if ( $cf->Type eq 'DateTime' ) {
-            $handled = $self->_CustomFieldLimit_Date($field, $cf, $value, $op, undef, %rest);
-        }
+        return if
+            $self->_CustomFieldLimit_TypeExpansion($field, $cf, $value, $op, undef, %rest);
     }
 
-    return if $handled;
-
     if ( !$negative_op || $single_value ) {
         $cfkey .= '.'. $self->{'_sql_multiple_cfs_index'}++ if !$single_value && !$range_op;
         my ($TicketCFs, $CFs) = $self->_CustomFieldJoin( $cfkey, $cfid, $field );

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


More information about the Rt-commit mailing list