1 package Reaction::UI::CRUDController;
5 use base 'Reaction::UI::Controller';
8 use aliased 'Reaction::UI::ViewPort::ListView';
9 use aliased 'Reaction::UI::ViewPort::ActionForm';
10 use aliased 'Reaction::UI::ViewPort::ObjectView';
12 has 'model_name' => (isa => 'Str', is => 'rw', required => 1);
13 has 'collection_name' => (isa => 'Str', is => 'rw', required => 1);
15 has action_viewport_map => (isa => 'HashRef', is => 'rw', lazy_build => 1);
16 has action_viewport_args => (isa => 'HashRef', is => 'rw', lazy_build => 1);
18 sub _build_action_viewport_map {
25 delete_all => ActionForm,
29 sub _build_action_viewport_args {
32 { action_prototypes =>
33 [ { label => 'Create', action => sub {
34 [ '', 'create', $_[1]->req->captures ] } },
35 { label => 'Delete all', action => sub {
36 [ '', 'delete_all', $_[1]->req->captures ] } },
39 { action_prototypes =>
40 [ { label => 'View', action => sub {
41 [ '', 'view', [ @{$_[1]->req->captures}, $_[0]->__id ] ] } },
42 { label => 'Edit', action => sub {
43 [ '', 'update', [ @{$_[1]->req->captures}, $_[0]->__id ] ] } },
44 { label => 'Delete', action => sub {
45 [ '', 'delete', [ @{$_[1]->req->captures}, $_[0]->__id ] ] } },
52 sub base :Action :CaptureArgs(0) {
56 #XXX candidate for futre optimization, should cache reader?
59 my $model = $c->model( $self->model_name );
60 my $attr = $model->meta->find_attribute_by_name( $self->collection_name );
61 my $reader = $attr->get_read_method;
62 return $model->$reader;
65 sub get_model_action {
66 my ($self, $c, $name, $target) = @_;
68 if ($target->can('action_for')) {
69 return $target->action_for($name, ctx => $c);
72 #can we please kill this already?
73 my $model_name = "Action::${name}".$self->model_name;
74 my $model = $c->model($model_name);
75 confess "no such Model $model_name" unless $model;
76 return $model->new(target_model => $target, ctx => $c);
81 sub list :Chained('base') :PathPart('') :Args(0) {
83 $c->forward(basic_page => { collection => $self->get_collection($c) });
86 sub create :Chained('base') :PathPart('create') :Args(0) {
89 next_action => 'list',
90 on_apply_callback => sub { $self->after_create_callback($c => @_); },
92 $c->forward( basic_model_action => $vp_args);
95 sub delete_all :Chained('base') :PathPart('delete_all') :Args(0) {
97 $c->forward(basic_model_action => { next_action => 'list'});
100 sub after_create_callback {
101 my ($self, $c, $vp, $result) = @_;
102 return $self->redirect_to
103 ( $c, 'update', [ @{$c->req->captures}, $result->id ] );
108 sub object :Chained('base') :PathPart('id') :CaptureArgs(1) {
109 my ($self, $c, $key) = @_;
110 my $object :Stashed = $self->get_collection($c)->find($key);
111 confess "Object? what object?" unless $object; # should be a 404.
114 sub update :Chained('object') :Args(0) {
116 #this needs a better solution. currently thinking about it
117 my @cap = @{$c->req->captures};
118 pop(@cap); # object id
119 my $vp_args = { next_action => [ $self, 'redirect_to', 'list', \@cap ]};
120 $c->forward(basic_model_action => $vp_args);
123 sub delete :Chained('object') :Args(0) {
125 #this needs a better solution. currently thinking about it
126 my @cap = @{$c->req->captures};
127 pop(@cap); # object id
128 my $vp_args = { next_action => [ $self, 'redirect_to', 'list', \@cap ]};
129 $c->forward(basic_model_action => $vp_args);
132 sub view :Chained('object') :Args(0) {
135 $c->forward(basic_page => {object => $object});
141 sub basic_model_action :Private {
142 my ($self, $c, $vp_args) = @_;
144 my $target = exists $c->stash->{object} ?
145 $c->stash->{object} : $self->get_collection($c);
147 my $cat_action_name = $c->stack->[-2]->name;
148 my $im_action_name = join('', (map{ uc } split('_', $cat_action_name)));
149 return $self->push_viewport
151 $self->action_viewport_map->{$cat_action_name},
152 action => $self->get_model_action($c, $im_action_name, $target),
154 %{ $self->action_viewport_args->{$cat_action_name} || {} },
158 sub basic_page : Private {
159 my ($self, $c, $vp_args) = @_;
160 my $action_name = $c->stack->[-2]->name;
161 return $self->push_viewport
163 $self->action_viewport_map->{$action_name},
165 %{ $self->action_viewport_args->{$action_name} || {} },