[Jifty-commit] r5556 - in jifty/trunk: . lib/Jifty/Action/Record t/TestApp/lib/TestApp/Action t/TestApp/lib/TestApp/Model

Jifty commits jifty-commit at lists.jifty.org
Sun Jul 20 23:16:37 EDT 2008


Author: sterling
Date: Sun Jul 20 23:16:36 2008
New Revision: 5556

Added:
   jifty/trunk/lib/Jifty/Action/Record/Do.pm
   jifty/trunk/t/TestApp/lib/TestApp/Action/PassSomething.pm
   jifty/trunk/t/TestApp/lib/TestApp/Model/Something.pm
   jifty/trunk/t/TestApp/t/22-do-action.t
Modified:
   jifty/trunk/   (props changed)

Log:
 r98261 at shanenka-lt-osx:  shanenka | 2008-07-20 20:39:20 -0500
 Added a Do action for help building general "do something to a record" actions.


Added: jifty/trunk/lib/Jifty/Action/Record/Do.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/lib/Jifty/Action/Record/Do.pm	Sun Jul 20 23:16:36 2008
@@ -0,0 +1,116 @@
+use warnings;
+use strict;
+
+package Jifty::Action::Record::Do;
+use base qw/ Jifty::Action::Record /;
+
+use Hash::Merge;
+
+=head1 NAME
+
+Jifty::Action::Record::Do - Simple abstract based for record actions
+
+=head1 SYNOPSIS
+
+  use strict;
+  use warnings;
+
+  package MyApp::Action::StartEncabulator;
+  use base qw/ MyApp::Action::DoEncabulator /;
+
+  use Jifty::Param::Schema;
+  use Jifty::Action schema {
+      param cardinal_grammeter_mode =>
+          type is 'text',
+          valid_values are qw/
+              magneto-reluctance
+              capacitive-duractance
+              sinusoidal-depleneration
+          /,
+          is mandatory,
+          ;
+  }; 
+
+  sub take_action {
+      my $self = shift;
+
+      my $mode = $self->argument_value('cardinal_grammeter_mode');
+      $self->record->start($mode);
+
+      $self->result->success('Deluxe Encabulator has started!');
+  }
+
+  # Later in your templates:
+  my $encabulator = MyApp::Model::Encabulator->new;
+  $encabulator->load($id);
+
+  my $startup = Jifty->web->new_action( 
+      class  => 'StartEncabulator',
+      record => $encabulator,
+  );
+
+  Jifty->web->start;
+
+  Jifty->web->out( $startup->form_field('cardinal_grammeter_mode') );
+
+  Jifty->web->form->submit(
+      label  => _('Start'),
+      submit => $startup,
+  );
+
+  Jifty->web->end;
+
+=head1 DESCRIPTION
+
+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>.
+
+You can use L<Jifty::Param::Schema> to add additional form fields to the action and such.
+
+=head1 METHODS
+
+=head2 arguments
+
+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>.
+
+=cut
+
+# XXX TODO Copied from Jifty::Action::Record::Delete
+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
+    }
+
+    # XXX Should this be moved up into Jifty::Action::Record?
+    return Hash::Merge::merge( 
+        $arguments, 
+        ($self->can('PARAMS') && $self->PARAMS),
+    );
+}
+
+=head2 take_action
+
+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.
+
+=cut
+
+sub take_action {}
+
+=head1 SEE ALSO
+
+L<Jifty::Action>, L<Jifty::Action::Record>, L<Jifty::Record>
+
+=head1 LICENSE
+
+Jifty is Copyright 2005-2006 Best Practical Solutions, LLC.
+Jifty is distributed under the same terms as Perl itself.
+
+=cut
+
+1;

Added: jifty/trunk/t/TestApp/lib/TestApp/Action/PassSomething.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/t/TestApp/lib/TestApp/Action/PassSomething.pm	Sun Jul 20 23:16:36 2008
@@ -0,0 +1,27 @@
+use strict;
+use warnings;
+
+package TestApp::Action::PassSomething;
+use base qw/ Jifty::Action::Record::Do /;
+
+use Test::More ();
+
+use Jifty::Param::Schema;
+use Jifty::Action schema {
+    param 'test1';
+};
+
+sub record_class { 'TestApp::Model::Something' }
+
+sub take_action {
+    my $self = shift;
+
+    Test::More::ok(1, 'taking action');
+    Test::More::is($self->argument_value('test1'), 42,        'test1 is 42');
+    Test::More::is($self->argument_value('test2'), 'Prefect', 'test2 is Prefect');
+
+    Test::More::isa_ok($self->record, 'TestApp::Model::Something');
+    Test::More::is($self->record->test3, 'Dent', 'test3 is Dent');
+}
+
+1

Added: jifty/trunk/t/TestApp/lib/TestApp/Model/Something.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/t/TestApp/lib/TestApp/Model/Something.pm	Sun Jul 20 23:16:36 2008
@@ -0,0 +1,14 @@
+use strict;
+use warnings;
+
+package TestApp::Model::Something;
+use base qw/ TestApp::Record /;
+
+use Jifty::DBI::Schema;
+use Jifty::Record schema {
+    column 'test3' => 
+        type is 'text',
+        ;
+};
+
+1

Added: jifty/trunk/t/TestApp/t/22-do-action.t
==============================================================================
--- (empty file)
+++ jifty/trunk/t/TestApp/t/22-do-action.t	Sun Jul 20 23:16:36 2008
@@ -0,0 +1,30 @@
+#!/usr/bin/env perl
+
+use warnings;
+use strict;
+
+=head1 DESCRIPTION
+
+Try out and make sure the the Do record action extends nicely.
+
+=cut
+
+use lib 't/lib';
+use Jifty::SubTest;
+use Jifty::Test tests => 5;
+
+Jifty::Test->web;
+
+my $model = TestApp::Model::Something->new;
+$model->create( test3 => 'Dent' );
+
+my $action = Jifty->web->new_action(
+    class     => 'PassSomething',
+    record    => $model,
+    arguments => {
+        test1 => 42,
+        test2 => 'Prefect',
+    },
+);
+
+$action->run;


More information about the Jifty-commit mailing list