[Bps-public-commit] r17106 - in Prophet/branches/actions/lib/Prophet: Server Server/ViewHelpers Web

jesse at bestpractical.com jesse at bestpractical.com
Thu Dec 4 21:54:05 EST 2008


Author: jesse
Date: Thu Dec  4 21:54:05 2008
New Revision: 17106

Modified:
   Prophet/branches/actions/lib/Prophet/Server.pm
   Prophet/branches/actions/lib/Prophet/Server/Controller.pm
   Prophet/branches/actions/lib/Prophet/Server/ViewHelpers.pm
   Prophet/branches/actions/lib/Prophet/Server/ViewHelpers/Function.pm
   Prophet/branches/actions/lib/Prophet/Server/ViewHelpers/Widget.pm
   Prophet/branches/actions/lib/Prophet/Web/Field.pm

Log:
* we can now do basic updates

Modified: Prophet/branches/actions/lib/Prophet/Server.pm
==============================================================================
--- Prophet/branches/actions/lib/Prophet/Server.pm	(original)
+++ Prophet/branches/actions/lib/Prophet/Server.pm	Thu Dec  4 21:54:05 2008
@@ -55,7 +55,7 @@
     $self->cgi($cgi);
 
    
-    my $controller = Prophet::Server::Controller->new(cgi => $self->cgi); 
+    my $controller = Prophet::Server::Controller->new(cgi => $self->cgi, app_handle => $self->app_handle); 
     $controller->handle_actions();
 
      my $dispatcher_class = ref($self->app_handle) . "::Server::Dispatcher";

Modified: Prophet/branches/actions/lib/Prophet/Server/Controller.pm
==============================================================================
--- Prophet/branches/actions/lib/Prophet/Server/Controller.pm	(original)
+++ Prophet/branches/actions/lib/Prophet/Server/Controller.pm	Thu Dec  4 21:54:05 2008
@@ -5,8 +5,8 @@
 has cgi => (is => 'rw', isa => 'CGI');
 has failed => ( is => 'rw', isa => 'Bool');
 has failure_message => ( is => 'rw', isa => 'Str');
-
-
+has actions => (is => 'rw', isa => 'HashRef');
+has app_handle => (is => 'rw', isa => 'Prophet::App');
 
 =head1 NAME
 
@@ -20,85 +20,47 @@
 
 =cut
 
