1 package Reaction::UI::Controller::Collection;
5 use base 'Reaction::UI::Controller';
8 use aliased 'Reaction::UI::ViewPort::ListView';
9 use aliased 'Reaction::UI::ViewPort::ObjectView';
11 has 'model_name' => (isa => 'Str', is => 'rw', required => 1);
12 has 'collection_name' => (isa => 'Str', is => 'rw', required => 1);
14 has action_viewport_map => (isa => 'HashRef', is => 'rw', lazy_build => 1);
15 has action_viewport_args => (isa => 'HashRef', is => 'rw', lazy_build => 1);
17 sub _build_action_viewport_map {
24 sub _build_action_viewport_args {
28 sub base :Action :CaptureArgs(0) {
32 #XXX candidate for futre optimization, should cache reader?
35 my $model = $c->model( $self->model_name );
36 my $attr = $model->meta->find_attribute_by_name( $self->collection_name );
37 my $reader = $attr->get_read_method;
38 return $model->$reader;
41 sub list :Chained('base') :PathPart('') :Args(0) {
43 $c->forward(basic_page => [{ collection => $self->get_collection($c) }]);
46 sub object :Chained('base') :PathPart('id') :CaptureArgs(1) {
47 my ($self, $c, $key) = @_;
48 my $object :Stashed = $self->get_collection($c)->find($key);
49 confess "Object? what object?" unless $object; # should be a 404.
52 sub view :Chained('object') :Args(0) {
55 $c->forward(basic_page => [{object => $object}]);
58 sub basic_page : Private {
59 my ($self, $c, $vp_args) = @_;
60 my $action_name = $c->stack->[-2]->name;
61 return $self->push_viewport
63 $self->action_viewport_map->{$action_name},
65 %{ $self->action_viewport_args->{$action_name} || {} },