do not include .git directory
[catagits/Reaction.git] / lib / Reaction / UI / Controller / Collection / CRUD.pm
CommitLineData
89b70ba7 1package Reaction::UI::Controller::Collection::CRUD;
2
26fa3b8a 3use Moose;
90bcd4d7 4BEGIN { extends 'Reaction::UI::Controller::Collection'; }
89b70ba7 5
8cc0103d 6use aliased 'Reaction::UI::ViewPort::ListView';
89b70ba7 7
931cbc8d 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
17with(
18 'Reaction::UI::Controller::Role::Action::Create',
931cbc8d 19 'Reaction::UI::Controller::Role::Action::Update',
20 'Reaction::UI::Controller::Role::Action::Delete',
21 'Reaction::UI::Controller::Role::Action::DeleteAll',
22);
23
24around _build_action_viewport_map => sub {
25 my $orig = shift;
26 my $map = shift->$orig( @_ );
27 $map->{list} = ListView;
89b70ba7 28 return $map;
931cbc8d 29};
89b70ba7 30
37728bba 31sub _build_default_member_actions {
32 [ @{shift->next::method(@_)}, qw/update delete/ ];
33}
34
35sub _build_default_collection_actions {
36 [ @{shift->next::method(@_)}, qw/create delete_all/ ];
89b70ba7 37}
38
931cbc8d 39##DEFAULT CALLBACKS
52f292b2 40
41sub on_delete_all_close_callback {
747a1feb 42 my($self, $c) = @_;
43 $self->redirect_to($c, 'list');
89b70ba7 44}
45
931cbc8d 46sub on_create_apply_callback {
747a1feb 47 my ($self, $c, $vp, $result) = @_;
c528be7a 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 }
89b70ba7 53 return $self->redirect_to
54 ( $c, 'update', [ @{$c->req->captures}, $result->id ] );
55}
56
52f292b2 57sub on_create_close_callback {
58 my($self, $c, $vp) = @_;
747a1feb 59 $self->redirect_to( $c, 'list' );
52f292b2 60}
61
52f292b2 62sub on_update_close_callback {
747a1feb 63 my($self, $c) = @_;
89b70ba7 64 #this needs a better solution. currently thinking about it
747a1feb 65 my @cap = @{$c->req->captures};
89b70ba7 66 pop(@cap); # object id
52f292b2 67 $self->redirect_to($c, 'list', \@cap);
89b70ba7 68}
69
35dd77ad 70sub 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
931cbc8d 78#### DEPRECATED METHODS
79
80sub 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
aee256be 90sub basic_model_action {
89b70ba7 91 my ($self, $c, $vp_args) = @_;
931cbc8d 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 }
893bf4f1 97 my $stash = $c->stash;
98 my $target = delete $vp_args->{target};
99 $target ||= ($stash->{object} || $stash->{collection} || $self->get_collection($c));
89b70ba7 100
7d3fe0d2 101 my $action_name = join('', map{ ucfirst } split('_', $c->stack->[-1]->name));
102 my $model = $self->get_model_action($c, $action_name, $target);
5d86015d 103 return $self->basic_page($c, { model => $model, %{$vp_args||{}} });
89b70ba7 104}
105
1061;
7d3fe0d2 107
108__END__
109
110=head1 NAME
111
e516f7e6 112Reaction::UI::Controller::Collection::CRUD - Basic CRUD functionality for Reaction::InterfaceModel data
7d3fe0d2 113
114=head1 DESCRIPTION
115
116Controller class which extends L<Reaction::UI::Controller::Collection> to
117provide basic Create / Update / Delete / DeleteAll actions.
118
119Building on the base of the Collection controller this controller allows you to
120easily create complex and highly flexible CRUD functionality for your
121InterfaceModel models by providing a simple way to render and process your
122custom InterfaceModel Actions and customize built-ins.
123
931cbc8d 124=head1 ROLES CONSUMED
125
126This 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
7d3fe0d2 140=head1 METHODS
141
142=head2 get_model_action $c, $action_name, $target_im
143
931cbc8d 144DEPRECATED. Get an instance of the C<$action_name>
7d3fe0d2 145L<InterfaceModel::Action|Reaction::InterfaceModel::Action> for model C<$target>
146This action is suitable for passing to an
147C<Action|Reaction::UI::ViewPort::Action> viewport
148
7d3fe0d2 149=head2 basic_model_action $c, \%vp_args
150
931cbc8d 151DEPRECTAED extension to C<basic_page> which automatically instantiates an
7d3fe0d2 152L<InterfaceModel::Action|Reaction::InterfaceModel::Action> with the right
153data target using C<get_model_action>
154
931cbc8d 155=head2 after_create_callback $c, $vp, $result
156
157When a <create> action is applied, move the user to the new object's,
158C<update> page.
159
7d3fe0d2 160=head2 _build_action_viewport_map
161
931cbc8d 162Map C<list> to L<ListView|Reaction::UI::ViewPort::ListView>.
b3cb974a 163
164=head2 _build_default_member_actions
7d3fe0d2 165
b3cb974a 166Add C<update> and C<delete> to the list of default actions.
7d3fe0d2 167
b3cb974a 168=head2 _build_default_collection_actions
169
170Add C<create> and C<delete_all> to the list of default actions.
7d3fe0d2 171
172=head1 ACTIONS
173
174=head2 create
175
931cbc8d 176Chained to C<base>. See L<Reaction::UI::Controller::Role::Action::Create>
7d3fe0d2 177
178=head2 delete_all
179
931cbc8d 180Chained to C<base>. See L<Reaction::UI::Controller::Role::Action::DeleteAll>
7d3fe0d2 181
182=head2 update
183
931cbc8d 184Chained to C<object>. See L<Reaction::UI::Controller::Role::Action::Update>
7d3fe0d2 185
186=head2 delete
187
931cbc8d 188Chained to C<object>. See L<Reaction::UI::Controller::Role::Action::Delete>
7d3fe0d2 189
190=head1 SEE ALSO
191
192L<Reaction::UI::Controller::Collection>, L<Reaction::UI::Controller>
193
194=head1 AUTHORS
195
196See L<Reaction::Class> for authors.
197
198=head1 LICENSE
199
200See L<Reaction::Class> for the license.
201
202=cut