Commit | Line | Data |
89b70ba7 |
1 | package Reaction::UI::Controller::Collection::CRUD; |
2 | |
26fa3b8a |
3 | use Moose; |
90bcd4d7 |
4 | BEGIN { extends 'Reaction::UI::Controller::Collection'; } |
89b70ba7 |
5 | |
8cc0103d |
6 | use 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 | |
17 | with( |
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 | |
24 | around _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 |
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/ ]; |
89b70ba7 |
37 | } |
38 | |
931cbc8d |
39 | ##DEFAULT CALLBACKS |
52f292b2 |
40 | |
41 | sub on_delete_all_close_callback { |
747a1feb |
42 | my($self, $c) = @_; |
43 | $self->redirect_to($c, 'list'); |
89b70ba7 |
44 | } |
45 | |
931cbc8d |
46 | sub 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 |
57 | sub on_create_close_callback { |
58 | my($self, $c, $vp) = @_; |
747a1feb |
59 | $self->redirect_to( $c, 'list' ); |
52f292b2 |
60 | } |
61 | |
52f292b2 |
62 | sub 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 |
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 | |
931cbc8d |
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 | |
aee256be |
90 | sub 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 | |
106 | 1; |
7d3fe0d2 |
107 | |
108 | __END__ |
109 | |
110 | =head1 NAME |
111 | |
e516f7e6 |
112 | Reaction::UI::Controller::Collection::CRUD - Basic CRUD functionality for Reaction::InterfaceModel data |
7d3fe0d2 |
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 | |
931cbc8d |
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 | |
7d3fe0d2 |
140 | =head1 METHODS |
141 | |
142 | =head2 get_model_action $c, $action_name, $target_im |
143 | |
931cbc8d |
144 | DEPRECATED. Get an instance of the C<$action_name> |
7d3fe0d2 |
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 | |
7d3fe0d2 |
149 | =head2 basic_model_action $c, \%vp_args |
150 | |
931cbc8d |
151 | DEPRECTAED extension to C<basic_page> which automatically instantiates an |
7d3fe0d2 |
152 | L<InterfaceModel::Action|Reaction::InterfaceModel::Action> with the right |
153 | data target using C<get_model_action> |
154 | |
931cbc8d |
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 | |
7d3fe0d2 |
160 | =head2 _build_action_viewport_map |
161 | |
931cbc8d |
162 | Map C<list> to L<ListView|Reaction::UI::ViewPort::ListView>. |
b3cb974a |
163 | |
164 | =head2 _build_default_member_actions |
7d3fe0d2 |
165 | |
b3cb974a |
166 | Add C<update> and C<delete> to the list of default actions. |
7d3fe0d2 |
167 | |
b3cb974a |
168 | =head2 _build_default_collection_actions |
169 | |
170 | Add C<create> and C<delete_all> to the list of default actions. |
7d3fe0d2 |
171 | |
172 | =head1 ACTIONS |
173 | |
174 | =head2 create |
175 | |
931cbc8d |
176 | Chained to C<base>. See L<Reaction::UI::Controller::Role::Action::Create> |
7d3fe0d2 |
177 | |
178 | =head2 delete_all |
179 | |
931cbc8d |
180 | Chained to C<base>. See L<Reaction::UI::Controller::Role::Action::DeleteAll> |
7d3fe0d2 |
181 | |
182 | =head2 update |
183 | |
931cbc8d |
184 | Chained to C<object>. See L<Reaction::UI::Controller::Role::Action::Update> |
7d3fe0d2 |
185 | |
186 | =head2 delete |
187 | |
931cbc8d |
188 | Chained to C<object>. See L<Reaction::UI::Controller::Role::Action::Delete> |
7d3fe0d2 |
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 |