683473e4fbbf2c5b5f9b696eae9790a83eb213d3
[catagits/Reaction.git] / lib / Reaction / UI / Controller / Collection.pm
1 package Reaction::UI::Controller::Collection;
2
3 use strict;
4 use warnings;
5 use base 'Reaction::UI::Controller';
6 use Reaction::Class;
7
8 use aliased 'Reaction::UI::ViewPort::ListView';
9 use aliased 'Reaction::UI::ViewPort::Object';
10
11 has 'model_name'      => (isa => 'Str', is => 'rw', required => 1);
12 has 'collection_name' => (isa => 'Str', is => 'rw', required => 1);
13
14 has action_viewport_map  => (isa => 'HashRef', is => 'rw', lazy_build => 1);
15 has action_viewport_args => (isa => 'HashRef', is => 'rw', lazy_build => 1);
16
17 sub _build_action_viewport_map {
18   return {
19           list => ListView,
20           view => Object,
21          };
22 }
23
24 sub _build_action_viewport_args {
25   return { };
26 }
27
28 #XXX candidate for futre optimization, should cache reader?
29 sub get_collection {
30   my ($self, $c) = @_;
31   my $model = $c->model( $self->model_name );
32   my $collection = $self->collection_name;
33   if( my $meth = $model->can( $collection ) ){
34     return $model->$meth;
35   } elsif ( my $attr = $model->meta->find_attribute_by_name($collection) ) {
36     my $reader = $attr->get_read_method;
37     return $model->$reader;
38   }
39   confess "Failed to find collection $collection";
40 }
41
42 sub base :Action :CaptureArgs(0) {
43   my ($self, $c) = @_;
44 }
45
46 sub object :Chained('base') :PathPart('id') :CaptureArgs(1) {
47   my ($self, $c, $key) = @_;
48   my $object = $self->get_collection($c)->find($key);
49   $c->detach("/error_404") unless $object;
50   $c->stash(object => $object);
51 }
52
53 sub list :Chained('base') :PathPart('') :Args(0) {
54   my ($self, $c) = @_;
55   $self->basic_page($c, { collection => $self->get_collection($c) });
56 }
57
58 sub view :Chained('object') :Args(0) {
59   my ($self, $c) = @_;
60   $self->basic_page($c, { model => $c->stash->{object} });
61 }
62
63 sub basic_page {
64   my ($self, $c, $vp_args) = @_;
65   my $action_name = $c->stack->[-1]->name;
66   return $self->push_viewport
67     (
68      $self->action_viewport_map->{$action_name},
69      %{ $vp_args || {} },
70      %{ $self->action_viewport_args->{$action_name} || {} },
71     );
72 }
73
74 1;
75
76
77 __END__;
78
79 =head1 NAME
80
81 Reaction::UI::Controller
82
83 =head1 DESCRIPTION
84
85 Controller class used to make displaying collections easier.
86 Inherits from L<Reaction::UI::Controller>.
87
88 =head1 ATTRIBUTES
89
90 =head2 model_name
91
92 The name of the model this controller will use as it's data source. Should be a
93 name that can be passed to C<$C-E<gt>model>
94
95 =head2 collection_name
96
97 The name of the collection whithin the model that this Controller will be
98 utilizing.
99
100 =head2 action_viewport_map
101
102 =over 4
103
104 =item B<_build_action_viewport_map> - Provided builder method, see METHODS
105
106 =item B<has_action_viewport_map> - Auto generated predicate
107
108 =item B<clear_action_viewport_map>- Auto generated clearer method
109
110 =back
111
112 Read-write lazy building hashref. The keys should match action names in the
113 Controller and the value should be the ViewPort class that this action should
114 use. See method C<basic_page> for more info.
115
116 =head action_viewport_args
117
118 Read-write lazy building hashref. Additional ViewPort arguments for the action
119 named as the key in the controller.  See method C<basic_page> for more info.
120
121 =over 4
122
123 =item B<_build_action_viewport_args> - Provided builder method, see METHODS
124
125 =item B<has_action_viewport_args> - Auto generated predicate
126
127 =item B<clear_action_viewport_args>- Auto generated clearer method
128
129 =back
130
131 =head1 METHODS
132
133 =head2 get_collection $c
134
135 Returns an instance of the collection this controller uses.
136
137 =head2 _build_action_viewport_map
138
139 Provided builder for C<action_viewport_map>. Returns a hash with two items:
140
141     list => 'Reaction::UI::ViewPort::ListView',
142     view => 'Reaction::UI::ViewPort::Object',
143
144 =head2 _build_action_viewport_args
145
146 Returns an empty hashref.
147
148 =head2 basic_page $c, \%vp_args
149
150 Accepts two arguments, context, and a hashref of viewport arguments. It will
151 automatically determine the action name using the catalyst stack and call
152 C<push_viewport> with the ViewPort class name contained in the
153 C<action_viewport_map> with a set of options determined by merging C<$vp_args>
154 and the arguments contained in C<action_viewport_args>, if any.
155
156 =head1 ACTIONS
157
158 =head2 base
159
160 Chain link, no-op.
161
162 =head2 list
163
164 Chain link, chained to C<base>. C<list> fetches the collection for the model
165 and calls C<basic_page> with a single argument, C<collection>.
166
167 The default ViewPort for this action is C<Reaction::UI::ViewPort::ListView> and
168 can be changed by altering the C<action_viewport_map> attribute hash.
169
170 =head2 object
171
172 Chain link, chained to C<base>, captures one argument, 'id'. Attempts to find
173 a single object by searching for a member of the current collection which has a
174 Primary Key or Unique constraint matching that argument. If the object is found
175 it is stored in the stash under the C<object> key.
176
177 =head2 view
178
179 Chain link, chained to C<object>. Calls C<basic page> with one argument,
180 C<model>, which contains an instance of the object fetched by the C<object>
181 action link.
182
183 The default ViewPort for this action is C<Reaction::UI::ViewPort::Object> and
184 can be changed by altering the C<action_viewport_map> attribute hash.
185
186 =SEE ALSO
187
188 L<Reaction::UI::Controller>
189
190 =head1 AUTHORS
191
192 See L<Reaction::Class> for authors.
193
194 =head1 LICENSE
195
196 See L<Reaction::Class> for the license.
197
198 =cut