more POD!
[catagits/Reaction.git] / lib / Reaction / UI / Controller / Collection / CRUD.pm
index 04d46e9..1141826 100644 (file)
@@ -6,62 +6,54 @@ use base 'Reaction::UI::Controller::Collection';
 use Reaction::Class;
 
 use aliased 'Reaction::UI::ViewPort::Action';
+use aliased 'Reaction::UI::ViewPort::ListView';
 
 sub _build_action_viewport_map {
-  my $map = shift->next::method(@_);
-  map{ $map->{$_} = Action } qw/create update delete delete_all/;
+  my $self = shift;
+  my $map = $self->next::method(@_);
+  $map->{list} = ListView if exists $map->{list};
+
+  #my %allowed = map { $_ => undef }
+  #  ( @{$self->default_member_actions}, @{$self->default_collection_actions} );
+  #my @local_actions = qw/create update delete delete_all/;
+  #$map->{$_} = Action for grep { exists $allowed{$_} } @local_actions;
+
+  $map->{$_} = Action for @local_actions;
   return $map;
 }
 
-sub _build_action_viewport_args {
-  my $args = shift->next::method(@_);
-  $args->{list} =
-    { action_prototypes =>
-      [ { label => 'Create', action => sub {
-            [ '', 'create',    $_[1]->req->captures ] } },
-        { label => 'Delete all', action => sub {
-            [ '', 'delete_all', $_[1]->req->captures ] } },
-      ],
-      Member =>
-      { 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 ] ] } },
-        ],
-      },
-    };
-  return $args;
+sub _build_default_member_actions {
+  [ @{shift->next::method(@_)}, qw/update delete/ ];
+}
+
+sub _build_default_collection_actions {
+  [ @{shift->next::method(@_)}, qw/create delete_all/ ];
 }
 
 sub get_model_action {
   my ($self, $c, $name, $target) = @_;
-
-  if ($target->can('action_for')) {
-    return $target->action_for($name, ctx => $c);
-  }
-
-  #can we please kill this already?
-  my $model_name = "Action::${name}".$self->model_name;
-  my $model = $c->model($model_name);
-  confess "no such Model $model_name" unless $model;
-  return $model->new(target_model => $target, ctx => $c);
+  return $target->action_for($name, ctx => $c);
 }
 
 sub create :Chained('base') :PathPart('create') :Args(0) {
   my ($self, $c) = @_;
   my $vp_args = {
-                 next_action => 'list',
-                 on_apply_callback => sub { $self->after_create_callback($c => @_); },
-                };
+    on_apply_callback => sub { $self->after_create_callback($c => @_); },
+    on_close_callback => sub { $self->on_create_close_callback($c => @_) }
+  };
   $self->basic_model_action( $c, $vp_args);
 }
 
 sub delete_all :Chained('base') :PathPart('delete_all') :Args(0) {
   my ($self, $c) = @_;
-  $self->basic_model_action( $c,  { next_action => 'list'});
+  $self->basic_model_action( $c, {
+    on_close_callback => sub { $self->on_delete_all_close_callback($c => @_) }
+  });
+}
+
+sub on_delete_all_close_callback {
+  my($self, $c) = @_;
+  $self->redirect_to($c, 'list');
 }
 
 sub after_create_callback {
@@ -70,21 +62,32 @@ sub after_create_callback {
     ( $c, 'update', [ @{$c->req->captures}, $result->id ] );
 }
 
+sub on_create_close_callback {
+  my($self, $c, $vp) = @_;
+  $self->redirect_to( $c, 'list' );
+}
+
 sub update :Chained('object') :Args(0) {
   my ($self, $c) = @_;
+  my $vp_args = {
+    on_close_callback => sub { $self->on_update_close_callback($c => @_ ) }
+  };
+  $self->basic_model_action( $c, $vp_args);
+}
+
+sub on_update_close_callback {
+  my($self, $c) = @_;
   #this needs a better solution. currently thinking about it
   my @cap = @{$c->req->captures};
   pop(@cap); # object id
-  my $vp_args = { next_action => [ $self, 'redirect_to', 'list', \@cap ]};
-  $self->basic_model_action( $c, $vp_args);
+  $self->redirect_to($c, 'list', \@cap);
 }
 
 sub delete :Chained('object') :Args(0) {
   my ($self, $c) = @_;
-  #this needs a better solution. currently thinking about it
-  my @cap = @{$c->req->captures}; 
-  pop(@cap); # object id
-  my $vp_args = { next_action => [ $self, 'redirect_to', 'list', \@cap ]};
+  my $vp_args = {
+    on_close_callback => sub { $self->on_update_close_callback($c => @_) }
+  };
   $self->basic_model_action( $c, $vp_args);
 }
 
@@ -139,12 +142,17 @@ data target using C<get_model_action>
 
 =head2 _build_action_viewport_map
 
-Map C<create>, C<update>, C<delete> and C<delete_all> to use the 
-C<Action|Reaction::UI::ViewPort::Action> viewport by default.
+Map C<create>, C<update>, C<delete> and C<delete_all> to use the
+L<Action|Reaction::UI::ViewPort::Action> viewport by default and have C<list>
+use L<ListView|Reaction::UI::ViewPort::ListView> by default.
+
+=head2 _build_default_member_actions
 
-=head2 _build_action_viewport_args
+Add C<update> and C<delete> to the list of default actions.
 
-Add action_prototypes to the C<list> action so that action links render correctly in L<ListView|Rection::UI::ViewPort::Listview>.
+=head2 _build_default_collection_actions
+
+Add C<create> and C<delete_all> to the list of default actions.
 
 =head1 ACTIONS
 
@@ -174,8 +182,7 @@ See L<Update|Reaction::InterfaceModel::Action::DBIC::Result::Update>
 
 =head2 delete
 
-Chained to C<object>, deletee a single object.
-
+Chained to C<object>, delete a single object.
 
 See L<Delete|Reaction::InterfaceModel::Action::DBIC::Result::Delete>
  for more info.