r31712@martha (orig r1247): groditi | 2009-10-02 17:02:01 -0400
[catagits/Reaction.git] / lib / Reaction / UI / Controller / Role / Action / Object.pm
1 package Reaction::UI::Controller::Role::Action::Object;
2
3 use Moose::Role -traits => 'MethodAttributes';
4
5 requires 'get_collection';
6
7 sub object :Action :CaptureArgs(1) {
8   my ($self, $c, $key) = @_;
9   if( my $object = $self->get_collection($c)->find($key) ){
10     $c->stash(object => $object);
11     return $object;
12   }
13   $c->res->status(404);
14   return;
15 }
16
17 1;
18
19 __END__;
20
21 =head1 NAME
22
23 Reaction::UI::Controller::Role::Action::Object
24
25 =head1 DESCRIPTION
26
27 Provides an C<object> action, which attempts to find an item in a collection
28 and store it in the stash.
29
30 =head1 SYNOPSYS
31
32     package MyApp::Controller::Foo;
33
34     use base 'Reaction::Controller';
35     use Reaction::Class;
36
37     with(
38       'Reaction::UI::Controller::Role::GetCollection',
39       'Reaction::UI::Controller::Role::Action::Simple',
40       'Reaction::UI::Controller::Role::Action::Object',
41     );
42
43     __PACKAGE__->config( action => {
44       object => { Chained => 'base', PathPart => 'id' },
45       foo_action => { Chained => 'object' },
46     } );
47
48     sub base :Chained('/base') :CaptureArgs(0) {
49       ...
50     }
51
52     sub foo_action :Args(0){
53       my($self, $c) = @_;
54       $c->stash->{object}; #object is here....
55     }
56
57 =head1 REQUIRED METHODS
58
59 The following methods must be provided by the consuming class:
60
61 =over4
62
63 =item C<get_collection>
64
65 =back
66
67 =head1 ACTIONS
68
69 =head2 object
70
71 Chain link, captures one argument. Attempts to find a single object by passing
72 the captured argument to the C<find> method of the collection returned by
73 C<get_collection>. If the object is found it is stored in the stash under the
74 C<object> key.
75
76 =head1 SEE ALSO
77
78 =over4
79
80 =item L<Reaction::UI::Controller>
81
82 =item L<Reaction::UI::Controller::Role::GetCollection>
83
84 =item L<Reaction::UI::Controller::Role::Action::Simple>
85
86 =item L<Reaction::UI::Controller::Role::Action::List>
87
88 =item L<Reaction::UI::Controller::Role::Action::View>
89
90 =item L<Reaction::UI::Controller::Role::Action::Create>
91
92 =item L<Reaction::UI::Controller::Role::Action::Update>
93
94 =item L<Reaction::UI::Controller::Role::Action::Delete>
95
96 =item L<Reaction::UI::Controller::Role::Action::DeleteAll>
97
98 =back
99
100 =head1 AUTHORS
101
102 See L<Reaction::Class> for authors.
103
104 =head1 LICENSE
105
106 See L<Reaction::Class> for the license.
107
108 =cut