CatalystX-CRUD
view release on metacpan or search on metacpan
lib/CatalystX/CRUD/Controller.pm view on Meta::CPAN
Attribute: chained to fetch(), expecting no arguments.
Creates an object with form_to_object(), then follows the precommit(),
save_obj() and postcommit() logic.
See the save_obj(), precommit() and postcommit() hook methods for
ways to affect the behaviour of save().
The special param() value C<_delete> is checked to support POST requests
to /save. If found, save() will detach() to rm().
save() returns 0 on any error, and returns 1 on success.
=cut
sub save : PathPart Chained('fetch') Args(0) {
my ( $self, $c ) = @_;
$self->_check_idempotent($c);
if ($c->request->params->{'_delete'}
or ( exists $c->request->params->{'x-tunneled-method'}
and $c->request->params->{'x-tunneled-method'} eq 'DELETE' )
)
{
$c->action->name('rm'); # so we can test against it in postcommit()
$self->rm($c);
return;
}
return if $self->has_errors($c);
unless ( $self->can_write($c) ) {
$self->throw_error('Permission denied');
return;
}
# get a valid object
my $obj = $self->form_to_object($c);
if ( !$obj ) {
$c->log->debug("form_to_object() returned false") if $c->debug;
return 0;
}
# write our changes
unless ( $self->precommit( $c, $obj ) ) {
$c->stash->{template} ||= $self->default_template;
return 0;
}
$self->save_obj( $c, $obj );
$self->postcommit( $c, $obj );
1;
}
=head2 update
Alias for save(), just for consistency with the U in CRUD.
=cut
sub update : PathPart Chained('fetch') Args(0) {
my ( $self, $c ) = @_;
$self->save($c);
}
=head2 rm
Attribute: chained to fetch(), expecting no arguments.
Checks the can_write() and has_errors() methods before proceeeding.
Calls the delete() method on the C<object>.
=cut
sub rm : PathPart Chained('fetch') Args(0) {
my ( $self, $c ) = @_;
$self->_check_idempotent($c);
return if $self->has_errors($c);
unless ( $self->can_write($c) ) {
$self->throw_error('Permission denied');
return;
}
my $o = $c->stash->{object};
unless ( $self->precommit( $c, $o ) ) {
return 0;
}
if ( $self->model_adapter ) {
$self->model_adapter->delete( $c, $o );
}
else {
$o->delete;
}
$self->postcommit( $c, $o );
}
=head2 delete
Wrapper for rm(), just for consistency with the D in CRUD.
=cut
sub delete : PathPart Chained('fetch') Args(0) {
my ( $self, $c ) = @_;
$self->rm($c);
}
=head2 list
Attribute: Local
Display all the objects represented by model_name().
The same as calling search() with no params().
See do_search().
=cut
sub list : Local {
my ( $self, $c, @arg ) = @_;
( run in 1.884 second using v1.01-cache-2.11-cpan-39bf76dae61 )