aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Reaction/UI/CRUDController.pm
blob: 554d588088c0f76a7471b447d6adf9fe2fa08a2a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package Reaction::UI::CRUDController;

use strict;
use warnings;
use base 'Reaction::UI::Controller';
use Reaction::Class;

use aliased 'Reaction::UI::ViewPort::ListView';
use aliased 'Reaction::UI::ViewPort::ActionForm';
use aliased 'Reaction::UI::ViewPort::ObjectView';

has 'model_name'      => (isa => 'Str', is => 'rw', required => 1);
has 'collection_name' => (isa => 'Str', is => 'rw', required => 1);

has action_viewport_map  => (isa => 'HashRef', is => 'rw', lazy_build => 1);
has action_viewport_args => (isa => 'HashRef', is => 'rw', lazy_build => 1);

sub _build_action_viewport_map {
  return {
          list       => ListView,
          view       => ObjectView,
          create     => ActionForm,
          update     => ActionForm,
          delete     => ActionForm,
          delete_all => ActionForm,
         };
}

sub _build_action_viewport_args {
  my $self = shift;
  return { list =>
           { action_prototypes =>
             [ { label => 'Create', action => sub {
                   [ '', 'create',    $_[1]->req->captures ] } },
               { label => 'Delete all', action => sub {
                   [ '', 'delete_all', $_[1]->req->captures ] } },
             ],
             Entity =>
             { action_prototypes =>
               [ { label => 'View', action => sub {
                     [ '', 'view', [ @{$_[1]->req->captures},   $_[0]->__id ] ] } },
                 { label => 'Edit', action => sub {
                     [ '', 'update', [ @{$_[1]->req->captures}, $_[0]->__id ] ] } },
                 { label => 'Delete', action => sub {
                     [ '', 'delete', [ @{$_[1]->req->captures}, $_[0]->__id ] ] } },
               ],
             },
           },
         };
}

sub base :Action :CaptureArgs(0) {
  my ($self, $c) = @_;
}

#XXX candidate for futre optimization
sub get_collection {
  my ($self, $c) = @_;
  my $model = $c->model( $self->model_name );
  my $attr  = $model->meta->find_attribute_by_name( $self->collection_name );
  my $reader = $attr->get_read_method;
  return $model->$reader;
}

sub get_model_action {
  my ($self, $c, $name, $target) = @_;

  if ($target->can('action_for')) {
    return $target->action_for($name, ctx => $c);
  }

  #can we please kill this already?
  my $model_name = "Action::${name}".$self->model_name;
  my $model = $c->model($model_name);
  confess "no such Model $model_name" unless $model;
  return $model->new(target_model => $target, ctx => $c);
}

sub list :Chained('base') :PathPart('') :Args(0) {
  my ($self, $c) = @_;

  $self->push_viewport(
                       $self->action_viewport_map->{list},
                       %{ $self->action_viewport_args->{list} || {} },
                       collection => $self->get_collection($c)
                      );
}

sub create :Chained('base') :PathPart('create') :Args(0) {
  my ($self, $c) = @_;
  my $action = $self->get_model_action($c, 'Create', $self->get_collection($c));
  $self->push_viewport
    (
     $self->action_viewport_map->{create},
     %{ $self->action_viewport_args->{create} || {} },
     action => $action,
     next_action => 'list',
     on_apply_callback => sub { $self->after_create_callback($c => @_); },
    );
}

sub delete_all :Chained('base') :PathPart('delete_all') :Args(0) {
  my ($self, $c) = @_;
  my $action = $self->get_model_action($c, 'DeleteAll', $self->get_collection($c));
  $self->push_viewport
    (
     $self->action_viewport_map->{delete_all},
     %{ $self->action_viewport_args->{delete_all} || {} },
     action => $action,
     next_action => 'list',
    );
}

sub after_create_callback {
  my ($self, $c, $vp, $result) = @_;
  return $self->redirect_to
    ( $c, 'update', [ @{$c->req->captures}, $result->id ] );
}

sub object :Chained('base') :PathPart('id') :CaptureArgs(1) {
  my ($self, $c, $key) = @_;
  my $object :Stashed = $self->get_collection($c)->find($key);
  confess "Object? what object?" unless $object; # should be a 404.
}

sub update :Chained('object') :Args(0) {
  my ($self, $c) = @_;
  my $object :Stashed;
  my $action = $self->get_model_action($c, 'Update', $object);
  my @cap = @{$c->req->captures};
  pop(@cap); # object id
  $self->push_viewport
    (
     $self->action_viewport_map->{update},
     %{ $self->action_viewport_args->{update} || {} },
     action => $action,
     next_action => [ $self, 'redirect_to', 'list', \@cap ]
  );
}

sub delete :Chained('object') :Args(0) {
  my ($self, $c) = @_;
  my $object :Stashed;
  my $action = $self->get_model_action($c, 'Delete', $object);
  my @cap = @{$c->req->captures};
  pop(@cap); # object id
  $self->push_viewport
    (
     $self->action_viewport_map->{delete},
     %{ $self->action_viewport_args->{delete} || {} },
     action => $action,
     next_action => [ $self, 'redirect_to', 'list', \@cap ]
  );
}

sub view :Chained('object') :Args(0) {
  my ($self, $c) = @_;
  my $object :Stashed;
  my @cap = @{$c->req->captures};
  pop(@cap); # object id
  $self->push_viewport
    (
     $self->action_viewport_map->{view},
     %{ $self->action_viewport_args->{view} || {} },
     object => $object,
    );
}

1;