[Jifty-commit] r5980 - in JiftyX-ModelHelpers/trunk: lib/JiftyX t/Simapp/t

Jifty commits jifty-commit at lists.jifty.org
Tue Nov 4 11:44:24 EST 2008


Author: gugod
Date: Tue Nov  4 11:44:24 2008
New Revision: 5980

Modified:
   JiftyX-ModelHelpers/trunk/   (props changed)
   JiftyX-ModelHelpers/trunk/Changes
   JiftyX-ModelHelpers/trunk/dist.ini
   JiftyX-ModelHelpers/trunk/lib/JiftyX/ModelHelpers.pm
   JiftyX-ModelHelpers/trunk/t/Simapp/t/helpers.t

Log:
 r24718 at yra:  gugod | 2008-11-04 22:14:56 +0800
 Refactor M() and autogenerated helpers such that:
 
 - helpers now fully delegate to M()
 - M can take a sold id value from now.
 
     M("Book", 42);
 
   Or, a more intuitive way to write it:
 
     M(Book => 42);
 
 r24719 at yra:  gugod | 2008-11-04 22:21:56 +0800
 Document about the changes to M().
 


Modified: JiftyX-ModelHelpers/trunk/Changes
==============================================================================
--- JiftyX-ModelHelpers/trunk/Changes	(original)
+++ JiftyX-ModelHelpers/trunk/Changes	Tue Nov  4 11:44:24 2008
@@ -1,5 +1,10 @@
 
-0.20
+0.22:
+- Improve M() function to accept parameters for model constructors.
+- M() can also take a sole id value now: M(Book, 42)
+- auto-generated helpers now works exactly the same as M()
+
+0.20:
 - Add M() function exported by default
 
 2008-09-08T21:13:46+0800

Modified: JiftyX-ModelHelpers/trunk/dist.ini
==============================================================================
--- JiftyX-ModelHelpers/trunk/dist.ini	(original)
+++ JiftyX-ModelHelpers/trunk/dist.ini	Tue Nov  4 11:44:24 2008
@@ -1,5 +1,5 @@
 name    = JiftyX-ModelHelpers
-version = 0.21
+version = 0.22
 author  = Kang-min Liu <gugod at gugod.org>
 license = MIT
 copyright_holder = Kang-min Liu

Modified: JiftyX-ModelHelpers/trunk/lib/JiftyX/ModelHelpers.pm
==============================================================================
--- JiftyX-ModelHelpers/trunk/lib/JiftyX/ModelHelpers.pm	(original)
+++ JiftyX-ModelHelpers/trunk/lib/JiftyX/ModelHelpers.pm	Tue Nov  4 11:44:24 2008
@@ -6,7 +6,6 @@
 
 our @ISA = qw(Exporter);
 our @EXPORT = qw(M);
-use YAML;
 
 sub M {
     my ($model, @params) = @_;;
@@ -28,6 +27,9 @@
             }
         }
         else {
+            if (@params == 1) {
+                unshift @params, "id";
+            }
             $record->load_by_cols(@params);
         }
     }
@@ -53,25 +55,9 @@
 
     no strict 'refs';
     for my $model (@models) {
-        if ( index($model, "Collection") >= 0) {
-            *{"$model"} = sub {
-                my @args = @_;
-                return M($model, @args);
-            }
-        }
-        else {
-            *{"$model"} = sub {
-                my @args = @_;
-                my $obj = M($model);
-                if (@args == 1) {
-                    $obj->load($args[0]);
-                }
-                elsif (@args && @args % 2 == 0) {
-                    $obj->load_by_cols(@args);
-                }
-                return $obj;
-            };
-        }
+        *{"$model"} = sub {
+            return M($model, @_);
+        };
         push @EXPORT, "&${model}";
     }
 
@@ -87,6 +73,9 @@
     use JiftyX::ModelHelper;
 
     # Load the record of book with id = $id
+    $book = M(Book => $id);
+
+    # Another way.
     $book = M(Book => id => $id);
 
     # Load by other criteria
@@ -157,6 +146,15 @@
     $book => Jifty->app_class(Model => "Book")->new;
     $book->load_by_cols(isbn => "978-0099410676");
 
+If you pass only one numeric value instead of a key-value pair, that
+is specially treated as if it's an record id. So instead of saying:
+
+    $book = M("Book", id => 42);
+
+It can be shorten to:
+
+    $book = M(Book => 42);
+
 If the given model name is a collection, instead of meaning a I<blank>
 collection object, it means the collection of I<all> records.
 

Modified: JiftyX-ModelHelpers/trunk/t/Simapp/t/helpers.t
==============================================================================
--- JiftyX-ModelHelpers/trunk/t/Simapp/t/helpers.t	(original)
+++ JiftyX-ModelHelpers/trunk/t/Simapp/t/helpers.t	Tue Nov  4 11:44:24 2008
@@ -2,7 +2,7 @@
 use warnings;
 use strict;
 
-use Jifty::Test::Dist tests => 6;
+use Jifty::Test::Dist tests => 10;
 use JiftyX::ModelHelpers;
 
 {
@@ -36,3 +36,16 @@
     is( ref($b), "Simapp::Model::BookCollection" );
     is( $b->count, 1 );
 }
+
+{
+    my $system_user = Simapp::CurrentUser->superuser;
+
+    my $b = Book({ current_user => $system_user });
+    my ($id) = $b->create(name => "Book Created by System User");
+
+    ok( $b->current_user->is_superuser );
+
+    ok($id, "Book create returned success");
+    ok($b->id, "New Book has valid id set");
+    is($b->id, $id, "Create returned the right id");
+}


More information about the Jifty-commit mailing list