r31712@martha (orig r1247): groditi | 2009-10-02 17:02:01 -0400
[catagits/Reaction.git] / lib / Reaction / UI / Controller / Collection.pm
CommitLineData
89b70ba7 1package Reaction::UI::Controller::Collection;
2
26fa3b8a 3use Moose;
90bcd4d7 4BEGIN { extends 'Reaction::UI::Controller'; }
89b70ba7 5
8cc0103d 6use aliased 'Reaction::UI::ViewPort::Collection::Grid';
89b70ba7 7
931cbc8d 8__PACKAGE__->config(
9 action => {
10 list => { Chained => 'base', PathPart => '' },
730673c6 11 object => { Chained => 'base', PathPart => 'id' },
931cbc8d 12 view => { Chained => 'object', },
13 },
14);
89b70ba7 15
931cbc8d 16with(
17 'Reaction::UI::Controller::Role::GetCollection',
18 'Reaction::UI::Controller::Role::Action::Simple',
19 'Reaction::UI::Controller::Role::Action::Object',
20 'Reaction::UI::Controller::Role::Action::View',
21 'Reaction::UI::Controller::Role::Action::List'
22);
89b70ba7 23
37728bba 24has default_member_actions => (
25 isa => 'ArrayRef',
26 is => 'rw',
27 lazy_build => 1
28);
29
30has default_collection_actions => (
31 isa => 'ArrayRef',
32 is => 'rw',
33 lazy_build => 1
34);
35
36sub _build_default_member_actions { ['view'] }
37
38sub _build_default_collection_actions { [] }
39
931cbc8d 40around _build_action_viewport_map => sub {
41 my $orig = shift;
42 my $map = shift->$orig( @_ );
43 $map->{list} = Grid;
44 return $map;
45};
89b70ba7 46
931cbc8d 47around _build_action_viewport_args => sub {
48 my $orig = shift;
37728bba 49 my $self = shift;
50 my $args = { list => { Member => {} } };
51
931cbc8d 52 my $m_protos = $args->{list}{Member}{action_prototypes} = {};
53 for my $action_name( @{ $self->default_member_actions }){
54 my $label = join(' ', map { ucfirst } split(/_/, $action_name));
55 my $proto = $self->_build_member_action_prototype($label, $action_name);
56 $m_protos->{$action_name} = $proto;
57 }
37728bba 58
931cbc8d 59 my $c_protos = $args->{list}{action_prototypes} = {};
60 for my $action_name( @{ $self->default_collection_actions }){
61 my $label = join(' ', map { ucfirst } split(/_/, $action_name));
62 my $proto = $self->_build_collection_action_prototype($label, $action_name);
63 $c_protos->{$action_name} = $proto;
64 }
37728bba 65
66 return $args;
931cbc8d 67};
37728bba 68
69sub _build_member_action_prototype {
70 my ($self, $label, $action_name) = @_;
71 return {
72 label => $label,
73 uri => sub {
74 my $action = $self->action_for($action_name);
75 $_[1]->uri_for($action, [ @{$_[1]->req->captures}, $_[0]->__id ]);
76 },
77 };
78}
79
80sub _build_collection_action_prototype {
81 my ($self, $label, $action_name) = @_;
82 return {
83 label => $label,
84 uri => sub {
85 my $action = $self->action_for($action_name);
86 $_[1]->uri_for($action, $_[1]->req->captures);
87 },
88 };
89b70ba7 89}
90
89b70ba7 91
931cbc8d 92sub base :CaptureArgs(0) {
89b70ba7 93 my ($self, $c) = @_;
89b70ba7 94}
95
931cbc8d 96##DEPRECATED ACTION
89b70ba7 97
aee256be 98sub basic_page {
931cbc8d 99 my( $self, $c, @args) = @_;
100 if( $c->debug ){
101 my ($package,undef,$line,$sub_name,@rest) = caller(1);
102 my $message = "The method 'basic_page', called from sub '${sub_name}' in package ${package} at line ${line} is deprecated. Please use 'setup_viewport' instead.";
103 $c->log->debug( $message );
104 }
105 $self->setup_viewport( $c, @args );
89b70ba7 106}
107
1081;
b3832dbc 109
b3832dbc 110__END__;
111
112=head1 NAME
113
7d3fe0d2 114Reaction::UI::Controller
b3832dbc 115
116=head1 DESCRIPTION
117
118Controller class used to make displaying collections easier.
119Inherits from L<Reaction::UI::Controller>.
120
931cbc8d 121=head1 ROLES CONSUMED
b3832dbc 122
931cbc8d 123This role also consumes the following roles:
b3832dbc 124
931cbc8d 125=over4
b3832dbc 126
931cbc8d 127=item L<Reaction::UI::Controller::Role::Action::GetCollection>
b3832dbc 128
931cbc8d 129=item L<Reaction::UI::Controller::Role::Action::Simple>
b3832dbc 130
931cbc8d 131=item L<Reaction::UI::Controller::Role::Action::Object>
b3832dbc 132
931cbc8d 133=item L<Reaction::UI::Controller::Role::Action::List>
b3832dbc 134
931cbc8d 135=item L<Reaction::UI::Controller::Role::Action::View>
b3832dbc 136
137=back
138
931cbc8d 139=head1 ATTRIBUTES
b3832dbc 140
b3cb974a 141=head2 default_member_actions
142
143Read-write lazy building arrayref. The names of the member actions (the actions
144that apply to each member of the collection and typically have an object as a
145target e.g. update,delete) to be enabled by default. By default, this is only
146'view'
147
148=over 4
149
3dc10901 150=item B<_build_default_member_actions> - Provided builder method, see METHODS
b3cb974a 151
152=item B<has_default_member_actions> - Auto generated predicate
153
154=item B<clear_default_member_actions>- Auto generated clearer method
155
156=back
157
158=head2 default_collection_actions
159
160Read-write lazy building arrayref. The names of the collection actions (the
161actions that apply to the entire collection and typically have a collection as
162a target e.g. create, delete_all) to be enabled by default. By default, this
163is only empty.
164
165=over 4
166
3dc10901 167=item B<_build_default_member_actions> - Provided builder method, see METHODS
b3cb974a 168
169=item B<has_default_member_actions> - Auto generated predicate
170
171=item B<clear_default_member_actions>- Auto generated clearer method
172
173=back
174
b3832dbc 175=head1 METHODS
176
b3832dbc 177=head2 _build_action_viewport_map
178
931cbc8d 179Set C<list> to L<Reaction::UI::ViewPort::Collection::Grid>
b3832dbc 180
181=head2 _build_action_viewport_args
182
b3cb974a 183By default will reurn a hashref containing action prototypes for all default
184member and collection actions. The prototype URI generators are generated by
185C<_build_member_action_prototype> and C<_build_collection_action_prototype>
186respectively and labels are the result of replacing underscores in the name
187with spaces and capitalizing the first letter. If you plan to use custom
188actions that are not supported by this scheme or you would like to customize
189the values it is suggested you wrap / override this method.
190
191Default output for a controller having only 'view' enabled:
192
193 { list => {
194 action_prototypes => {},
195 Member => {
196 action_prototypes => {
197 view => {label => 'View', uri => sub{...} },
198 },
199 },
200 },
201 }
202
203=head2 _build_member_action_prototype $label, $action_name
204
205Creates an action prototype suitable for creating action links in
206L<Reaction::UI::ViewPort::Role::Actions>. C<$action_name> should be the name of
207a Catalyst action in this controller.The prototype will generate a URI
208based on the action, current captures.
209
210=head2 _build_collection_action_prototype $label, $action_name
b3832dbc 211
7d3fe0d2 212=head2 basic_page $c, \%vp_args
213
931cbc8d 214Deprecated alias to C<setup_viewport>.
7d3fe0d2 215
b3832dbc 216=head1 ACTIONS
217
218=head2 base
219
220Chain link, no-op.
221
222=head2 list
223
931cbc8d 224Chained to C<base>. See L<Reaction::UI::Controller::Role::Action::List>
b3832dbc 225
226=head2 object
227
931cbc8d 228Chained to C<base>. See L<Reaction::UI::Controller::Role::Action::Object>
b3832dbc 229
230=head2 view
231
931cbc8d 232Chained to C<object>. See L<Reaction::UI::Controller::Role::Action::View>
b3832dbc 233
de5b3fab 234=head1 SEE ALSO
b3832dbc 235
7d3fe0d2 236L<Reaction::UI::Controller>
b3832dbc 237
238=head1 AUTHORS
239
240See L<Reaction::Class> for authors.
241
242=head1 LICENSE
243
244See L<Reaction::Class> for the license.
245
246=cut