| 1 | { package Catalyst::Action::DBIC::Transaction; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use base 'Catalyst::Action'; | ||
| 5 | use MRO::Compat; | ||
| 6 | use mro 'c3'; | ||
| 7 | use Sub::Name 'subname'; | ||
| 8 | |||
| 9 | sub execute { | ||
| 10 | my $self = shift; | ||
| 11 | my ( $controller, $c ) = @_; | ||
| 12 | my @other_args = @_; | ||
| 13 | |||
| 14 | my $model_class = $controller->_dbic_transaction_schemas->{$self->name} | ||
| 15 | or die 'No schema class defined for DBIC::Transaction ActionClass'; | ||
| 16 | |||
| 17 | my $wantarray = wantarray; | ||
| 18 | my ($return, @return, $success); | ||
| 19 | |||
| 20 | my $sub = subname 'Catalyst::Action::DBIC::Transaction::execute' => sub { | ||
| 21 | if ($wantarray) { | ||
| 22 | @return = $self->next::method(@other_args); | ||
| 23 | } else { | ||
| 24 | $return = $self->next::method(@other_args); | ||
| 25 | } | ||
| 26 | }; | ||
| 27 | |||
| 28 | $c->model($model_class)->schema->txn_do($sub); | ||
| 29 | |||
| 30 | if ($wantarray) { | ||
| 31 | return @return; | ||
| 32 | } else { | ||
| 33 | return $return; | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | }; | ||
| 38 | 1; | ||
| 39 | |||
| 40 | =head1 NAME | ||
| 41 | |||
| 42 | Catalyst::Action::DBIC::Transaction - Encloses actions into transactions | ||
| 43 | |||
| 44 | =head1 SYNOPSIS | ||
| 45 | |||
| 46 | use base qw(Catalyst::Controller::DBIC::Transaction); | ||
| 47 | sub foo :DBICTransaction('DB') { | ||
| 48 | do $something or die $!; | ||
| 49 | } | ||
| 50 | |||
| 51 | =head1 DESCRIPTION | ||
| 52 | |||
| 53 | THIS MODULE SHOULD BE CONSIDERED DEPRECATED. What is currently | ||
| 54 | understood as a better practice is to enclose the code requiring a | ||
| 55 | transaction into a proper model class. The point is that business | ||
| 56 | logic should not be in the controller. | ||
| 57 | |||
| 58 | This module enables the use of automatic transaction support into | ||
| 59 | Catalyst Actions, it will associate a given action with the | ||
| 60 | appropriate action class and save the DBIC::Schema model class name | ||
| 61 | for later use. | ||
| 62 | |||
| 63 | The action will be executed inside a txn_do, and a die inside that | ||
| 64 | method will cause the transaction to be rolled back, as documented in | ||
| 65 | DBIx::Class::Schema. | ||
| 66 | |||
| 67 | This method will not, on the other hand, handle that exception, since | ||
| 68 | txn_do will rethrow it. This means that this handling is not much | ||
| 69 | intrusive in the action processing flow. | ||
| 70 | |||
| 71 | =head1 AUTHORS | ||
| 72 | |||
| 73 | Daniel Ruoso <daniel@ruoso.com> | ||
| 74 | |||
| 75 | =head1 BUG REPORTS | ||
| 76 | |||
| 77 | Please submit all bugs regarding C<Catalyst::Controller::DBIC::Transaction> to | ||
| 78 | C<bug-catalyst-controller-dbic-transaction@rt.cpan.org> | ||
| 79 | |||
| 80 | =head1 LICENSE | ||
| 81 | |||
| 82 | This library is free software, you can redistribute it and/or modify | ||
| 83 | it under the same terms as Perl itself. | ||
| 84 | |||
| 85 | =cut |