Bug 38273: Make Koha::Object->discard_changes chainable

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
This commit is contained in:
Jonathan Druart 2024-10-28 11:49:13 +01:00 committed by Katrin Fischer
parent e8c8b68672
commit bca1c65bb4
No known key found for this signature in database
GPG Key ID: 0EF6E2C03357A834
2 changed files with 25 additions and 2 deletions

View File

@ -218,6 +218,18 @@ sub store {
}
}
=head3 discard_changes
Refetch the row from the DB
=cut
sub discard_changes {
my ($self) = @_;
my $object_class = Koha::Object::_get_object_class( $self->_result->result_class );
return $object_class->_new_from_dbic( $self->_result->discard_changes );
}
=head3 $object->update();
A shortcut for set + store in one call.
@ -1020,7 +1032,7 @@ sub AUTOLOAD {
return $accessor->( $self, @_ );
}
my @known_methods = qw( is_changed id in_storage get_column discard_changes make_column_dirty );
my @known_methods = qw( is_changed id in_storage get_column make_column_dirty );
Koha::Exceptions::Object::MethodNotCoveredByTests->throw(
error => sprintf("The method %s->%s is not covered by tests!", ref($self), $method),

View File

@ -153,7 +153,7 @@ subtest 'get_column' => sub {
};
subtest 'discard_changes' => sub {
plan tests => 1;
plan tests => 3;
$schema->storage->txn_begin;
@ -166,6 +166,17 @@ subtest 'discard_changes' => sub {
dt_from_string->truncate( to => 'day' ),
'discard_changes should refresh the object'
);
my $cardnumber = $patron->cardnumber;
my $categorycode = $patron->categorycode;
my $branchcode = $patron->branchcode;
$patron->delete;
$patron =
Koha::Patron->new( { cardnumber => $cardnumber, categorycode => $categorycode, branchcode => $branchcode } )
->store->discard_changes;
is( ref($patron), 'Koha::Patron', 'discard_changes should return a Koha::Object object' );
isnt( $patron->updated_on, undef, 'discard_changes should have fetched the row from the DB' );
$schema->storage->txn_rollback;
};