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