From 0b9a3ff04bb468d94dd33e87ee499138630f639e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sven=20Sch=C3=B6ling?= Date: Tue, 9 Jul 2013 15:12:29 +0200 Subject: [PATCH] with_objects in ParseFilter korrekt weiterreichen --- SL/Controller/Helper/ParseFilter.pm | 25 ++++++++++--------- t/controllers/helpers/parse_filter.t | 36 +++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/SL/Controller/Helper/ParseFilter.pm b/SL/Controller/Helper/ParseFilter.pm index ee5a740d1..8b26e88b6 100644 --- a/SL/Controller/Helper/ParseFilter.pm +++ b/SL/Controller/Helper/ParseFilter.pm @@ -33,13 +33,12 @@ my %methods = ( sub parse_filter { my ($filter, %params) = @_; - my $hint_objects = $params{with_objects} || []; - my $auto_objects = []; + my $objects = $params{with_objects} || []; - my ($flattened, $objects) = flatten($filter, $auto_objects, '', %params); + my ($flattened, $auto_objects) = flatten($filter, '', %params); - if ($params{class}) { - $objects = $hint_objects; + if (!$params{class}) { + _add_uniq($objects, $_) for @$auto_objects; } my $query = _parse_filter($flattened, $objects, %params); @@ -70,19 +69,19 @@ sub _launder_keys { } sub flatten { - my ($filter, $with_objects, $prefix, %params) = @_; + my ($filter, $prefix, %params) = @_; - return (undef, $with_objects) unless 'HASH' eq ref $filter; - $with_objects ||= []; + return (undef, []) unless 'HASH' eq ref $filter; + my $with_objects = []; my @result; while (my ($key, $value) = each %$filter) { next if !defined $value || $value eq ''; # 0 is fine if ('HASH' eq ref $value) { - my ($query, $more_objects) = flatten($value, $with_objects, _prefix($prefix, $key)); + my ($query, $more_objects) = flatten($value, _prefix($prefix, $key)); push @result, @$query if $query; - push @$with_objects, _prefix($prefix, $key), ($more_objects ? @$more_objects : ()); + _add_uniq($with_objects, $_) for _prefix($prefix, $key), @$more_objects; } else { push @result, _prefix($prefix, $key) => $value; } @@ -138,9 +137,9 @@ sub _dispatch_custom_filters { if ($manager->can('filter')) { ($key, $value, my $obj) = $manager->filter($last_token, $value, $obj_prefix); - _add_uniq($with_objects, $obj); + _add_uniq($with_objects, $obj) if $obj; } else { - _add_uniq($with_objects, $obj_path); + _add_uniq($with_objects, $obj_path) if $obj_path; } return ($key, $value); @@ -150,7 +149,7 @@ sub _add_uniq { my ($array, $what) = @_; $array //= []; - $array = [ uniq @$array, listify($what) ]; + @$array = (uniq @$array, listify($what)); } sub _collapse_indirect_filters { diff --git a/t/controllers/helpers/parse_filter.t b/t/controllers/helpers/parse_filter.t index c72c33fce..2588f904c 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 => 23; +use Test::More tests => 27; use Test::Deep; use Data::Dumper; @@ -247,3 +247,37 @@ test { ] ], }, 'object test with prefix but complex value', class => 'SL::DB::Manager::OrderItem'; + +test { + description => 'test' +}, { + query => [ description => 'test' ], + with_objects => [ 'order' ] +}, 'with_objects don\'t get clobbered', with_objects => [ 'order' ]; + +test { + customer => { + description => 'test' + } +}, { + query => [ 'customer.description' => 'test' ], + with_objects => [ 'order', 'customer' ] +}, 'with_objects get extended with auto infered objects', with_objects => [ 'order' ]; + +test { + customer => { + description => 'test' + } +}, { + query => [ 'customer.description' => 'test' ], + with_objects => [ 'order', 'customer' ] +}, 'with_objects get extended with auto infered objects with classes', class => 'SL::DB::Manager::Order', with_objects => [ 'order' ]; + +test { + customer => { + description => 'test' + } +}, { + query => [ 'customer.description' => 'test' ], + with_objects => [ 'customer' ] +}, 'with_objects: no duplicates', with_objects => [ 'customer' ]; -- 2.20.1