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/Role/Action/Create.pm | 145 +++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 lib/Reaction/UI/Controller/Role/Action/Create.pm (limited to 'lib/Reaction/UI/Controller/Role/Action/Create.pm') diff --git a/lib/Reaction/UI/Controller/Role/Action/Create.pm b/lib/Reaction/UI/Controller/Role/Action/Create.pm new file mode 100644 index 0000000..f227ef8 --- /dev/null +++ b/lib/Reaction/UI/Controller/Role/Action/Create.pm @@ -0,0 +1,145 @@ +package Reaction::UI::Controller::Role::Action::Create; + +use Moose::Role -traits => 'MethodAttributes'; +use Reaction::UI::ViewPort::Action; + +requires qw/get_collection make_context_closure setup_viewport/; + +sub create :Action :Args(0) { + my ($self, $c) = @_; + my $target = $c->stash->{collection} || $self->get_collection($c); + my %vp_args = ( model => $target->action_for('Create') ); + + if( $self->can('on_create_apply_callback') ){ + my $apply = sub { $self->on_create_apply_callback( @_) }; + $vp_args{on_apply_callback} = $self->make_context_closure( $apply ); + } + if( $self->can('on_create_close_callback') ){ + my $close = sub { $self->on_create_close_callback( @_) }; + $vp_args{on_close_callback} = $self->make_context_closure( $close ); + } + + $self->setup_viewport( $c, \%vp_args ); +} + +around _build_action_viewport_map => sub { + my $orig = shift; + my $map = shift->$orig( @_ ); + $map->{create} = 'Reaction::UI::ViewPort::Action'; + return $map; +}; + +1; + +__END__; + +=head1 NAME + +Reaction::UI::Controller::Role::Action::Create - Create action + +=head1 DESCRIPTION + +Provides a C action, which sets up an L +by calling C on either the object located in the C slot +of the C or on the object returned by the method C. + +=head1 SYNOPSYS + + package MyApp::Controller::Foo; + + use base 'Reaction::Controller'; + use Reaction::Class; + + with( + 'Reaction::UI::Controller::Role::GetCollection', + 'Reaction::UI::Controller::Role::Action::Simple', + 'Reaction::UI::Controller::Role::Action::Create' + ); + + __PACKAGE__->config( action => { + create => { Chained => 'base' }, + } ); + + sub base :Chained('/base') :CaptureArgs(0) { + ... + } + + sub on_create_apply_callback{ #optional callback + my($self, $c, $vp, $result) = @_; + ... + } + + sub on_create_close_callback{ #optional callback + my($self, $c, $vp) = @_; + ... + } + +=head1 ROLES CONSUMED + +This role also consumes the following roles: + +=over4 + +=item L + +=back + +=head1 REQUIRED METHODS + +The following methods must be provided by the consuming class: + +=over4 + +=item C + +=item C + +=back + +=head1 ACTIONS + +=head2 create + +Chain endpoint with no args, sets up the viewport with the appropriate action. +If the methods C and C are +present in the consuming class, they will be used as callbacks in the viewport. + +=head1 METHODS + +=head2 _build_action_viewport_map + +Extends to set the C key in the map to L + +=head1 SEE ALSO + +=over4 + +=item L + +=item L + +=item L + +=item L + +=item L + +=item L + +=item L + +=item L + +=item L + +=back + +=head1 AUTHORS + +See L for authors. + +=head1 LICENSE + +See L for the license. + +=cut -- cgit v1.2.3-54-g00ecf