+
 sub extract_actions_from_cgi {
     my $self = shift;
 
-    my $cgi = $self->cgi;
-    my @params = $cgi->all_parameters;
-
-    my @action_hashes;
-
-    my $bundles = $self->_bundle_params_by_action(\@params);   
-    for (values %$bundles) {
-        push  @action_hashes, $self->_bundle_to_hash($_);
-    } 
-   return \@action_hashes; 
-}
-
-
-sub _bundle_params_by_action {
-    my $self = shift;
-    my $params = shift;
+    my $actions = {};
+   foreach my $param ($self->cgi->all_parameters){
+        next unless $param =~ /^prophet-function\|(.*)$/;
+        my $name = $1;
+        warn "Duplicate action definition for @{[$name]}." if (exists $actions->{$name});
 
-    my $bundles = {};
-    my @actions = $self->_find_actions_from_cgi_params($params);
-    foreach my $param (@$params) {
-        my $action = $self->_parse_cgi_param_name($param);
-        $bundles->{$action} = 'XXX TODO';
-    }
-    
 
-    return $bundles;
-}
+        my $action_data = $self->cgi->param($param);
+        my $attr = $self->string_to_hash($action_data);
 
-sub find_actions_from_cgi {
-    my $self = shift;
-    my $params = shift;
-
-    my $cgi = $self->cgi;
-    my $actions = {};
-   foreach my $param (@$params) {
-        next unless $param =~ /^prophet-action(.*)$/;
-        my $action_data = $1;
-
-       my @bits = grep { defined $_ } split( /|/, $action_data );
-        my %attr ={
-            split(/=/, @bits)
-        };
-
-        $attr{value} = $cgi->param($param);
-
-        warn "Duplicate action definition for @{[$attr{name}]}." if ($actions->{$attr{name}});
-        $actions->{$attr{name}} = \%attr;
-        $actions->{$attr{name}}->{params} = $self->params_for_action_from_cgi($attr{name});
+        $actions->{$name} = $attr;
+        $actions->{$name}->{params} = $self->params_for_action_from_cgi($name);
    } 
-
+   $self->actions($actions);
 }
 
 sub params_for_action_from_cgi {
-    my $self = shift;
+    my $self   = shift;
     my $action = shift;
 
-    my @params = grep { /^prophet-field|.*?|action=$action|/} 
-    $self->cgi->all_parameters
-}
-
+    my $values;
+    for my $field ( $self->cgi->all_parameters ) {
+        next unless ( $field =~ /^prophet-field\|function=$action\|(.*)\|$/ );
+        my $metadata = $1;
+        my $meta     = $self->string_to_hash($metadata);
+        my $name     = $meta->{prop};
+        $meta->{value} = $self->cgi->param($field);
+        $meta->{original_value} = $self->cgi->param( "original-value-" . $field );
 
-sub _parse_cgi_param_name {
-    my $self = shift;
-    my $param = shift;
+        $values->{$name} = $meta;
 
-    my ($uuid, $prop);
-    if ($param =~ /|uuid-(.*?)|/) {
-        $uuid = $1;
-    }
-    if ($param =~ /|prop-(.*?)|/) {
-        $prop = $1;
     }
-    my $value = $self->cgi->param($param); 
 
+    return $values;
 }
 
+
 sub _bundle_to_hash {
     my $self = shift;
 }
@@ -119,12 +81,31 @@
     if (my $err = $@) {
         $self->failed(1);
         $self->failure_message($err);   
+        warn "AIEEE: $err";
     }
 }
 
-
 sub canonicalize_actions {
-    my $self = shift;
+    my $self    = shift;
+    my $actions = $self->actions;
+    foreach my $action ( keys %$actions ) {
+        foreach my $param (
+            keys %{ $actions->{$action}->{params} }
+
+            )
+        {
+            if ( $actions->{$action}->{params}->{$param}->{original_value} eq
+                $actions->{$action}->{params}->{$param}->{value} )
+            {
+
+                delete $actions->{$action}->{params}->{$param};
+                next;
+            }
+
+        }
+
+    }
+
 }
 
 sub validate_actions {
@@ -132,8 +113,55 @@
 }
 
 sub execute_actions {
+    my $self = shift;
+
+    foreach my $action (keys %{$self->actions}) {
+
+        if ($self->actions->{$action}->{action} eq 'update') {
+            $self->_exec_action_update($self->actions->{$action});
+        } else {
+            die "I don't know how to handle a ".$self->actions->{$action}->{action};
+        }
+
+        warn "My action is $action";
+        warn YAML::Dump($self->actions->{$action}); use YAML;
+    }
+
+}
+
+
+sub _exec_action_update {
+    my $self = shift;
+    my $action = shift;
+
+    die $action->{class} ." is not a valid class " unless (UNIVERSAL::isa($action->{class}, 'Prophet::Record'));
+    my $object = $action->{class}->new( uuid => $action->{uuid}, app_handle => $self->app_handle);
+    die "Did not find the object " unless $object->uuid;
+
+            warn YAML::Dump({ map { $_ -> $action->{props}->{$_}->{value} } keys %{$action->{props}}});
+    warn "YAY we got ".$object->uuid;
+    my ( $val, $msg ) = $object->set_props(
+        props => {
+            map {
+                $action->{params}->{$_}->{prop} => $action->{params}->{$_}->{value}
+                } keys %{ $action->{params} }
+            }
+
+    );
+    warn $val, $msg;
 
 }
+
+
+sub string_to_hash {
+    my $self = shift;
+    my $data = shift;
+    my @bits = grep {$_} split( /\|/, $data );
+    my %attr = map { split( /=/, $_ ) } @bits;
+    return \%attr;
+}
+
+
 __PACKAGE__->meta->make_immutable;
 no Moose;
 

Modified: Prophet/branches/actions/lib/Prophet/Server/ViewHelpers.pm
==============================================================================
--- Prophet/branches/actions/lib/Prophet/Server/ViewHelpers.pm	(original)
+++ Prophet/branches/actions/lib/Prophet/Server/ViewHelpers.pm	Thu Dec  4 21:54:05 2008
@@ -7,7 +7,7 @@
 use Params::Validate qw/validate/;
 use Template::Declare::Tags;
 use Prophet::Web::Field;
-our @EXPORT = qw(page content widget function);
+our @EXPORT = ( qw(form page content widget function));
 use Prophet::Server::ViewHelpers::Widget;
 use Prophet::Server::ViewHelpers::Function;
 
@@ -52,4 +52,17 @@
     return $w;
 }
 
