fc7585eb08386a61ae641d8c2c30ae53ed6a761b
[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   return $self->redirect_to
49     ( $c, 'update', [ @{$c->req->captures}, $result->id ] );
50 }
51
52 sub on_create_close_callback {
53   my($self, $c, $vp) = @_;
54   $self->redirect_to( $c, 'list' );
55 }
56
57 sub on_update_close_callback {
58   my($self, $c) = @_;
59   #this needs a better solution. currently thinking about it
60   my @cap = @{$c->req->captures};
61   pop(@cap); # object id
62   $self->redirect_to($c, 'list', \@cap);
63 }
64
65 sub 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
73 #### DEPRECATED METHODS
74
75 sub 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
85 sub basic_model_action {
86   my ($self, $c, $vp_args) = @_;
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   }
92   my $stash = $c->stash;
93   my $target = delete $vp_args->{target};
94   $target ||= ($stash->{object} || $stash->{collection} || $self->get_collection($c));
95
96   my $action_name = join('', map{ ucfirst } split('_', $c->stack->[-1]->name));
97   my $model = $self->get_model_action($c, $action_name, $target);
98   return $self->basic_page($c, { model => $model, %{$vp_args||{}} });
99 }
100
101 1;
102
103 __END__
104
105 =head1 NAME
106
107 Reaction::UI::Controller::Collection::CRUD - Basic CRUD functionality for Reaction::InterfaceModel data
108
109 =head1 DESCRIPTION
110
111 Controller class which extends L<Reaction::UI::Controller::Collection> to 
112 provide basic Create / Update / Delete / DeleteAll actions.
113
114 Building on the base of the Collection controller this controller allows you to
115 easily create complex and highly flexible CRUD functionality for your 
116 InterfaceModel models by providing a simple way to render and process your
117 custom InterfaceModel Actions and customize built-ins.
118
119 =head1 ROLES CONSUMED
120
121 This 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
135 =head1 METHODS
136
137 =head2 get_model_action $c, $action_name, $target_im
138
139 DEPRECATED. Get an instance of the C<$action_name> 
140 L<InterfaceModel::Action|Reaction::InterfaceModel::Action> for model C<$target>
141 This action is suitable for passing to an 
142 C<Action|Reaction::UI::ViewPort::Action> viewport
143
144 =head2 basic_model_action $c, \%vp_args
145
146 DEPRECTAED extension to C<basic_page> which automatically instantiates an 
147 L<InterfaceModel::Action|Reaction::InterfaceModel::Action> with the right
148 data target using C<get_model_action>
149
150 =head2 after_create_callback $c, $vp, $result
151
152 When a <create> action is applied, move the user to the new object's,
153 C<update> page.
154
155 =head2 _build_action_viewport_map
156
157 Map C<list> to L<ListView|Reaction::UI::ViewPort::ListView>.
158
159 =head2 _build_default_member_actions
160
161 Add C<update> and C<delete> to the list of default actions.
162
163 =head2 _build_default_collection_actions
164
165 Add C<create> and C<delete_all> to the list of default actions.
166
167 =head1 ACTIONS
168
169 =head2 create
170
171 Chained to C<base>. See L<Reaction::UI::Controller::Role::Action::Create>
172
173 =head2 delete_all
174
175 Chained to C<base>. See L<Reaction::UI::Controller::Role::Action::DeleteAll>
176
177 =head2 update
178
179 Chained to C<object>. See L<Reaction::UI::Controller::Role::Action::Update>
180
181 =head2 delete
182
183 Chained to C<object>. See L<Reaction::UI::Controller::Role::Action::Delete>
184
185 =head1 SEE ALSO
186
187 L<Reaction::UI::Controller::Collection>, L<Reaction::UI::Controller>
188
189 =head1 AUTHORS
190
191 See L<Reaction::Class> for authors.
192
193 =head1 LICENSE
194
195 See L<Reaction::Class> for the license.
196
197 =cut