View | Details | Raw Unified | Return to bug 26555
Collapse All | Expand All

(-)a/Koha/Object.pm (+43 lines)
Lines 28-33 use Try::Tiny; Link Here
28
use Koha::Database;
28
use Koha::Database;
29
use Koha::Exceptions::Object;
29
use Koha::Exceptions::Object;
30
use Koha::DateUtils;
30
use Koha::DateUtils;
31
use Koha::Object::Message;
31
32
32
=head1 NAME
33
=head1 NAME
33
34
Lines 77-82 sub new { Link Here
77
          $schema->resultset( $class->_type() )->new($attributes);
78
          $schema->resultset( $class->_type() )->new($attributes);
78
    }
79
    }
79
80
81
    $self->{_messages} = [];
82
80
    croak("No _type found! Koha::Object must be subclassed!")
83
    croak("No _type found! Koha::Object must be subclassed!")
81
      unless $class->_type();
84
      unless $class->_type();
82
85
Lines 332-337 sub get_from_storage { Link Here
332
    return $object_class->_new_from_dbic($stored_object);
335
    return $object_class->_new_from_dbic($stored_object);
333
}
336
}
334
337
338
=head3 $object->messages
339
340
    my @messages = @{ $object->messages };
341
342
Returns the (probably non-fatal) messages that were recorded on the object.
343
344
=cut
345
346
sub messages {
347
    my ( $self ) = @_;
348
    return $self->{_messages};
349
}
350
351
=head3 $object->add_message
352
353
    try {
354
        <some action that might fail>
355
    }
356
    catch {
357
        if ( <fatal condition> ) {
358
            Koha::Exception->throw...
359
        }
360
361
        # This is a non fatal error, notify the caller
362
        $self->add_message({ message => $error, type => 'error' });
363
    }
364
    return $self;
365
366
Adds a message.
367
368
=cut
369
370
sub add_message {
371
    my ( $self, $params ) = @_;
372
373
    push @{ $self->{_messages} }, Koha::Object::Message->new($params);
374
375
    return $self;
376
}
377
335
=head3 $object->TO_JSON
378
=head3 $object->TO_JSON
336
379
337
Returns an unblessed representation of the object, suitable for JSON output.
380
Returns an unblessed representation of the object, suitable for JSON output.
(-)a/t/db_dependent/Koha/Object.t (-2 / +22 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 20;
20
use Test::More tests => 21;
21
use Test::Exception;
21
use Test::Exception;
22
use Test::Warn;
22
use Test::Warn;
23
use DateTime;
23
use DateTime;
Lines 867-869 subtest 'set_or_blank' => sub { Link Here
867
867
868
    $schema->storage->txn_rollback;
868
    $schema->storage->txn_rollback;
869
};
869
};
870
- 
870
871
subtest 'messages() and add_message() tests' => sub {
872
873
    plan tests => 6;
874
875
    my $patron = Koha::Patron->new;
876
877
    my @messages = @{ $patron->messages };
878
    is( scalar @messages, 0, 'No messages' );
879
880
    $patron->add_message({ message => "message_1" });
881
    $patron->add_message({ message => "message_2" });
882
883
    @messages = @{ $patron->messages };
884
885
    is( scalar @messages, 2, 'Messages are returned' );
886
    is( ref($messages[0]), 'Koha::Object::Message', 'Right type returned' );
887
    is( ref($messages[1]), 'Koha::Object::Message', 'Right type returned' );
888
    is( $messages[0]->message, 'message_1', 'Right message recorded' );
889
    is( $messages[1]->message, 'message_2', 'Right message recorded' );
890
};

Return to bug 26555