Browse Source

Bug 24321: Make dbic_merge_sorting accept a result set as parameter

This patch makes dbic_merge_sorting accept a result set as parameter and
solves a FIXME in _build_order_atom.

To test:
1. Apply this patch
2. Run:
   $ kshell
  k$ prove t/Koha/REST/Plugin/Query.t
=> SUCCESS: Tests pass!
3. Sign off :-D

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
20.05.x
Tomás Cohen Arazi 4 years ago
committed by Martin Renvoize
parent
commit
451e7dacd2
Signed by: martin.renvoize GPG Key ID: 422B469130441A0F
  1. 18
      Koha/REST/Plugin/Query.pm
  2. 26
      t/Koha/REST/Plugin/Query.t

18
Koha/REST/Plugin/Query.pm

@ -81,17 +81,17 @@ Generates the DBIC order_by attributes based on I<$params>, and merges into I<$a
'dbic_merge_sorting' => sub {
my ( $c, $args ) = @_;
my $attributes = $args->{attributes};
my $to_model = $args->{to_model};
my $result_set = $args->{result_set};
if ( defined $args->{params}->{_order_by} ) {
my $order_by = $args->{params}->{_order_by};
if ( reftype($order_by) and reftype($order_by) eq 'ARRAY' ) {
my @order_by = map { _build_order_atom( $_, $to_model) }
my @order_by = map { _build_order_atom({ string => $_, result_set => $result_set }) }
@{ $args->{params}->{_order_by} };
$attributes->{order_by} = \@order_by;
}
else {
$attributes->{order_by} = _build_order_atom( $order_by, $to_model );
$attributes->{order_by} = _build_order_atom({ string => $order_by, result_set => $result_set });
}
}
@ -173,15 +173,15 @@ according to the following rules:
=cut
sub _build_order_atom {
my $string = shift;
my $to_model = shift;
my ( $args ) = @_;
my $string = $args->{string};
my $result_set = $args->{result_set};
# FIXME: This should be done differently once 23893 is pushed
# and we have access to the to_model_mapping hash
my $param = $string;
$param =~ s/^(\+|\-|\s)//;
$param = (keys %{$to_model->({ $param => 1 })})[0]
if $to_model;
if ( $result_set ) {
$param = (keys %{$result_set->attributes_from_api({ $param => 1 })})[0];
}
if ( $string =~ m/^\+/ or
$string =~ m/^\s/ ) {

26
t/Koha/REST/Plugin/Query.t

@ -21,6 +21,8 @@ use Modern::Perl;
use Mojolicious::Lite;
use Try::Tiny;
use Koha::Cities;
app->log->level('error');
plugin 'Koha::REST::Plugin::Query';
@ -92,14 +94,15 @@ get '/dbic_merge_sorting_single' => sub {
$c->render( json => $attributes, status => 200 );
};
get '/dbic_merge_sorting_to_model' => sub {
get '/dbic_merge_sorting_result_set' => sub {
my $c = shift;
my $attributes = { a => 'a', b => 'b' };
my $result_set = Koha::Cities->new;
$attributes = $c->dbic_merge_sorting(
{
attributes => $attributes,
params => { _match => 'exact', _order_by => [ 'uno', '-dos', '+tres', ' cuatro' ] },
to_model => \&to_model
params => { _match => 'exact', _order_by => [ 'name', '-postal_code', '+country', ' state' ] },
result_set => $result_set
}
);
$c->render( json => $attributes, status => 200 );
@ -122,13 +125,6 @@ get '/build_query' => sub {
};
};
sub to_model {
my ($args) = @_;
$args->{three} = delete $args->{tres}
if exists $args->{tres};
return $args;
}
# The tests
use Test::More tests => 3;
@ -178,14 +174,14 @@ subtest 'dbic_merge_sorting() tests' => sub {
]
);
$t->get_ok('/dbic_merge_sorting_to_model')->status_is(200)
$t->get_ok('/dbic_merge_sorting_result_set')->status_is(200)
->json_is( '/a' => 'a', 'Existing values are kept (a)' )
->json_is( '/b' => 'b', 'Existing values are kept (b)' )->json_is(
'/order_by' => [
'uno',
{ -desc => 'dos' },
{ -asc => 'three' },
{ -asc => 'cuatro' }
'city_name',
{ -desc => 'city_zipcode' },
{ -asc => 'city_country' },
{ -asc => 'city_state' }
]
);

Loading…
Cancel
Save