From d562df4af2f9de0e0aa94d03b40b464e10cd8737 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Fri, 21 Nov 2014 11:40:48 -0500 Subject: [PATCH] Bug 13019 [QA Followup] - Allow find() and search() to be called as static methods Signed-off-by: Marcel de Rooy Signed-off-by: Kyle M Hall Signed-off-by: Tomas Cohen Arazi --- Koha/Objects.pm | 13 +++++++++---- t/db_dependent/Borrowers.t | 6 +++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/Koha/Objects.pm b/Koha/Objects.pm index 8c17665910..5d2d6d063c 100644 --- a/Koha/Objects.pm +++ b/Koha/Objects.pm @@ -105,7 +105,7 @@ sub search { } else { - my $class = ref( $self ); + my $class = ref($self) ? ref($self) : $self; my $rs = $self->_resultset()->search($params); return $class->new_from_dbic($rs); @@ -202,10 +202,15 @@ Returns the internal resultset or creates it if undefined sub _resultset { my ($self) = @_; - $self->{_resultset} ||= - Koha::Database->new()->schema()->resultset( $self->type() ); + if ( ref($self) ) { + $self->{_resultset} ||= + Koha::Database->new()->schema()->resultset( $self->type() ); - $self->{_resultset}; + return $self->{_resultset}; + } + else { + return Koha::Database->new()->schema()->resultset( $self->type() ); + } } =head3 type diff --git a/t/db_dependent/Borrowers.t b/t/db_dependent/Borrowers.t index 8d47ce707b..bd75551dfc 100755 --- a/t/db_dependent/Borrowers.t +++ b/t/db_dependent/Borrowers.t @@ -66,13 +66,13 @@ my $b3 = Koha::Borrower->new( ); $b3->store(); -my $b1_new = Koha::Borrowers->new()->find( $b1->borrowernumber() ); +my $b1_new = Koha::Borrowers->find( $b1->borrowernumber() ); is( $b1->surname(), $b1_new->surname(), "Found matching borrower" ); -my @borrowers = Koha::Borrowers->new()->search( { branchcode => $branchcode } ); +my @borrowers = Koha::Borrowers->search( { branchcode => $branchcode } ); is( @borrowers, 3, "Found 3 borrowers with Search" ); -my $borrowers = Koha::Borrowers->new()->search( { branchcode => $branchcode } ); +my $borrowers = Koha::Borrowers->search( { branchcode => $branchcode } ); is( $borrowers->count( { branchcode => $branchcode } ), 3, "Counted 3 borrowers with Count" ); my $b = $borrowers->next(); -- 2.39.5