Revision 0b9a3ff0
Von Sven Schöling vor mehr als 11 Jahren hinzugefügt
SL/Controller/Helper/ParseFilter.pm | ||
---|---|---|
33 | 33 |
sub parse_filter { |
34 | 34 |
my ($filter, %params) = @_; |
35 | 35 |
|
36 |
my $hint_objects = $params{with_objects} || []; |
|
37 |
my $auto_objects = []; |
|
36 |
my $objects = $params{with_objects} || []; |
|
38 | 37 |
|
39 |
my ($flattened, $objects) = flatten($filter, $auto_objects, '', %params);
|
|
38 |
my ($flattened, $auto_objects) = flatten($filter, '', %params);
|
|
40 | 39 |
|
41 |
if ($params{class}) { |
|
42 |
$objects = $hint_objects;
|
|
40 |
if (!$params{class}) {
|
|
41 |
_add_uniq($objects, $_) for @$auto_objects;
|
|
43 | 42 |
} |
44 | 43 |
|
45 | 44 |
my $query = _parse_filter($flattened, $objects, %params); |
... | ... | |
70 | 69 |
} |
71 | 70 |
|
72 | 71 |
sub flatten { |
73 |
my ($filter, $with_objects, $prefix, %params) = @_;
|
|
72 |
my ($filter, $prefix, %params) = @_; |
|
74 | 73 |
|
75 |
return (undef, $with_objects) unless 'HASH' eq ref $filter;
|
|
76 |
$with_objects ||= [];
|
|
74 |
return (undef, []) unless 'HASH' eq ref $filter;
|
|
75 |
my $with_objects = [];
|
|
77 | 76 |
|
78 | 77 |
my @result; |
79 | 78 |
|
80 | 79 |
while (my ($key, $value) = each %$filter) { |
81 | 80 |
next if !defined $value || $value eq ''; # 0 is fine |
82 | 81 |
if ('HASH' eq ref $value) { |
83 |
my ($query, $more_objects) = flatten($value, $with_objects, _prefix($prefix, $key));
|
|
82 |
my ($query, $more_objects) = flatten($value, _prefix($prefix, $key)); |
|
84 | 83 |
push @result, @$query if $query; |
85 |
push @$with_objects, _prefix($prefix, $key), ($more_objects ? @$more_objects : ());
|
|
84 |
_add_uniq($with_objects, $_) for _prefix($prefix, $key), @$more_objects;
|
|
86 | 85 |
} else { |
87 | 86 |
push @result, _prefix($prefix, $key) => $value; |
88 | 87 |
} |
... | ... | |
138 | 137 |
|
139 | 138 |
if ($manager->can('filter')) { |
140 | 139 |
($key, $value, my $obj) = $manager->filter($last_token, $value, $obj_prefix); |
141 |
_add_uniq($with_objects, $obj); |
|
140 |
_add_uniq($with_objects, $obj) if $obj;
|
|
142 | 141 |
} else { |
143 |
_add_uniq($with_objects, $obj_path); |
|
142 |
_add_uniq($with_objects, $obj_path) if $obj_path;
|
|
144 | 143 |
} |
145 | 144 |
|
146 | 145 |
return ($key, $value); |
... | ... | |
150 | 149 |
my ($array, $what) = @_; |
151 | 150 |
|
152 | 151 |
$array //= []; |
153 |
$array = [ uniq @$array, listify($what) ];
|
|
152 |
@$array = (uniq @$array, listify($what));
|
|
154 | 153 |
} |
155 | 154 |
|
156 | 155 |
sub _collapse_indirect_filters { |
t/controllers/helpers/parse_filter.t | ||
---|---|---|
1 | 1 |
use lib 't'; |
2 | 2 |
|
3 |
use Test::More tests => 23;
|
|
3 |
use Test::More tests => 27;
|
|
4 | 4 |
use Test::Deep; |
5 | 5 |
use Data::Dumper; |
6 | 6 |
|
... | ... | |
247 | 247 |
] |
248 | 248 |
], |
249 | 249 |
}, 'object test with prefix but complex value', class => 'SL::DB::Manager::OrderItem'; |
250 |
|
|
251 |
test { |
|
252 |
description => 'test' |
|
253 |
}, { |
|
254 |
query => [ description => 'test' ], |
|
255 |
with_objects => [ 'order' ] |
|
256 |
}, 'with_objects don\'t get clobbered', with_objects => [ 'order' ]; |
|
257 |
|
|
258 |
test { |
|
259 |
customer => { |
|
260 |
description => 'test' |
|
261 |
} |
|
262 |
}, { |
|
263 |
query => [ 'customer.description' => 'test' ], |
|
264 |
with_objects => [ 'order', 'customer' ] |
|
265 |
}, 'with_objects get extended with auto infered objects', with_objects => [ 'order' ]; |
|
266 |
|
|
267 |
test { |
|
268 |
customer => { |
|
269 |
description => 'test' |
|
270 |
} |
|
271 |
}, { |
|
272 |
query => [ 'customer.description' => 'test' ], |
|
273 |
with_objects => [ 'order', 'customer' ] |
|
274 |
}, 'with_objects get extended with auto infered objects with classes', class => 'SL::DB::Manager::Order', with_objects => [ 'order' ]; |
|
275 |
|
|
276 |
test { |
|
277 |
customer => { |
|
278 |
description => 'test' |
|
279 |
} |
|
280 |
}, { |
|
281 |
query => [ 'customer.description' => 'test' ], |
|
282 |
with_objects => [ 'customer' ] |
|
283 |
}, 'with_objects: no duplicates', with_objects => [ 'customer' ]; |
Auch abrufbar als: Unified diff
with_objects in ParseFilter korrekt weiterreichen