From 6750014122fbf31113aed10ead3a9c0d15450cb5 Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Fri, 19 Jul 2024 15:08:09 +0000 Subject: [PATCH] Bug 37389: _build_extended_attributes_relations update Move this method to the new ExtendedAttributes mixin Abstract this method and apply to ILL::Requests --- Koha/ILL/Requests.pm | 49 ++++------------- Koha/Objects/Mixin/ExtendedAttributes.pm | 70 ++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 38 deletions(-) create mode 100644 Koha/Objects/Mixin/ExtendedAttributes.pm diff --git a/Koha/ILL/Requests.pm b/Koha/ILL/Requests.pm index 46fbc9d9ce..f10e6edd6f 100644 --- a/Koha/ILL/Requests.pm +++ b/Koha/ILL/Requests.pm @@ -22,8 +22,9 @@ use Modern::Perl; use Koha::Database; use Koha::ILL::Request; use Koha::ILL::Request::Config; +use Koha::Objects::Mixin::ExtendedAttributes; -use base qw(Koha::Objects); +use base qw(Koha::Objects::Mixin::ExtendedAttributes Koha::Objects); =head1 NAME @@ -106,48 +107,20 @@ sub search_incomplete { } ); } -=head2 Internal methods - - -=head3 _build_extended_attributes_relations - -Method to dynamically add has_many relations for each ILLRequestAttribute type stored in the ILLRequestAttributes table. - -Used in the API to allow for advanced joins. - -Returns a list of relation accessor names. +=head3 extended_attributes_config =cut -sub _build_extended_attributes_relations { - my ( $self, $types ) = @_; - - my $result_source = $self->_resultset->result_source; - for my $type ( @{$types} ) { - $result_source->add_relationship( - "extended_attributes_$type", - "Koha::Schema::Result::Illrequestattribute", - sub { - my $args = shift; - - return { - "$args->{foreign_alias}.illrequest_id" => { -ident => "$args->{self_alias}.illrequest_id" }, - "$args->{foreign_alias}.type" => { '=', $type }, - }; - }, - { - accessor => 'multi', - join_type => 'LEFT', - cascade_copy => 0, - cascade_delete => 0, - is_depends_on => 0 - }, - ); - - } - return map { 'extended_attributes_' . $_ } @{$types}; +sub extended_attributes_config { + return { + 'id_field' => 'illrequest_id', + 'key_field' => 'type', + 'schema_class' => 'Koha::Schema::Result::Illrequestattribute', + }; } +=head2 Internal methods + =head3 _type =cut diff --git a/Koha/Objects/Mixin/ExtendedAttributes.pm b/Koha/Objects/Mixin/ExtendedAttributes.pm new file mode 100644 index 0000000000..f4d4420d47 --- /dev/null +++ b/Koha/Objects/Mixin/ExtendedAttributes.pm @@ -0,0 +1,70 @@ +package Koha::Objects::Mixin::ExtendedAttributes; + +# Copyright 2024 PTFS Europe Ltd +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +=head1 NAME + +Koha::Objects::Mixin::ExtendedAttributes + +=head2 Class methods + +=head3 _build_extended_attributes_relations + +Method to dynamically add has_many relations for Koha classes that support extended_attributes. + +Used in the API to allow for advanced joins. + +Returns a list of relation accessor names. + +=cut + +sub _build_extended_attributes_relations { + my ( $self, $types ) = @_; + + my $ea_config = $self->extended_attributes_config; + + my $result_source = $self->_resultset->result_source; + for my $type ( @{$types} ) { + $result_source->add_relationship( + "extended_attributes_$type", + "$ea_config->{schema_class}", + sub { + my $args = shift; + + return { + "$args->{foreign_alias}.$ea_config->{id_field}" => + { -ident => "$args->{self_alias}.$ea_config->{id_field}" }, + "$args->{foreign_alias}.$ea_config->{key_field}" => { '=', $type }, + }; + }, + { + accessor => 'multi', + join_type => 'LEFT', + cascade_copy => 0, + cascade_delete => 0, + is_depends_on => 0 + }, + ); + + } + return map { 'extended_attributes_' . $_ } @{$types}; +} + +1; \ No newline at end of file -- 2.34.1