21faa4a3ee4f617d3a0128b2d600fc76d916987c
[catagits/Reaction.git] / lib / Reaction / UI / Controller / Collection / CRUD.pm
1 package Reaction::UI::Controller::Collection::CRUD;
2
3 use Moose;
4 BEGIN { extends 'Reaction::UI::Controller::Collection'; }
5
6 use aliased 'Reaction::UI::ViewPort::ListView';
7
8 __PACKAGE__->config(
9   action => {
10     create => { Chained => 'base', },
11     delete_all => { Chained => 'base', },
12     update => { Chained => 'object', },
13     delete => { Chained => 'object', },
14   },
15 );
16
17 with(
18   'Reaction::UI::Controller::Role::Action::Create',
19   'Reaction::UI::Controller::Role::Action::Update',
20   'Reaction::UI::Controller::Role::Action::Delete',
21   'Reaction::UI::Controller::Role::Action::DeleteAll',
22 );
23
24 around _build_action_viewport_map => sub {
25   my $orig = shift;
26   my $map = shift->$orig( @_ );
27   $map->{list} = ListView;
28   return $map;
29 };
30
31 sub _build_default_member_actions {
32   [ @{shift->next::method(@_)}, qw/update delete/ ];
33 }
34
35 sub _build_default_collection_actions {
36   [ @{shift->next::method(@_)}, qw/create delete_all/ ];
37 }
38
39 ##DEFAULT CALLBACKS
40
41 sub on_delete_all_close_callback {
42   my($self, $c) = @_;
43   $self->redirect_to($c, 'list');
44 }
45
46 sub on_create_apply_callback {
47   my ($self, $c, $vp, $result) = @_;
48   if( $self->can('after_create_callback') ){
49     $c->log->debug("'after_create_callback' has been replaced with 'on_create_apply_callback' and is deprecated.");
50     shift @_;
51     return $self->after_create_callback(@_);
52   }
53   return $self->redirect_to
54     ( $c, 'update', [ @{$c->req->captures}, $result->id ] );
55 }
56
57 sub on_create_close_callback {
58   my($self, $c, $vp) = @_;
59   $self->redirect_to( $c, 'list' );
60 }
61
62 sub on_update_close_callback {
63   my($self, $c) = @_;
64   #this needs a better solution. currently thinking about it
65   my @cap = @{$c->req->captures};
66   pop(@cap); # object id
67   $self->redirect_to($c, 'list', \@cap);
68 }
69
70 sub on_delete_close_callback {
71   my($self, $c) = @_;
72   #this needs a better solution. currently thinking about it
73   my @cap = @{$c->req->captures};
74   pop(@cap); # object id
75   $self->redirect_to($c, 'list', \@cap);
76 }
77
78 #### DEPRECATED METHODS
79
80 sub get_model_action {
81   my ($self, $c, $name, $target) = @_;
82   if( $c->debug ){
83     my ($package,undef,$line,$sub_name,@rest) = caller(1);
84     my $message = "The method 'get_model_action', called from sub '${sub_name}' in package ${package} at line ${line} is deprecated.";
85     $c->log->debug( $message );
86   }
87   return $target->action_for($name, ctx => $c);
88 }
89
90 sub basic_model_action {
91   my ($self, $c, $vp_args) = @_;
92   if( $c->debug ){
93     my ($package,undef,$line,$sub_name,@rest) = caller(1);
94     my $message = "The method 'basic_model_action', called from sub '${sub_name}' in package ${package} at line ${line} is deprecated.";
95     $c->log->debug( $message );
96   }
97   my $stash = $c->stash;
98   my $target = delete $vp_args->{target};
99   $target ||= ($stash->{object} || $stash->{collection} || $self->get_collection($c));
100
101   my $action_name = join('', map{ ucfirst } split('_', $c->stack->[-1]->name));
102   my $model = $self->get_model_action($c, $action_name, $target);
103   return $self->basic_page($c, { model => $model, %{$vp_args||{}} });
104 }
105
106 1;
107
108 __END__
109
110 =head1 NAME
111
112 Reaction::UI::Controller::Collection::CRUD - Basic CRUD functionality for Reaction::InterfaceModel data
113
114 =head1 DESCRIPTION
115
116 Controller class which extends L<Reaction::UI::Controller::Collection> to 
117 provide basic Create / Update / Delete / DeleteAll actions.
118
119 Building on the base of the Collection controller this controller allows you to
120 easily create complex and highly flexible CRUD functionality for your 
121 InterfaceModel models by providing a simple way to render and process your
122 custom InterfaceModel Actions and customize built-ins.
123
124 =head1 ROLES CONSUMED
125
126 This role also consumes the following roles:
127
128 =over4
129
130 =item L<Reaction::UI::Controller::Role::Action::Create>
131
132 =item L<Reaction::UI::Controller::Role::Action::Update>
133
134 =item L<Reaction::UI::Controller::Role::Action::Delete>
135
136 =item L<Reaction::UI::Controller::Role::Action::DeleteAll>
137
138 =back
139
140 =head1 METHODS
141
142 =head2 get_model_action $c, $action_name, $target_im
143
144 DEPRECATED. Get an instance of the C<$action_name> 
145 L<InterfaceModel::Action|Reaction::InterfaceModel::Action> for model C<$target>
146 This action is suitable for passing to an 
147 C<Action|Reaction::UI::ViewPort::Action> viewport
148
149 =head2 basic_model_action $c, \%vp_args
150
151 DEPRECTAED extension to C<basic_page> which automatically instantiates an 
152 L<InterfaceModel::Action|Reaction::InterfaceModel::Action> with the right
153 data target using C<get_model_action>
154
155 =head2 after_create_callback $c, $vp, $result
156
157 When a <create> action is applied, move the user to the new object's,
158 C<update> page.
159
160 =head2 _build_action_viewport_map
161
162 Map C<list> to L<ListView|Reaction::UI::ViewPort::ListView>.
163
164 =head2 _build_default_member_actions
165
166 Add C<update> and C<delete> to the list of default actions.
167
168 =head2 _build_default_collection_actions
169
170 Add C<create> and C<delete_all> to the list of default actions.
171
172 =head1 ACTIONS
173
174 =head2 create
175
176 Chained to C<base>. See L<Reaction::UI::Controller::Role::Action::Create>
177
178 =head2 delete_all
179
180 Chained to C<base>. See L<Reaction::UI::Controller::Role::Action::DeleteAll>
181
182 =head2 update
183
184 Chained to C<object>. See L<Reaction::UI::Controller::Role::Action::Update>
185
186 =head2 delete
187
188 Chained to C<object>. See L<Reaction::UI::Controller::Role::Action::Delete>
189
190 =head1 SEE ALSO
191
192 L<Reaction::UI::Controller::Collection>, L<Reaction::UI::Controller>
193
194 =head1 AUTHORS
195
196 See L<Reaction::Class> for authors.
197
198 =head1 LICENSE
199
200 See L<Reaction::Class> for the license.
201
202 =cut