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