Commit | Line | Data |
89b70ba7 |
1 | package Reaction::UI::Controller::Collection::CRUD; |
2 | |
89b70ba7 |
3 | use base 'Reaction::UI::Controller::Collection'; |
4 | use Reaction::Class; |
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) = @_; |
89b70ba7 |
48 | return $self->redirect_to |
49 | ( $c, 'update', [ @{$c->req->captures}, $result->id ] ); |
50 | } |
51 | |
52f292b2 |
52 | sub on_create_close_callback { |
53 | my($self, $c, $vp) = @_; |
747a1feb |
54 | $self->redirect_to( $c, 'list' ); |
52f292b2 |
55 | } |
56 | |
52f292b2 |
57 | sub 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 |
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 | |
931cbc8d |
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 | |
aee256be |
85 | sub 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 | |
101 | 1; |
7d3fe0d2 |
102 | |
103 | __END__ |
104 | |
105 | =head1 NAME |
106 | |
e516f7e6 |
107 | Reaction::UI::Controller::Collection::CRUD - Basic CRUD functionality for Reaction::InterfaceModel data |
7d3fe0d2 |
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 | |
931cbc8d |
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 | |
7d3fe0d2 |
135 | =head1 METHODS |
136 | |
137 | =head2 get_model_action $c, $action_name, $target_im |
138 | |
931cbc8d |
139 | DEPRECATED. Get an instance of the C<$action_name> |
7d3fe0d2 |
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 | |
7d3fe0d2 |
144 | =head2 basic_model_action $c, \%vp_args |
145 | |
931cbc8d |
146 | DEPRECTAED extension to C<basic_page> which automatically instantiates an |
7d3fe0d2 |
147 | L<InterfaceModel::Action|Reaction::InterfaceModel::Action> with the right |
148 | data target using C<get_model_action> |
149 | |
931cbc8d |
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 | |
7d3fe0d2 |
155 | =head2 _build_action_viewport_map |
156 | |
931cbc8d |
157 | Map C<list> to L<ListView|Reaction::UI::ViewPort::ListView>. |
b3cb974a |
158 | |
159 | =head2 _build_default_member_actions |
7d3fe0d2 |
160 | |
b3cb974a |
161 | Add C<update> and C<delete> to the list of default actions. |
7d3fe0d2 |
162 | |
b3cb974a |
163 | =head2 _build_default_collection_actions |
164 | |
165 | Add C<create> and C<delete_all> to the list of default actions. |
7d3fe0d2 |
166 | |
167 | =head1 ACTIONS |
168 | |
169 | =head2 create |
170 | |
931cbc8d |
171 | Chained to C<base>. See L<Reaction::UI::Controller::Role::Action::Create> |
7d3fe0d2 |
172 | |
173 | =head2 delete_all |
174 | |
931cbc8d |
175 | Chained to C<base>. See L<Reaction::UI::Controller::Role::Action::DeleteAll> |
7d3fe0d2 |
176 | |
177 | =head2 update |
178 | |
931cbc8d |
179 | Chained to C<object>. See L<Reaction::UI::Controller::Role::Action::Update> |
7d3fe0d2 |
180 | |
181 | =head2 delete |
182 | |
931cbc8d |
183 | Chained to C<object>. See L<Reaction::UI::Controller::Role::Action::Delete> |
7d3fe0d2 |
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 |