Bug 25044: Remove the need to define koha_object[s]_class for standard object class names

In Koha::Object[s]->prefetch_whitelist, there is a call to the koha_object[s]_class DBIC resultset class. We should not, otherwise those 2 methods will have to be defined all the time, even when we can guess it easily.

Koha::Item <> Schema::Result::Item => standard
Koha::Acquisition::Order <> Schema::Result::Aqorder => non-standard

sub _get_object_class {
    my ( $type ) = @_;
    return unless $type;

    if( $type->can('koha_object_class') ) {
        return $type->koha_object_class;
    }
    $type =~ s|Schema::Result::||;
    return ${type};
}

Test plan:
 % prove t/db_dependent/Koha/Object.t t/db_dependent/Koha/Objects.t
should return green before and after this patch

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
This commit is contained in:
Jonathan Druart 2020-04-02 16:03:17 +02:00 committed by Martin Renvoize
parent 81c141d41a
commit 65c5407d41
Signed by: martin.renvoize
GPG key ID: 422B469130441A0F
3 changed files with 17 additions and 5 deletions

View file

@ -436,7 +436,7 @@ sub prefetch_whitelist {
my $result_class = $relations->{$key}->{class};
my $obj = $result_class->new;
try {
$whitelist->{$key} = $obj->koha_object_class;
$whitelist->{$key} = Koha::Object::_get_object_class( $obj->result_class );
} catch {
$whitelist->{$key} = undef;
}

View file

@ -796,7 +796,7 @@ subtest 'get_from_storage' => sub {
subtest 'prefetch_whitelist() tests' => sub {
plan tests => 2;
plan tests => 3;
$schema->storage->txn_begin;
@ -811,7 +811,13 @@ subtest 'prefetch_whitelist() tests' => sub {
is(
$prefetch_whitelist->{orders},
'Koha::Acquisition::Order',
'Guessed the object class correctly'
'Guessed the non-standard object class correctly'
);
is(
$prefetch_whitelist->{items},
'Koha::Item',
'Guessed the standard object class correctly'
);
$schema->storage->txn_rollback;

View file

@ -773,7 +773,7 @@ subtest "from_api_mapping() tests" => sub {
subtest 'prefetch_whitelist() tests' => sub {
plan tests => 2;
plan tests => 3;
$schema->storage->txn_begin;
@ -788,7 +788,13 @@ subtest 'prefetch_whitelist() tests' => sub {
is(
$prefetch_whitelist->{orders},
'Koha::Acquisition::Order',
'Guessed the object class correctly'
'Guessed the non-standard object class correctly'
);
is(
$prefetch_whitelist->{items},
'Koha::Item',
'Guessed the standard object class correctly'
);
$schema->storage->txn_rollback;