From 303a4d5d9c401254c2cf623a702a9e7676fb639f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sven=20Sch=C3=B6ling?= Date: Wed, 26 Mar 2014 17:02:45 +0100 Subject: [PATCH] =?utf8?q?:multi=20filter=20f=C3=BCr=20ParseFilter?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- SL/Controller/Helper/ParseFilter.pm | 22 ++++++++++++++++---- t/controllers/helpers/parse_filter.t | 31 +++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/SL/Controller/Helper/ParseFilter.pm b/SL/Controller/Helper/ParseFilter.pm index 9b5eb516e..55e65e1b5 100644 --- a/SL/Controller/Helper/ParseFilter.pm +++ b/SL/Controller/Helper/ParseFilter.pm @@ -103,10 +103,24 @@ sub _parse_filter { my ($key, $value) = ($flattened->[$i], $flattened->[$i+1]); my ($type, $op) = $key =~ m{:(.+)::(.+)}; - ($key, $value) = _apply_all($key, $value, qr/\b:(\w+)/, { %filters, %{ $params{filters} || {} } }); - ($key, $value) = _apply_all($key, $value, qr/\b::(\w+)/, { %methods, %{ $params{methods} || {} } }); - ($key, $value) = _dispatch_custom_filters($params{class}, $with_objects, $key, $value) if $params{class}; - ($key, $value) = _apply_value_filters($key, $value, $type, $op); + if ($key =~ s/:multi//) { + my @multi; + my $orig_key = $key; + for my $value (split / /, $value) { + ($key, $value) = _apply_all($key, $value, qr/\b:(\w+)/, { %filters, %{ $params{filters} || {} } }); + ($key, $value) = _apply_all($key, $value, qr/\b::(\w+)/, { %methods, %{ $params{methods} || {} } }); + ($key, $value) = _dispatch_custom_filters($params{class}, $with_objects, $key, $value) if $params{class}; + ($key, $value) = _apply_value_filters($key, $value, $type, $op); + push @multi, $key, $value; + $key = $orig_key; + } + ($key, $value) = (and => \@multi); + } else { + ($key, $value) = _apply_all($key, $value, qr/\b:(\w+)/, { %filters, %{ $params{filters} || {} } }); + ($key, $value) = _apply_all($key, $value, qr/\b::(\w+)/, { %methods, %{ $params{methods} || {} } }); + ($key, $value) = _dispatch_custom_filters($params{class}, $with_objects, $key, $value) if $params{class}; + ($key, $value) = _apply_value_filters($key, $value, $type, $op); + } push @result, $key, $value if defined $key; } diff --git a/t/controllers/helpers/parse_filter.t b/t/controllers/helpers/parse_filter.t index 856ae2e33..21dbb7442 100644 --- a/t/controllers/helpers/parse_filter.t +++ b/t/controllers/helpers/parse_filter.t @@ -1,6 +1,6 @@ use lib 't'; -use Test::More tests => 28; +use Test::More tests => 30; use Test::Deep; use Data::Dumper; @@ -293,3 +293,32 @@ test { } ] }, 'Regression check: prefixing of fallback filtering in relation with custom filters', class => 'SL::DB::Manager::OrderItem'; +test { + 'description:substr:multi::ilike' => 'term1 term2', +}, { + query => [ + and => [ + description => { ilike => '%term1%' }, + description => { ilike => '%term2%' }, + ] + ] +}, 'simple :multi'; + +test { + part => { + 'all:substr:multi::ilike' => 'term1 term2', + }, +}, { + query => [ + and => [ + or => [ + 'part.partnumber' => { ilike => '%term1%' }, + 'part.description' => { ilike => '%term1%' }, + ], + or => [ + 'part.partnumber' => { ilike => '%term2%' }, + 'part.description' => { ilike => '%term2%' }, + ], + ] + ], +}, 'complex :multi with custom dispatch and prefix', class => 'SL::DB::Manager::OrderItem'; -- 2.20.1