Commit | Line | Data |
89b70ba7 |
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'; |
c8fbb8ad |
9 | use aliased 'Reaction::UI::ViewPort::Object'; |
89b70ba7 |
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, |
c8fbb8ad |
20 | view => Object, |
89b70ba7 |
21 | }; |
22 | } |
23 | |
24 | sub _build_action_viewport_args { |
25 | return { }; |
26 | } |
27 | |
89b70ba7 |
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 $attr = $model->meta->find_attribute_by_name( $self->collection_name ); |
33 | my $reader = $attr->get_read_method; |
34 | return $model->$reader; |
35 | } |
36 | |
b3832dbc |
37 | sub base :Action :CaptureArgs(0) { |
89b70ba7 |
38 | my ($self, $c) = @_; |
89b70ba7 |
39 | } |
40 | |
41 | sub 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 |
48 | sub 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 |
53 | sub view :Chained('object') :Args(0) { |
54 | my ($self, $c) = @_; |
b68d6a35 |
55 | $self->basic_page($c, { model => $c->stash->{object} }); |
89b70ba7 |
56 | } |
57 | |
aee256be |
58 | sub 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 | |
69 | 1; |
b3832dbc |
70 | |
71 | |
72 | __END__; |
73 | |
74 | =head1 NAME |
75 | |
7d3fe0d2 |
76 | Reaction::UI::Controller |
b3832dbc |
77 | |
78 | =head1 DESCRIPTION |
79 | |
80 | Controller class used to make displaying collections easier. |
81 | Inherits from L<Reaction::UI::Controller>. |
82 | |
83 | =head1 ATTRIBUTES |
84 | |
85 | =head2 model_name |
86 | |
7d3fe0d2 |
87 | The name of the model this controller will use as it's data source. Should be a |
88 | name that can be passed to C<$C-E<gt>model> |
b3832dbc |
89 | |
90 | =head2 collection_name |
91 | |
7d3fe0d2 |
92 | The name of the collection whithin the model that this Controller will be |
93 | utilizing. |
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 |
107 | Read-write lazy building hashref. The keys should match action names in the |
108 | Controller and the value should be the ViewPort class that this action should |
109 | use. See method C<basic_page> for more info. |
b3832dbc |
110 | |
111 | =head action_viewport_args |
112 | |
7d3fe0d2 |
113 | Read-write lazy building hashref. Additional ViewPort arguments for the action |
114 | named 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 | |
130 | Returns an instance of the collection this controller uses. |
131 | |
132 | =head2 _build_action_viewport_map |
133 | |
134 | Provided 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 | |
141 | Returns an empty hashref. |
142 | |
7d3fe0d2 |
143 | =head2 basic_page $c, \%vp_args |
144 | |
145 | Accepts two arguments, context, and a hashref of viewport arguments. It will |
146 | automatically determine the action name using the catalyst stack and call |
147 | C<push_viewport> with the ViewPort class name contained in the |
148 | C<action_viewport_map> with a set of options determined by merging C<$vp_args> |
149 | and the arguments contained in C<action_viewport_args>, if any. |
150 | |
b3832dbc |
151 | =head1 ACTIONS |
152 | |
153 | =head2 base |
154 | |
155 | Chain link, no-op. |
156 | |
157 | =head2 list |
158 | |
7d3fe0d2 |
159 | Chain link, chained to C<base>. C<list> fetches the collection for the model |
160 | and calls C<basic_page> with a single argument, C<collection>. |
b3832dbc |
161 | |
7d3fe0d2 |
162 | The default ViewPort for this action is C<Reaction::UI::ViewPort::ListView> and |
163 | can be changed by altering the C<action_viewport_map> attribute hash. |
b3832dbc |
164 | |
165 | =head2 object |
166 | |
7d3fe0d2 |
167 | Chain link, chained to C<base>, captures one argument, 'id'. Attempts to find |
168 | a single object by searching for a member of the current collection which has a |
169 | Primary Key or Unique constraint matching that argument. If the object is found |
170 | it is stored in the stash under the C<object> key. |
b3832dbc |
171 | |
172 | =head2 view |
173 | |
7d3fe0d2 |
174 | Chain link, chained to C<object>. Calls C<basic page> with one argument, |
175 | C<model>, which contains an instance of the object fetched by the C<object> |
176 | action link. |
b3832dbc |
177 | |
7d3fe0d2 |
178 | The default ViewPort for this action is C<Reaction::UI::ViewPort::Object> and |
179 | can be changed by altering the C<action_viewport_map> attribute hash. |
b3832dbc |
180 | |
7d3fe0d2 |
181 | =SEE ALSO |
b3832dbc |
182 | |
7d3fe0d2 |
183 | L<Reaction::UI::Controller> |
b3832dbc |
184 | |
185 | =head1 AUTHORS |
186 | |
187 | See L<Reaction::Class> for authors. |
188 | |
189 | =head1 LICENSE |
190 | |
191 | See L<Reaction::Class> for the license. |
192 | |
193 | =cut |