]> git.decadent.org.uk Git - maypole.git/blobdiff - lib/Maypole/Model/CDBI.pm
CRUD tests, FromCGI that works, test to prove it ;)
[maypole.git] / lib / Maypole / Model / CDBI.pm
index 3705331c8ec74de51de6f50ab4562591752bcc83..d8d5ace5ea3d18f76e6be064701b421597256593 100644 (file)
@@ -25,9 +25,18 @@ will instead use Class::DBI classes provided.
 
 use base qw(Maypole::Model::Base Class::DBI);
 use Maypole::Model::CDBI::AsForm;
+
+use Maypole::Model::CDBI::FromCGI; 
 use CGI::Untaint::Maypole;
+our $Untainter = 'CGI::Untaint::Maypole';
+
+# or if you like bugs 
+
+#use Class::DBI::FromCGI;
+#use CGI::Untaint;
+#our $Untainter = 'CGI::Untaint';
 
-use Class::DBI::FromCGI;
+use Class::DBI::Plugin::Type;
 use Class::DBI::Loader;
 use Class::DBI::AbstractSearch;
 use Class::DBI::Plugin::RetrieveAll;
@@ -36,6 +45,8 @@ use Class::DBI::Pager;
 use Lingua::EN::Inflect::Number qw(to_PL);
 use attributes ();
 
+use Data::Dumper;
+
 ###############################################################################
 # Helper methods
 
@@ -59,7 +70,7 @@ sub do_edit : Exported {
   my $config   = $r->config;
   my $table    = $r->table;
 
-  # handle cancel button hits
+  # handle cancel button hit
   if ( $r->{params}->{cancel} ) {
     $r->template("list");
     $r->objects( [$self->retrieve_all] );
@@ -98,11 +109,15 @@ sub do_edit : Exported {
     }
 
     undef $obj if $creating;
+
+    die "do_update failed with error : $fatal" if ($fatal);
     $r->template("edit");
   } else {
     $r->template("view");
   }
 
+
+
   $r->objects( $obj ? [$obj] : []);
 }
 
@@ -112,10 +127,20 @@ sub _do_update_or_create {
 
   my $fatal;
   my $creating = 0;
-  my $h = CGI::Untaint::Maypole->new( %{$r->params} );
+
+  my $h = $Untainter->new( %{$r->params} );
 
   # update or create
   if ($obj) {
+               # 1: Required fields for update are different than create. Its only required
+         #              if it is in the parameters
+
+#              my @real_required = ();
+#              my %required = map { $_ => 1 } @$required_cols;
+#    foreach (keys %{$r->params}) {
+#                      push @real_required, $_ if $required{$_};
+#        }
+                       
     # We have something to edit
     eval { $obj->update_from_cgi( $h => {
                                         required => $required_cols,
@@ -125,17 +150,14 @@ sub _do_update_or_create {
         };
     $fatal = $@;
   } else {
-    eval {
-      $obj = $self->create_from_cgi( $h => {
+       eval {
+       $obj = $self->create_from_cgi( $h => {
                                            required => $required_cols,
                                            ignore => $ignored_cols,
-                                          } )
-    };
-
-    if ($fatal = $@) {
-      warn "$fatal" if $r->debug;
-    }
-    $creating++;
+                                          } );
+       };
+       $fatal = $@;
+       $creating++;
   }
 
   return $obj, $fatal, $creating;
@@ -170,7 +192,9 @@ sub delete : Exported {
 
 sub do_delete {
   my ( $self, $r ) = @_;
+  # FIXME: handle fatal error with exception
   $_->SUPER::delete for @{ $r->objects || [] };
+#  $self->dbi_commit;
   $r->objects( [ $self->retrieve_all ] );
   $r->{template} = "list";
   $self->list($r);
@@ -580,7 +604,9 @@ sub setup_database {
     );
     $config->{classes} = [ $config->{loader}->classes ];
     $config->{tables}  = [ $config->{loader}->tables ];
-    warn( 'Loaded tables: ' . join ',', @{ $config->{tables} } )
+
+    my @table_class = map { $_ . " => " . $config->{loader}->_table2class($_) } @{ $config->{tables} };
+    warn( 'Loaded tables to classes: ' . join ', ', @table_class )
       if $namespace->debug;
 }