Jifty::Action::Record::Delete - Automagic delete action


Jifty documentation Contained in the Jifty distribution.

Index


Code Index:

NAME

Top

Jifty::Action::Record::Delete - Automagic delete action

DESCRIPTION

Top

This class is used as the base class for Jifty::Actions that are merely deleting Jifty::Record objects. To use it, subclass it and override the record_class method to return the name of the Jifty::Record subclass that this action should delete.

METHODS

Top

arguments

Overrides the arguments in Jifty::Action::Record method to specify that all of the primary keys must have values when submitted; that is, they are constructors (constructors in Jifty::Manual::Glossary). No other arguments are required.

take_action

Overrides the virtual take_action method on Jifty::Action to delete the row from the database.

report_success

Sets the message in Jifty::Result to default success message, "Deleted". Override this if you want to report some other more user-friendly result.

SEE ALSO

Top

Jifty::Action::Record, Jifty::Record

LICENSE

Top

Jifty is Copyright 2005-2010 Best Practical Solutions, LLC. Jifty is distributed under the same terms as Perl itself.


Jifty documentation Contained in the Jifty distribution.
use warnings;
use strict;

package Jifty::Action::Record::Delete;

use base qw/Jifty::Action::Record/;

sub arguments {
    my $self = shift;
    my $arguments = {};

    # Mark the primary key for use in the constructor and not rendered
    for my $pk (@{ $self->record->_primary_keys }) {
        $arguments->{$pk}{'constructor'} = 1;
        # XXX TODO IS THERE A BETTER WAY TO NOT RENDER AN ITEM IN arguments
        $arguments->{$pk}{'render_as'} = 'Unrendered'; 
        # primary key fields should always be hidden fields
    }
    return $arguments;
}

sub take_action {
    my $self = shift;

    # Setup the event info for later publishing
    my $event_info = $self->_setup_event_before_action();

    # Delete the record and return an error if delete fails
    my ( $val, $msg ) = $self->record->delete;
    $self->result->error($msg || _('Permission denied')) if not $val;

    # Otherwise, we seem to have succeeded, report that
    $self->report_success if not $self->result->failure;

    # Publish the event
    $self->_setup_event_after_action($event_info);

    return 1;
}

sub report_success {
    my $self = shift;
    $self->result->message(_("Deleted"))
}

1;