[Jifty-commit] r4080 - in jifty/trunk: lib/Jifty/View/Declare t/TestApp/t

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Tue Sep 11 13:38:57 EDT 2007


Author: sterling
Date: Tue Sep 11 13:38:57 2007
New Revision: 4080

Added:
   jifty/trunk/t/TestApp/t/crud.t
Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/lib/Jifty/View/Declare/CRUD.pm

Log:
 r11941 at dynpc145:  andrew | 2007-09-11 12:38:20 -0500
  * Simplifying the CRUD code for generating the record_class and making it more flexible.
  * Adding a stub for CRUD testing.


Modified: jifty/trunk/lib/Jifty/View/Declare/CRUD.pm
==============================================================================
--- jifty/trunk/lib/Jifty/View/Declare/CRUD.pm	(original)
+++ jifty/trunk/lib/Jifty/View/Declare/CRUD.pm	Tue Sep 11 13:38:57 2007
@@ -78,6 +78,8 @@
     Jifty::Util->require($vclass);
     eval qq{package $caller;
             alias $vclass under '$path'; 1} or die $@;
+
+    # Override object_type
     no strict 'refs';
     *{$vclass."::object_type"} = sub { $model };
 }
@@ -110,6 +112,40 @@
     return $self->package_variable('object_type') || get('object_type');
 }
 
+=head2 record_class
+
+This is the full name of the model class these CRUD views are for. The default implementation returns:
+
+  Jifty->app_class('Model', $self->object_type);
+
+You will want to override this if (in addition to L</object_type>) if you want to provide CRUD views in a plugin, or from an external model class, or for one of the Jifty built-in models.
+
+=cut
+
+# NB: We don't just create the record class here and return it. Why? Because
+# the mount_view() method is generally called very early in the Jifty
+# lifecycle. As such, Jifty->app_class() might not work yet since it requires
+# the Jifty singleton to be built and the configuration to be loaded. So, this
+# implementation caches teh record class after the first calculation, which
+# should happen during the request dispatch process, which always happens after
+# Jifty is completely initialized.
+sub record_class {
+    my $self = shift;
+
+    # If object_type is set via set, don't cache
+    if (!$self->package_variable('object_type') && get('object_type')) {
+        return Jifty->app_class('Model', $self->object_type);
+    }
+
+    # Otherwise, assume object_type is permanent
+    else {
+        return ($self->package_variable('record_class') 
+            or ($self->package_variable( record_class =>
+                    Jifty->app_class('Model', $self->object_type))));
+    }
+
+}
+
 =head2 fragment_for
 
 This is a helper that returns the path to a given fragment. The only argument is the name of the fragment. It returns a absolute base path to the fragment page.
@@ -170,7 +206,7 @@
     my ( $self, $id ) = @_;
 
     # Load the model, create an empty object, load the object by ID
-    my $record_class = Jifty->app_class( "Model", $self->object_type );
+    my $record_class = $self->record_class;
     my $record = $record_class->new();
     $record->load($id);
 
@@ -219,9 +255,8 @@
 template 'search' => sub {
     my $self          = shift;
     my ($object_type) = ( $self->object_type );
-    my $search        = Jifty->web->new_action(
-        class             => "Search" . $object_type,
-        moniker           => "search",
+    my $search        = $self->record_class->as_search_action(
+        moniker           => 'search',
         sticky_on_success => 1,
     );
 
@@ -251,10 +286,8 @@
     my $self   = shift;
     my $record = $self->_get_record( get('id') );
 
-    my $update = new_action(
-        class   => 'Update' . $self->object_type,
+    my $update = $record->as_update_action(
         moniker => "update-" . Jifty->web->serial,
-        record  => $record,
     );
 
     div {
@@ -303,13 +336,11 @@
     my $self = shift;
     my ( $object_type, $id ) = ( $self->object_type, get('id') );
 
-    my $record_class = Jifty->app_class( "Model", $object_type );
+    my $record_class = $self->record_class;
     my $record = $record_class->new();
     $record->load($id);
-    my $update = new_action(
-        class   => "Update" . $object_type,
+    my $update = $record->as_update_action(
         moniker => "update-" . Jifty->web->serial,
-        record  => $record
     );
 
     div {
@@ -336,11 +367,10 @@
     my $object_type = $self->object_type;
     my $id = $record->id;
 
-    my $delete = Jifty->web->form->add_action(
-        class   => 'Delete' . $object_type,
+    my $delete = $record->as_delete_action(
         moniker => 'delete-' . Jifty->web->serial,
-        record  => $record
     );
+    Jifty->web->form->register_action($delete);
 
         div {
             { class is 'crud editlink' };
@@ -413,7 +443,7 @@
 sub _current_collection {
     my $self = shift; 
     my ( $page, $search_collection ) = get(qw(page  search_collection));
-    my $collection_class = Jifty->app_class( "Model", $self->object_type . "Collection" );
+    my $collection_class = $self->record_class->collection_class;
     my $search = $search_collection || ( Jifty->web->response->result('search') ? Jifty->web->response->result('search')->content('search') : undef );
     my $collection;
     if ( $search ) {
@@ -596,8 +626,8 @@
     my $self = shift;
     my ( $object_type, $id ) = ( $self->object_type, get('id') );
 
-    my $record_class = Jifty->app_class( "Model", $object_type );
-    my $create = Jifty->web->new_action( class => 'Create' . $object_type );
+    my $record_class = $self->record_class;
+    my $create = $record_class->as_create_action;
 
     div {
         { class is 'crud create item inline' };

Added: jifty/trunk/t/TestApp/t/crud.t
==============================================================================
--- (empty file)
+++ jifty/trunk/t/TestApp/t/crud.t	Tue Sep 11 13:38:57 2007
@@ -0,0 +1,20 @@
+#!/usr/bin/env perl
+use warnings;
+use strict;
+
+use lib 't/lib';
+use Jifty::SubTest;
+
+use Jifty::Test tests => 3;
+use Jifty::Test::WWW::Mechanize;
+
+my $server = Jifty::Test->make_server;
+isa_ok($server, 'Jifty::Server');
+
+my $url = $server->started_ok;
+
+my $mech = Jifty::Test::WWW::Mechanize->new;
+
+$mech->get_ok("$url/crud/User");
+
+# TODO FIXME XXX Surely more tests are needed... and don't call me Shirley.


More information about the Jifty-commit mailing list