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/Object.pm | 108 +++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 lib/Reaction/UI/Controller/Role/Action/Object.pm (limited to 'lib/Reaction/UI/Controller/Role/Action/Object.pm') diff --git a/lib/Reaction/UI/Controller/Role/Action/Object.pm b/lib/Reaction/UI/Controller/Role/Action/Object.pm new file mode 100644 index 0000000..95dbfbd --- /dev/null +++ b/lib/Reaction/UI/Controller/Role/Action/Object.pm @@ -0,0 +1,108 @@ +package Reaction::UI::Controller::Role::Action::Object; + +use Moose::Role -traits => 'MethodAttributes'; + +requires 'get_collection'; + +sub object :Action :CaptureArgs(1) { + my ($self, $c, $key) = @_; + if( my $object = $self->get_collection($c)->find($key) ){ + $c->stash(object => $object); + return $object; + } + $c->res->status(404); + return; +} + +1; + +__END__; + +=head1 NAME + +Reaction::UI::Controller::Role::Action::Object + +=head1 DESCRIPTION + +Provides an C action, which attempts to find an item in a collection +and store it in the stash. + +=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::Object', + ); + + __PACKAGE__->config( action => { + object => { Chained => 'base', PathPart => 'id' }, + foo_action => { Chained => 'object' }, + } ); + + sub base :Chained('/base') :CaptureArgs(0) { + ... + } + + sub foo_action :Args(0){ + my($self, $c) = @_; + $c->stash->{object}; #object is here.... + } + +=head1 REQUIRED METHODS + +The following methods must be provided by the consuming class: + +=over4 + +=item C + +=back + +=head1 ACTIONS + +=head2 object + +Chain link, captures one argument. Attempts to find a single object by passing +the captured argument to the C method of the collection returned by +C. If the object is found it is stored in the stash under the +C key. + +=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