From 4a8105c87955197451c16dc99e004290c5823e46 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 31 Dec 2019 09:48:54 -0300 Subject: [PATCH] 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 Signed-off-by: Kyle M Hall --- Koha/REST/Plugin/Query.pm | 18 +++++++++--------- t/Koha/REST/Plugin/Query.t | 26 +++++++++++--------------- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/Koha/REST/Plugin/Query.pm b/Koha/REST/Plugin/Query.pm index 14b3f5997e..f1402d95ba 100644 --- a/Koha/REST/Plugin/Query.pm +++ b/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/ ) { diff --git a/t/Koha/REST/Plugin/Query.t b/t/Koha/REST/Plugin/Query.t index 2fc027c149..f920af6705 100644 --- a/t/Koha/REST/Plugin/Query.t +++ b/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' } ] ); -- 2.21.0 (Apple Git-122.2)