Bug 18332: Add the Koha::Objects->last method
DBIx::Class does not provide such method, but it can be handy in some cases. Test plan: prove t/db_dependent/Koha/Objects.t should return green Test returned green. Signed-off-by: Marc Véron <veron@veron.ch> Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io> Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
This commit is contained in:
parent
0fcfc561ad
commit
712e67aa7e
|
|
@ -205,6 +205,30 @@ sub next {
|
|||
return $object;
|
||||
}
|
||||
|
||||
=head3 Koha::Objects->last;
|
||||
|
||||
my $object = Koha::Objects->last;
|
||||
|
||||
Returns the last object that is part of this set.
|
||||
Returns undef if there are no object to return.
|
||||
|
||||
=cut
|
||||
|
||||
sub last {
|
||||
my ( $self ) = @_;
|
||||
|
||||
my $count = $self->_resultset->count;
|
||||
return unless $count;
|
||||
|
||||
my $result = $self->_resultset->slice($count - 1, $count)->first;
|
||||
|
||||
my $object = $self->object_class()->_new_from_dbic( $result );
|
||||
|
||||
return $object;
|
||||
}
|
||||
|
||||
|
||||
|
||||
=head3 Koha::Objects->reset();
|
||||
|
||||
Koha::Objects->reset();
|
||||
|
|
@ -335,7 +359,7 @@ Currently count, pager, update and delete are covered.
|
|||
sub AUTOLOAD {
|
||||
my ( $self, @params ) = @_;
|
||||
|
||||
my @known_methods = qw( count pager update delete result_class single );
|
||||
my @known_methods = qw( count pager update delete result_class single slice );
|
||||
my $method = our $AUTOLOAD;
|
||||
$method =~ s/.*:://;
|
||||
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@
|
|||
|
||||
use Modern::Perl;
|
||||
|
||||
use Test::More tests => 11;
|
||||
use Test::More tests => 12;
|
||||
use Test::Warn;
|
||||
|
||||
use Koha::Authority::Types;
|
||||
|
|
@ -127,6 +127,19 @@ subtest 'single' => sub {
|
|||
"Warning is presented if single is used for a result with multiple rows.";
|
||||
};
|
||||
|
||||
subtest 'last' => sub {
|
||||
plan tests => 3;
|
||||
my $builder = t::lib::TestBuilder->new;
|
||||
my $patron_1 = $builder->build( { source => 'Borrower' } );
|
||||
my $patron_2 = $builder->build( { source => 'Borrower' } );
|
||||
my $last_patron = Koha::Patrons->search->last;
|
||||
is( $last_patron->borrowernumber, $patron_2->{borrowernumber}, '->last should return the last inserted patron' );
|
||||
$last_patron = Koha::Patrons->search({ borrowernumber => $patron_1->{borrowernumber} })->last;
|
||||
is( $last_patron->borrowernumber, $patron_1->{borrowernumber}, '->last should work even if there is only 1 result' );
|
||||
$last_patron = Koha::Patrons->search({ surname => 'should_not_exist' })->last;
|
||||
is( $last_patron, undef, '->last should return undef if search does not return any results' );
|
||||
};
|
||||
|
||||
subtest 'Exceptions' => sub {
|
||||
plan tests => 2;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue