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