+
+BEGIN {
+   no warnings 'redefine'; 
+    *old_form = \&form;
+*form = sub (&;$){
+    my $code = shift;
+        old_form ( sub { attr { method => 'post'};
+            $code->(@_);
+        }
+    )
+}};
+
+
 1;

Modified: Prophet/branches/actions/lib/Prophet/Server/ViewHelpers/Function.pm
==============================================================================
--- Prophet/branches/actions/lib/Prophet/Server/ViewHelpers/Function.pm	(original)
+++ Prophet/branches/actions/lib/Prophet/Server/ViewHelpers/Function.pm	Thu Dec  4 21:54:05 2008
@@ -57,18 +57,20 @@
     my %bits =( 
         order  => $self->order,
         action => $self->action,
+        type => $self->record->type,
+        class => ref($self->record),
         uuid   => $self->record->uuid
     );
 
     my $string
         = "|"
-        . join( "|", map { $bits{$_} ? $_ . "-" . $bits{$_} : '' } keys %bits )
+        . join( "|", map { $bits{$_} ? $_ . "=" . $bits{$_} : '' } keys %bits )
         . "|";
 
     input {
         attr {
             type  => 'hidden',
-            name  => "prophet-action|" . $self->name,
+            name  => "prophet-function|" . $self->name,
             value => $string
         };
     };

Modified: Prophet/branches/actions/lib/Prophet/Server/ViewHelpers/Widget.pm
==============================================================================
--- Prophet/branches/actions/lib/Prophet/Server/ViewHelpers/Widget.pm	(original)
+++ Prophet/branches/actions/lib/Prophet/Server/ViewHelpers/Widget.pm	Thu Dec  4 21:54:05 2008
@@ -34,11 +34,19 @@
     my $self = shift;
 
     my $f = Prophet::Web::Field->new(
-        name       => $self->_generate_name(),
-            record => $self->function->record,
-        label => $self->prop,
-        value => $self->function->record->prop( $self->prop )
+        name   => $self->_generate_name(),
+        record => $self->function->record,
+        label  => $self->prop,
+        value  => $self->function->record->prop( $self->prop )
     );
+
+    my $orig = Prophet::Web::Field->new(
+        name  => "original-value-". $self->_generate_name(),
+        value => $self->function->record->prop( $self->prop ),
+        type  => 'hidden'
+    );
+    outs_raw( $orig->render_input );
+
     outs_raw( $f->render );
 }
 
@@ -48,9 +56,9 @@
 sub _generate_name {
     my $self = shift;
     return
-          "prophet-field||function-"
+          "prophet-field|function="
         . $self->function->name
-        . "|prop-"
+        . "|prop="
         . $self->prop . "|";
 }
 

Modified: Prophet/branches/actions/lib/Prophet/Web/Field.pm
==============================================================================
--- Prophet/branches/actions/lib/Prophet/Web/Field.pm	(original)
+++ Prophet/branches/actions/lib/Prophet/Web/Field.pm	Thu Dec  4 21:54:05 2008
@@ -9,6 +9,7 @@
 has id    => ( isa => 'Str', is => 'rw' );
 has class => ( isa => 'Str', is => 'rw' );
 has value => ( isa => 'Str', is => 'rw' );
+has type => ( isa => 'Str', is => 'rw', default => 'text');
 
 sub _render_attr {
     my $self = shift;
@@ -43,13 +44,22 @@
 
     my $output = <<EOF;
 <label @{[$self->render_name]}>@{[$self->label]}</label>
-<input type="text" @{[$self->render_name]} @{[$self->render_id]} @{[$self->render_class]} @{[$self->render_value]} />
+@{[$self->render_input]}
+
 
 EOF
 
     return $output;
 
 }
+sub render_input {
+    my $self = shift;
+return <<EOF;
+<input type="@{[$self->type]}" @{[$self->render_name]} @{[$self->render_id]} @{[$self->render_class]} @{[$self->render_value]} />
+EOF
+}
+
+
 
 __PACKAGE__->meta->make_immutable;
 no Moose;



More information about the Bps-public-commit mailing list