From 931cbc8d6673ec352b369ae2f70f01ff96b4f507 Mon Sep 17 00:00:00 2001 From: groditi Date: Fri, 11 Sep 2009 13:36:16 +0000 Subject: initial refactor of CRUD controller actions as roles --- lib/Reaction/UI/Controller/Collection/CRUD.pm | 159 ++++++++++++-------------- 1 file changed, 72 insertions(+), 87 deletions(-) (limited to 'lib/Reaction/UI/Controller/Collection/CRUD.pm') diff --git a/lib/Reaction/UI/Controller/Collection/CRUD.pm b/lib/Reaction/UI/Controller/Collection/CRUD.pm index 11a29f9..c8d1147 100644 --- a/lib/Reaction/UI/Controller/Collection/CRUD.pm +++ b/lib/Reaction/UI/Controller/Collection/CRUD.pm @@ -1,26 +1,36 @@ 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'; use aliased 'Reaction::UI::ViewPort::ListView'; -sub _build_action_viewport_map { - 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; +__PACKAGE__->config( + action => { + create => { Chained => 'base', }, + delete_all => { Chained => 'base', }, + update => { Chained => 'object', }, + delete => { Chained => 'object', }, + }, +); + +with( + 'Reaction::UI::Controller::Role::Action::Create', +); + +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_default_member_actions { [ @{shift->next::method(@_)}, qw/update delete/ ]; @@ -30,37 +40,14 @@ sub _build_default_collection_actions { [ @{shift->next::method(@_)}, qw/create delete_all/ ]; } -sub get_model_action { - my ($self, $c, $name, $target) = @_; - return $target->action_for($name, ctx => $c); -} -sub create :Chained('base') :PathPart('create') :Args(0) { - my ($self, $c) = @_; - my $apply = sub { $self->after_create_callback( @_) }; - my $close = sub { $self->on_create_close_callback( @_) }; - my $vp_args = { - target => ($c->stash->{collection} || $self->get_collection($c)), - on_apply_callback => $self->make_context_closure($apply), - on_close_callback => $self->make_context_closure($close), - }; - $self->basic_model_action( $c, $vp_args); -} - -sub delete_all :Chained('base') :PathPart('delete_all') :Args(0) { - my ($self, $c) = @_; - my $close = sub { $self->on_delete_all_close_callback( @_) }; - $self->basic_model_action( $c, { - target => ($c->stash->{collection} || $self->get_collection($c)), - on_close_callback => $self->make_context_closure($close), - }); -} +##DEFAULT CALLBACKS 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 ] ); @@ -71,15 +58,6 @@ sub on_create_close_callback { $self->redirect_to( $c, 'list' ); } -sub update :Chained('object') :Args(0) { - my ($self, $c) = @_; - my $close = sub { $self->on_update_close_callback( @_) }; - my $vp_args = { - on_close_callback => $self->make_context_closure($close), - }; - $self->basic_model_action( $c, $vp_args); -} - sub on_update_close_callback { my($self, $c) = @_; #this needs a better solution. currently thinking about it @@ -88,15 +66,6 @@ sub on_update_close_callback { $self->redirect_to($c, 'list', \@cap); } -sub delete :Chained('object') :Args(0) { - my ($self, $c) = @_; - my $close = sub { $self->on_delete_close_callback( @_) }; - my $vp_args = { - on_close_callback => $self->make_context_closure($close), - }; - $self->basic_model_action( $c, $vp_args); -} - sub on_delete_close_callback { my($self, $c) = @_; #this needs a better solution. currently thinking about it @@ -105,8 +74,25 @@ sub on_delete_close_callback { $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) = @_; + 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)); @@ -134,31 +120,45 @@ easily create complex and highly flexible CRUD functionality for your 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 + +=item L + +=item L + +=item L + +=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 for model C<$target> This action is suitable for passing to an C viewport -=head2 after_create_callback $c, $vp, $result - -When a action is applied, move the user to the new object's, -C page. - =head2 basic_model_action $c, \%vp_args -Extension to C which automatically instantiates an +DEPRECTAED extension to C which automatically instantiates an L with the right data target using C +=head2 after_create_callback $c, $vp, $result + +When a action is applied, move the user to the new object's, +C page. + =head2 _build_action_viewport_map -Map C, C, C and C to use the -L viewport by default and have C -use L by default. +Map C to L. =head2 _build_default_member_actions @@ -172,34 +172,19 @@ Add C and C to the list of default actions. =head2 create -Chaned to C. Create a new member of the collection represented by -this controller. By default it attaches the C to -DWIM after apply operations. - -See L - for more info. +Chained to C. See L =head2 delete_all -Chained to B, delete all the members of the B. In most cases -this is very much like a C operation. - -See L - for more info. +Chained to C. See L =head2 update -Chained to C, update a single object. - -See L - for more info. +Chained to C. See L =head2 delete -Chained to C, delete a single object. - -See L - for more info. +Chained to C. See L =head1 SEE ALSO -- cgit v1.2.3-54-g00ecf