made listview do paging and ordering and all sorts of stupid shit. nobody is welcome...
[catagits/Reaction.git] / lib / Reaction / UI / CRUDController.pm
index b630dd9..6720cf4 100644 (file)
@@ -12,12 +12,42 @@ use aliased 'Reaction::UI::ViewPort::ObjectView';
 has 'model_base' => (isa => 'Str', is => 'rw', required => 1);
 has 'model_name' => (isa => 'Str', is => 'rw', required => 1);
 
-has 'ActionForm_class' => (isa => 'Str', is => 'rw', required => 1,
-                           lazy => 1, default => sub{ ActionForm });
-has 'ListView_class'   => (isa => 'Str', is => 'rw', required => 1,
-                           lazy => 1, default => sub{ ListView });
-has 'ObjectView_class' => (isa => 'Str', is => 'rw', required => 1,
-                           lazy => 1, default => sub{ ObjectView });
+has action_viewport_map  => (isa => 'HashRef', is => 'rw', lazy_build => 1);
+has action_viewport_args => (isa => 'HashRef', is => 'rw', lazy_build => 1);
+
+sub build_action_viewport_map {
+  return {
+          list       => ListView,
+          view       => ObjectView,
+          create     => ActionForm,
+          update     => ActionForm,
+          delete     => ActionForm,
+          delete_all => ActionForm,
+         };
+}
+
+sub build_action_viewport_args {
+  my $self = shift;
+  return { list =>
+           { action_prototypes =>
+             [ { label => 'Create', action => sub {
+                   [ '', 'create',    $_[1]->req->captures ] } },
+               { label => 'Delete all', action => sub {
+                   [ '', 'delete_all', $_[1]->req->captures ] } },
+             ],
+             Entity =>
+             { action_prototypes =>
+               [ { label => 'View', action => sub {
+                     [ '', 'view', [ @{$_[1]->req->captures},   $_[0]->__id ] ] } },
+                 { label => 'Edit', action => sub {
+                     [ '', 'update', [ @{$_[1]->req->captures}, $_[0]->__id ] ] } },
+                 { label => 'Delete', action => sub {
+                     [ '', 'delete', [ @{$_[1]->req->captures}, $_[0]->__id ] ] } },
+               ],
+             },
+           },
+         };
+}
 
 sub base :Action :CaptureArgs(0) {
   my ($self, $c) = @_;
@@ -47,27 +77,41 @@ sub list :Chained('base') :PathPart('') :Args(0) {
   my ($self, $c) = @_;
 
   $self->push_viewport(
-    $self->ListView_class,
-    collection => $self->get_collection($c)
-  );
+                       $self->action_viewport_map->{list},
+                       %{ $self->action_viewport_args->{list} || {} },
+                       collection => $self->get_collection($c)
+                      );
 }
 
 sub create :Chained('base') :PathPart('create') :Args(0) {
   my ($self, $c) = @_;
   my $action = $self->get_model_action($c, 'Create', $self->get_collection($c));
-  $self->push_viewport(
-    $self->ActionForm_class,
-    action => $action,
-    next_action => 'list',
-    on_apply_callback => sub { $self->after_create_callback($c => @_); },
-  );
+  $self->push_viewport
+    (
+     $self->action_viewport_map->{create},
+     %{ $self->action_viewport_args->{create} || {} },
+     action => $action,
+     next_action => 'list',
+     on_apply_callback => sub { $self->after_create_callback($c => @_); },
+    );
+}
+
+sub delete_all :Chained('base') :PathPart('delete_all') :Args(0) {
+  my ($self, $c) = @_;
+  my $action = $self->get_model_action($c, 'DeleteAll', $self->get_collection($c));
+  $self->push_viewport
+    (
+     $self->action_viewport_map->{delete_all},
+     %{ $self->action_viewport_args->{delete_all} || {} },
+     action => $action,
+     next_action => 'list',
+    );
 }
 
 sub after_create_callback {
   my ($self, $c, $vp, $result) = @_;
-  return $self->redirect_to(
-           $c, 'update', [ @{$c->req->captures}, $result->id ]
-         );
+  return $self->redirect_to
+    ( $c, 'update', [ @{$c->req->captures}, $result->id ] );
 }
 
 sub object :Chained('base') :PathPart('id') :CaptureArgs(1) {
@@ -82,10 +126,12 @@ sub update :Chained('object') :Args(0) {
   my $action = $self->get_model_action($c, 'Update', $object);
   my @cap = @{$c->req->captures};
   pop(@cap); # object id
-  $self->push_viewport(
-    $self->ActionForm_class,
-    action => $action,
-    next_action => [ $self, 'redirect_to', 'list', \@cap ]
+  $self->push_viewport
+    (
+     $self->action_viewport_map->{update},
+     %{ $self->action_viewport_args->{update} || {} },
+     action => $action,
+     next_action => [ $self, 'redirect_to', 'list', \@cap ]
   );
 }
 
@@ -95,10 +141,12 @@ sub delete :Chained('object') :Args(0) {
   my $action = $self->get_model_action($c, 'Delete', $object);
   my @cap = @{$c->req->captures};
   pop(@cap); # object id
-  $self->push_viewport(
-    $self->ActionForm_class,
-    action => $action,
-    next_action => [ $self, 'redirect_to', 'list', \@cap ]
+  $self->push_viewport
+    (
+     $self->action_viewport_map->{delete},
+     %{ $self->action_viewport_args->{delete} || {} },
+     action => $action,
+     next_action => [ $self, 'redirect_to', 'list', \@cap ]
   );
 }
 
@@ -107,10 +155,12 @@ sub view :Chained('object') :Args(0) {
   my $object :Stashed;
   my @cap = @{$c->req->captures};
   pop(@cap); # object id
-  $self->push_viewport(
-    $self->ObjectView_class,
-    object => $object
-  );
+  $self->push_viewport
+    (
+     $self->action_viewport_map->{view},
+     %{ $self->action_viewport_args->{view} || {} },
+     object => $object,
+    );
 }
 
 1;