| 1 | use warnings; | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | package Jifty::Action::Record::Execute; | ||
| 5 | use base qw/ Jifty::Action::Record /; | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Jifty::Action::Record::Execute - Simple abstract based for record actions | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | use strict; | ||
| 14 | use warnings; | ||
| 15 | |||
| 16 | package MyApp::Action::StartEncabulator; | ||
| 17 | use base qw/ MyApp::Action::ExecuteEncabulator /; | ||
| 18 | |||
| 19 | use Jifty::Param::Schema; | ||
| 20 | use Jifty::Action schema { | ||
| 21 | param cardinal_grammeter_mode => | ||
| 22 | type is 'text', | ||
| 23 | valid_values are qw/ | ||
| 24 | magneto-reluctance | ||
| 25 | capacitive-duractance | ||
| 26 | sinusoidal-depleneration | ||
| 27 | /, | ||
| 28 | is mandatory, | ||
| 29 | ; | ||
| 30 | }; | ||
| 31 | |||
| 32 | sub take_action { | ||
| 33 | my $self = shift; | ||
| 34 | |||
| 35 | my $mode = $self->argument_value('cardinal_grammeter_mode'); | ||
| 36 | $self->record->start($mode); | ||
| 37 | |||
| 38 | $self->result->success('Deluxe Encabulator has started!'); | ||
| 39 | } | ||
| 40 | |||
| 41 | # Later in your templates: | ||
| 42 | my $encabulator = MyApp::Model::Encabulator->new; | ||
| 43 | $encabulator->load($id); | ||
| 44 | |||
| 45 | my $startup = Jifty->web->new_action( | ||
| 46 | class => 'StartEncabulator', | ||
| 47 | record => $encabulator, | ||
| 48 | ); | ||
| 49 | |||
| 50 | Jifty->web->form->start; | ||
| 51 | |||
| 52 | Jifty->web->out( $startup->form_field('cardinal_grammeter_mode') ); | ||
| 53 | |||
| 54 | Jifty->web->form->submit( | ||
| 55 | label => _('Start'), | ||
| 56 | submit => $startup, | ||
| 57 | ); | ||
| 58 | |||
| 59 | Jifty->web->form->end; | ||
| 60 | |||
| 61 | =head1 DESCRIPTION | ||
| 62 | |||
| 63 | This action class is a good generic basis for creating custom action classes. It expects a record object to be associated and is (in this way) very similar to L<Jifty::Action::Record::Delete>. | ||
| 64 | |||
| 65 | You can use L<Jifty::Param::Schema> to add additional form fields to the action and such. | ||
| 66 | |||
| 67 | =head1 METHODS | ||
| 68 | |||
| 69 | =head2 arguments | ||
| 70 | |||
| 71 | This is customized so that it expects the C<record> argument of all L<Jifty::Action::Record> actions, but also allows for overrides using L<Jifty::Param::Schema>. | ||
| 72 | |||
| 73 | =cut | ||
| 74 | |||
| 75 | # XXX TODO Copied from Jifty::Action::Record::Delete | ||
| 76 | sub arguments { | ||
| 77 | my $self = shift; | ||
| 78 | my $arguments = {}; | ||
| 79 | |||
| 80 | # Mark the primary key for use in the constructor and not rendered | ||
| 81 | for my $pk (@{ $self->record->_primary_keys }) { | ||
| 82 | $arguments->{$pk}{'constructor'} = 1; | ||
| 83 | # XXX TODO IS THERE A BETTER WAY TO NOT RENDER AN ITEM IN arguments | ||
| 84 | $arguments->{$pk}{'render_as'} = 'Unrendered'; | ||
| 85 | # primary key fields should always be hidden fields | ||
| 86 | } | ||
| 87 | |||
| 88 | if ( $self->can('PARAMS') ) { | ||
| 89 | use Jifty::Param::Schema; | ||
| 90 | return Jifty::Param::Schema::merge_params( | ||
| 91 | $arguments, ($self->PARAMS || {}) | ||
| 92 | ); | ||
| 93 | } | ||
| 94 | else { | ||
| 95 | return $arguments; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | =head2 take_action | ||
| 100 | |||
| 101 | This overrides the definition in L<Jifty::Action::Record> so that it does absolutely nothing rather than complain. You will probably want to implement your own version that actually does something. | ||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | sub take_action {} | ||
| 106 | |||
| 107 | =head1 SEE ALSO | ||
| 108 | |||
| 109 | L<Jifty::Action>, L<Jifty::Action::Record>, L<Jifty::Record> | ||
| 110 | |||
| 111 | =head1 LICENSE | ||
| 112 | |||
| 113 | Jifty is Copyright 2005-2010 Best Practical Solutions, LLC. | ||
| 114 | Jifty is distributed under the same terms as Perl itself. | ||
| 115 | |||
| 116 | =cut | ||
| 117 | |||
| 118 | 1; |