[Jifty-commit] r3915 - in jifty/trunk: lib/Jifty t

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Sun Aug 19 09:39:37 EDT 2007


Author: sterling
Date: Sun Aug 19 09:39:34 2007
New Revision: 3915

Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/lib/Jifty/Web.pm
   jifty/trunk/t/01-test-web.t

Log:
 r8812 at riddle:  andrew | 2007-08-19 08:38:43 -0500
 Added a new helper, new_record_action(), that wraps new_action() with additional help creating Create, Update, Delete, and Search actions for models.


Modified: jifty/trunk/lib/Jifty/Web.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Web.pm	(original)
+++ jifty/trunk/lib/Jifty/Web.pm	Sun Aug 19 09:39:34 2007
@@ -9,13 +9,11 @@
 
 =cut
 
-
-
-
 use CGI::Cookie;
 use XML::Writer;
 use CSS::Squish;
 use Digest::MD5 qw(md5_hex);
+use Scalar::Util qw(blessed);
 use Carp qw(carp);
 use base qw/Class::Accessor::Fast Class::Data::Inheritable Jifty::Object/;
 
@@ -566,6 +564,50 @@
     );
 }
 
+=head3 new_record_action model => MODELCLASS, record_action => RECORD_ACTION, PARAMHASH
+
+This takes all the same arguments as L</new_action>, except that you specify the C<model> and C<record_action> rather than the C<class>. This helps you create a record action.
+
+You must give the C<model> argument, which may either be an instance of the model or the class name. 
+
+The C<record_action> is optional and defaults to 'Update' if not given. It can be set to 'Create', 'Update', 'Delete', or 'Search'.
+
+For example:
+
+  my $record = MyApp::Model::Employee->new;
+  $record->load(14);
+
+  my $action = Jifty->web->new_record_action(
+      model         => $record,
+      record_action => 'Delete',
+      record        => $record,
+  );
+
+  # $action is now an instance of MyApp::Model::DeleteEmployee
+
+=cut
+
+sub new_record_action {
+    my $self = shift;
+    my %args = (
+        model         => undef,
+        record_action => 'Update',
+        @_
+    );
+
+    # Get the name of the model class and action
+    my $model         = delete $args{model};
+    my $action_class  = blessed $model || $model;
+    my $record_action = delete $args{record_action};
+
+    # Convert it to the action
+    $action_class =~ s/::Model::/::Action::$record_action/;
+
+    # Add it to the arguments and call new_action()
+    $args{class} = $action_class;
+    return $self->new_action( %args );
+}
+
 =head3 failed_actions
 
 Returns an array of L<Jifty::Action> objects, one for each

Modified: jifty/trunk/t/01-test-web.t
==============================================================================
--- jifty/trunk/t/01-test-web.t	(original)
+++ jifty/trunk/t/01-test-web.t	Sun Aug 19 09:39:34 2007
@@ -1,6 +1,6 @@
 #!/usr/bin/perl -w
 
-use Jifty::Test tests => 5;
+use Jifty::Test tests => 9;
 
 my $web = Jifty::Test->web;
 isa_ok( $web->request,  "Jifty::Request"  );
@@ -36,3 +36,39 @@
 $web = Jifty::Test->web;
 isa_ok( $web->request,  "Jifty::Request::Subclass"  );
 isa_ok( $web->response, "Jifty::Response::Subclass" );
+
+# Testing new_record_action()
+{
+    no warnings 'redefine'; 
+
+    # Create a mock new_action()
+    $orig_new_action = \&Jifty::Web::new_action;
+    *Jifty::Web::new_action = sub {
+        is($args{class}, $args{expected});
+    };
+
+    Jifty->web->new_record_action(
+        model         => 'Jifty::Model::ModelClass',
+        expected      => 'Jifty::Action::UpdateModelClass',
+    );
+
+    Jifty->web->new_record_action(
+        model         => 'Jifty::Model::ModelClass',
+        record_action => 'Delete',
+        expected      => 'Jifty::Action::DeleteModelClass',
+    );
+
+    Jifty->web->new_record_action(
+        model         => 'TestApp::Model::Employee',
+        expected      => 'TestApp::Action::UpdateEmployee',
+    );
+
+    Jifty->web->new_record_action(
+        model         => 'TestApp::Model::Employee',
+        record_action => 'Search',
+        expected      => 'TestApp::Action::SearchEmployee',
+    );
+
+    # Restore the original
+    *Jifty::Web::new_action = $orig_new_action;
+}


More information about the Jifty-commit mailing list