package Reaction::UI::Controller::Collection::CRUD;
-use strict;
-use warnings;
-use base 'Reaction::UI::Controller::Collection';
use Reaction::Class;
-
-use aliased 'Reaction::UI::ViewPort::Action';
-
-sub _build_action_viewport_map {
- my $map = shift->next::method(@_);
- map{ $map->{$_} = Action } qw/create update delete delete_all/;
+BEGIN { extends 'Reaction::UI::Controller::Collection'; }
+
+use aliased 'Reaction::UI::ViewPort::ListView';
+
+__PACKAGE__->config(
+ action => {
+ create => { Chained => 'base', },
+ delete_all => { Chained => 'base', },
+ update => { Chained => 'object', },
+ delete => { Chained => 'object', },
+ },
+);
+
+with(
+ 'Reaction::UI::Controller::Role::Action::Create',
+ 'Reaction::UI::Controller::Role::Action::Update',
+ 'Reaction::UI::Controller::Role::Action::Delete',
+ 'Reaction::UI::Controller::Role::Action::DeleteAll',
+);
+
+around _build_action_viewport_map => sub {
+ my $orig = shift;
+ my $map = shift->$orig( @_ );
+ $map->{list} = ListView;
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 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);
+sub _build_default_collection_actions {
+ [ @{shift->next::method(@_)}, qw/create delete_all/ ];
}
-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 => @_); },
- };
- $self->basic_model_action( $c, $vp_args);
-}
+##DEFAULT CALLBACKS
-sub delete_all :Chained('base') :PathPart('delete_all') :Args(0) {
- my ($self, $c) = @_;
- $self->basic_model_action( $c, { next_action => 'list'});
+sub on_delete_all_close_callback {
+ my($self, $c) = @_;
+ $self->redirect_to($c, 'list');
}
-sub after_create_callback {
+sub on_create_apply_callback {
my ($self, $c, $vp, $result) = @_;
return $self->redirect_to
( $c, 'update', [ @{$c->req->captures}, $result->id ] );
}
-sub update :Chained('object') :Args(0) {
- my ($self, $c) = @_;
+sub on_create_close_callback {
+ my($self, $c, $vp) = @_;
+ $self->redirect_to( $c, 'list' );
+}
+
+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) = @_;
+sub on_delete_close_callback {
+ my($self, $c) = @_;
#this needs a better solution. currently thinking about it
- my @cap = @{$c->req->captures};
+ 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);
+}
+
+#### DEPRECATED METHODS
+
+sub get_model_action {
+ my ($self, $c, $name, $target) = @_;
+ if( $c->debug ){
+ my ($package,undef,$line,$sub_name,@rest) = caller(1);
+ my $message = "The method 'get_model_action', called from sub '${sub_name}' in package ${package} at line ${line} is deprecated.";
+ $c->log->debug( $message );
+ }
+ return $target->action_for($name, ctx => $c);
}
sub basic_model_action {
my ($self, $c, $vp_args) = @_;
-
- my $target = exists $c->stash->{object} ?
- $c->stash->{object} : $self->get_collection($c);
+ if( $c->debug ){
+ my ($package,undef,$line,$sub_name,@rest) = caller(1);
+ my $message = "The method 'basic_model_action', called from sub '${sub_name}' in package ${package} at line ${line} is deprecated.";
+ $c->log->debug( $message );
+ }
+ my $stash = $c->stash;
+ my $target = delete $vp_args->{target};
+ $target ||= ($stash->{object} || $stash->{collection} || $self->get_collection($c));
my $action_name = join('', map{ ucfirst } split('_', $c->stack->[-1]->name));
my $model = $self->get_model_action($c, $action_name, $target);
=head1 NAME
-Reaction::UI::Controller::CRUD - Basic CRUD functionality for Reaction::InterfaceModel data
+Reaction::UI::Controller::Collection::CRUD - Basic CRUD functionality for Reaction::InterfaceModel data
=head1 DESCRIPTION
InterfaceModel models by providing a simple way to render and process your
custom InterfaceModel Actions and customize built-ins.
+=head1 ROLES CONSUMED
+
+This role also consumes the following roles:
+
+=over4
+
+=item L<Reaction::UI::Controller::Role::Action::Create>
+
+=item L<Reaction::UI::Controller::Role::Action::Update>
+
+=item L<Reaction::UI::Controller::Role::Action::Delete>
+
+=item L<Reaction::UI::Controller::Role::Action::DeleteAll>
+
+=back
+
=head1 METHODS
=head2 get_model_action $c, $action_name, $target_im
-Get an instance of the C<$action_name>
+DEPRECATED. Get an instance of the C<$action_name>
L<InterfaceModel::Action|Reaction::InterfaceModel::Action> for model C<$target>
This action is suitable for passing to an
C<Action|Reaction::UI::ViewPort::Action> viewport
+=head2 basic_model_action $c, \%vp_args
+
+DEPRECTAED extension to C<basic_page> which automatically instantiates an
+L<InterfaceModel::Action|Reaction::InterfaceModel::Action> with the right
+data target using C<get_model_action>
+
=head2 after_create_callback $c, $vp, $result
When a <create> action is applied, move the user to the new object's,
C<update> page.
-=head2 basic_model_action $c, \%vp_args
+=head2 _build_action_viewport_map
-Extension to C<basic_page> which automatically instantiates an
-L<InterfaceModel::Action|Reaction::InterfaceModel::Action> with the right
-data target using C<get_model_action>
+Map C<list> to L<ListView|Reaction::UI::ViewPort::ListView>.
-=head2 _build_action_viewport_map
+=head2 _build_default_member_actions
-Map C<create>, C<update>, C<delete> and C<delete_all> to use the
-C<Action|Reaction::UI::ViewPort::Action> viewport by default.
+Add C<update> and C<delete> to the list of default actions.
-=head2 _build_action_viewport_args
+=head2 _build_default_collection_actions
-Add action_prototypes to the C<list> action so that action links render correctly in L<ListView|Rection::UI::ViewPort::Listview>.
+Add C<create> and C<delete_all> to the list of default actions.
=head1 ACTIONS
=head2 create
-Chaned to C<base>. Create a new member of the collection represented by
-this controller. By default it attaches the C<after_create_callback> to
-DWIM after apply operations.
-
-See L<Create|Reaction::InterfaceModel::Action::DBIC::ResultSet::Create>
- for more info.
+Chained to C<base>. See L<Reaction::UI::Controller::Role::Action::Create>
=head2 delete_all
-Chained to B<base>, delete all the members of the B<collection>. In most cases
-this is very much like a C<TRUNCATE> operation.
-
-See L<DeleteAll|Reaction::InterfaceModel::Action::DBIC::ResultSet::DeleteAll>
- for more info.
+Chained to C<base>. See L<Reaction::UI::Controller::Role::Action::DeleteAll>
=head2 update
-Chained to C<object>, update a single object.
-
-See L<Update|Reaction::InterfaceModel::Action::DBIC::Result::Update>
- for more info.
+Chained to C<object>. See L<Reaction::UI::Controller::Role::Action::Update>
=head2 delete
-Chained to C<object>, deletee a single object.
-
-
-See L<Delete|Reaction::InterfaceModel::Action::DBIC::Result::Delete>
- for more info.
+Chained to C<object>. See L<Reaction::UI::Controller::Role::Action::Delete>
=head1 SEE ALSO