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

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Tue Sep 11 12:50:55 EDT 2007


Author: sterling
Date: Tue Sep 11 12:50:51 2007
New Revision: 4075

Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/lib/Jifty/Collection.pm
   jifty/trunk/lib/Jifty/Record.pm
   jifty/trunk/lib/Jifty/Web.pm
   jifty/trunk/t/TestApp/t/00-model-User.t

Log:
 r11935 at dynpc145:  andrew | 2007-09-11 11:50:08 -0500
  * Ripping out new_record_action() from Jifty::Web.
  * Re-implementing it as as_create_action(), as_update_action(), as_delete_action, and as_search_action() in Jifty::Record and Jifty::Collection.


Modified: jifty/trunk/lib/Jifty/Collection.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Collection.pm	(original)
+++ jifty/trunk/lib/Jifty/Collection.pm	Tue Sep 11 12:50:51 2007
@@ -53,6 +53,17 @@
 
 __PACKAGE__->mk_accessors(qw(pager results_are_readable));
 
+=head2 as_search_action
+
+Returns the L<Jifty::Action::Record::Search> action for the model associated with this collection.
+
+=cut
+
+sub as_search_action {
+    my $self = shift;
+    return $self->record_class->as_search_action;
+}
+
 =head2 add_record
 
 If L</results_are_readable> is false, only add records to the collection that

Modified: jifty/trunk/lib/Jifty/Record.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Record.pm	(original)
+++ jifty/trunk/lib/Jifty/Record.pm	Tue Sep 11 12:50:51 2007
@@ -152,6 +152,71 @@
     return ($id,$msg);
 }
 
+
+=head2 as_create_action
+
+Returns the L<Jifty::Action::Record::Create> action for this model class.
+
+=cut
+
+sub _action_from_record {
+    my $self = shift;
+    my $verb = shift;
+    my $class = ref $self || $self;
+    $class =~ s/::Model::/::Action::$verb/;
+    return $class;
+}
+
+sub as_create_action {
+    my $self = shift;
+    my $action_class = $self->_action_from_record('Create');
+    return Jifty->web->new_action( class => $action_class );
+}
+
+=head2 as_update_action
+
+Returns the L<Jifty::Action::Record::Update> action for this model class. The current record is passed to the constructor.
+
+=cut
+
+sub as_update_action {
+    my $self = shift;
+    my $action_class = $self->_action_from_record('Update');
+    return Jifty->web->new_action( 
+        class  => $action_class,
+        record => $self,
+    );
+}
+
+=head2 as_delete_action
+
+Returns the L<Jifty::Action::Record::Delete> action for this model class. The current record is passed to the constructor.
+
+=cut
+
+sub as_delete_action {
+    my $self = shift;
+    my $action_class = $self->_action_from_record('Delete');
+    return Jifty->web->new_action(
+        class  => $action_class,
+        record => $self,
+    );
+}
+
+=head2 as_search_action
+
+Returns the L<Jifty::Action::Record::Search> action for this model class.
+
+=cut
+
+sub as_search_action {
+    my $self = shift;
+    my $action_class = $self->_action_from_record('Search');
+    return Jifty->web->new_action(
+        class  => $action_class,
+    );
+}
+
 =head2 _guess_table_name
 
 Guesses a table name based on the class's last part. In addition to the work performed in L<Jifty::DBI::Record>, this method also prefixes the table name with the plugin table prefix, if the model belongs to a plugin.

Modified: jifty/trunk/lib/Jifty/Web.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Web.pm	(original)
+++ jifty/trunk/lib/Jifty/Web.pm	Tue Sep 11 12:50:51 2007
@@ -564,50 +564,6 @@
     );
 }
 
-=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/TestApp/t/00-model-User.t
==============================================================================
--- jifty/trunk/t/TestApp/t/00-model-User.t	(original)
+++ jifty/trunk/t/TestApp/t/00-model-User.t	Tue Sep 11 12:50:51 2007
@@ -11,7 +11,8 @@
 use lib 't/lib';
 use Jifty::SubTest;
 
-use Jifty::Test tests => 12;
+use Jifty::Test tests => 19;
+Jifty::Test->web; # initialize for use with the as_*_action tests
 # Make sure we can load the model
 use_ok('TestApp::Model::User');
 
@@ -27,6 +28,18 @@
 is($o->id, $id, "Create returned the right id");
 is($o->name, $$, "Created object has the right name");
 
+# Test the as_foo_action methods
+my $action = $o->as_create_action;
+isa_ok($action, 'TestApp::Action::CreateUser');
+$action = $o->as_update_action;
+isa_ok($action, 'TestApp::Action::UpdateUser');
+is($action->record->id, $o->id, 'update action ID is correct');
+$action = $o->as_delete_action;
+isa_ok($action, 'TestApp::Action::DeleteUser');
+is($action->record->id, $o->id, 'delete action ID is correct');
+$action = $o->as_search_action;
+isa_ok($action, 'TestApp::Action::SearchUser');
+
 # And another
 $o->create( name => $$, email => $$, password => $$ );
 ok($o->id, "User create returned another value");
@@ -37,6 +50,10 @@
 $collection->unlimit;
 is($collection->count, 2, "Finds two records");
 
+# Check the as_search_action method
+$action = $collection->as_search_action;
+isa_ok($action, 'TestApp::Action::SearchUser');
+
 # Searches in specific
 $collection->limit(column => 'id', value => $o->id);
 is($collection->count, 1, "Finds one record with specific id");


More information about the Jifty-commit mailing list