Bugzilla – Attachment 61625 Details for
Bug 18332
Add the Koha::Objects->last method
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 18332: Add the Koha::Objects->last method
Bug-18332-Add-the-KohaObjects-last-method.patch (text/plain), 4.25 KB, created by
Jonathan Druart
on 2017-03-24 16:45:43 UTC
(
hide
)
Description:
Bug 18332: Add the Koha::Objects->last method
Filename:
MIME Type:
Creator:
Jonathan Druart
Created:
2017-03-24 16:45:43 UTC
Size:
4.25 KB
patch
obsolete
>From 65f387ac60a3bff6267b89826fe0ab7d65b0980f Mon Sep 17 00:00:00 2001 >From: Jonathan Druart <jonathan.druart@bugs.koha-community.org> >Date: Thu, 23 Mar 2017 22:03:19 -0300 >Subject: [PATCH] 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 >--- > C4/Letters.pm | 6 +++--- > Koha/Objects.pm | 26 +++++++++++++++++++++++++- > t/db_dependent/Koha/Objects.t | 15 ++++++++++++++- > 3 files changed, 42 insertions(+), 5 deletions(-) > >diff --git a/C4/Letters.pm b/C4/Letters.pm >index b8aab42..e9e61b5 100644 >--- a/C4/Letters.pm >+++ b/C4/Letters.pm >@@ -1575,9 +1575,9 @@ sub _get_tt_params { > foreach my $key ( @$fk ) { > $search->{$key} = $id->{$key}; > } >- $object = $module->search( $search )->next(); >+ $object = $module->search( $search )->last; > } else { # Foreign key is single column >- $object = $module->search( { $fk => $id } )->next(); >+ $object = $module->search( { $fk => $id } )->last; > } > } else { # using the table's primary key for lookup > $object = $module->find($id); >@@ -1587,7 +1587,7 @@ sub _get_tt_params { > else { # $ref eq 'ARRAY' > my $object; > if ( @{ $tables->{$table} } == 1 ) { # Param is a single key >- $object = $module->search( { $pk => $tables->{$table} } )->next(); >+ $object = $module->search( { $pk => $tables->{$table} } )->last; > } > else { # Params are mutliple foreign keys > croak "Multiple foreign keys (table $table) should be passed using an hashref"; >diff --git a/Koha/Objects.pm b/Koha/Objects.pm >index 86a5789..1a52188 100644 >--- a/Koha/Objects.pm >+++ b/Koha/Objects.pm >@@ -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/.*:://; > >diff --git a/t/db_dependent/Koha/Objects.t b/t/db_dependent/Koha/Objects.t >index 29dea67..f1910da 100644 >--- a/t/db_dependent/Koha/Objects.t >+++ b/t/db_dependent/Koha/Objects.t >@@ -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; > >-- >2.1.4
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 18332
:
61625
|
61626
|
61636
|
61694
|
61751