| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Catalyst::Helper::Controller::Scaffold::Mason; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
22244
|
use strict; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
41
|
|
|
4
|
1
|
|
|
1
|
|
859
|
use Path::Class; |
|
|
1
|
|
|
|
|
839508
|
|
|
|
1
|
|
|
|
|
226
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
Catalyst::Helper::Controller::Scaffold::Mason - Helper for Scaffolding |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# Imagine you want to generate a scaffolding controller MyApp::C::SomeTable |
|
15
|
|
|
|
|
|
|
# for a CDBI table class MyApp::M::CDBI::SomeTable |
|
16
|
|
|
|
|
|
|
script/myapp_create.pl controller SomeTable Scaffold::Mason CDBI::SomeTable |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
Helper for Scaffolding. |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
Templates are Mason so you'll need a Mason View Component and a forward in |
|
23
|
|
|
|
|
|
|
your end action too, or the DefaultEnd plugin. |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
Note that you have to add these lines to your CDBI class... |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
use Class::DBI::AsForm; |
|
28
|
|
|
|
|
|
|
use Class::DBI::FromForm; |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
for L<Catalyst::Model::CDBI> you can do that by adding this |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
additional_base_classes => [qw/Class::DBI::AsForm Class::DBI::FromForm/], |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
to the component config. Also, change your application class like this: |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
use Catalyst qw/-Debug FormValidator/; |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head1 METHODS |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=over 4 |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=item mk_compclass |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
Does the actual work. Called from helper api. |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=cut |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub mk_compclass { |
|
49
|
0
|
|
|
0
|
1
|
|
my ( $self, $helper, $table_class ) = @_; |
|
50
|
0
|
|
|
|
|
|
$helper->{table_class} = $helper->{app} . '::M::' . $table_class; |
|
51
|
0
|
|
|
|
|
|
my $file = $helper->{file}; |
|
52
|
0
|
|
|
|
|
|
my $dir = dir( $helper->{base}, 'root', $helper->{prefix} ); |
|
53
|
0
|
|
|
|
|
|
$helper->mk_dir($dir); |
|
54
|
0
|
|
|
|
|
|
$helper->render_file( 'compclass', $file ); |
|
55
|
0
|
|
|
|
|
|
$helper->render_file( 'add', file( $dir, 'add.mhtml' ) ); |
|
56
|
0
|
|
|
|
|
|
$helper->render_file( 'edit', file( $dir, 'edit.mhtml' ) ); |
|
57
|
0
|
|
|
|
|
|
$helper->render_file( 'list', file( $dir, 'list.mhtml' ) ); |
|
58
|
0
|
|
|
|
|
|
$helper->render_file( 'view', file( $dir, 'view.mhtml' ) ); |
|
59
|
|
|
|
|
|
|
} |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=back |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=head1 AUTHOR |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
Sebastian Riedel |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=head1 LICENSE |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
This library is free software . You can redistribute it and/or modify |
|
70
|
|
|
|
|
|
|
it under the same terms as perl itself. |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=cut |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
1; |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
__DATA__ |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
__compclass__ |
|
79
|
|
|
|
|
|
|
package [% class %]; |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
use strict; |
|
82
|
|
|
|
|
|
|
use base 'Catalyst::Base'; |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head1 NAME |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
[% class %] - Scaffolding Controller Component |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
See L<[% app %]> |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
Scaffolding Controller Component. |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head1 METHODS |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=over 4 |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=item add |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
Sets a template. |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=cut |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub add : Local { |
|
107
|
|
|
|
|
|
|
my ( $self, $c ) = @_; |
|
108
|
|
|
|
|
|
|
$c->stash->{template} = '[% prefix %]/add.mhtml'; |
|
109
|
|
|
|
|
|
|
} |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=item default |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
Forwards to list. |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=cut |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub default : Private { |
|
118
|
|
|
|
|
|
|
my ( $self, $c ) = @_; |
|
119
|
|
|
|
|
|
|
$c->forward('list'); |
|
120
|
|
|
|
|
|
|
} |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=item destroy |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Destroys a row and forwards to list. |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=cut |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub destroy : Local { |
|
129
|
|
|
|
|
|
|
my ( $self, $c, $id ) = @_; |
|
130
|
|
|
|
|
|
|
[% table_class %]->retrieve($id)->delete; |
|
131
|
|
|
|
|
|
|
$c->forward('list'); |
|
132
|
|
|
|
|
|
|
} |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=item do_add |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Adds a new row to the table and forwards to list. |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=cut |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
sub do_add : Local { |
|
141
|
|
|
|
|
|
|
my ( $self, $c ) = @_; |
|
142
|
|
|
|
|
|
|
$c->form( optional => [ [% table_class %]->columns ] ); |
|
143
|
|
|
|
|
|
|
if ($c->form->has_missing) { |
|
144
|
|
|
|
|
|
|
$c->stash->{message}='You have to fill in all fields. '. |
|
145
|
|
|
|
|
|
|
'The following are missing: <b>'. |
|
146
|
|
|
|
|
|
|
join(', ',$c->form->missing()).'</b>'; |
|
147
|
|
|
|
|
|
|
} elsif ($c->form->has_invalid) { |
|
148
|
|
|
|
|
|
|
$c->stash->{message}='Some fields are correctly filled in. '. |
|
149
|
|
|
|
|
|
|
'The following are invalid: <b>'. |
|
150
|
|
|
|
|
|
|
join(', ',$c->form->invalid()).'</b>'; |
|
151
|
|
|
|
|
|
|
} else { |
|
152
|
|
|
|
|
|
|
[% table_class %]->create_from_form( $c->form ); |
|
153
|
|
|
|
|
|
|
return $c->forward('list'); |
|
154
|
|
|
|
|
|
|
} |
|
155
|
|
|
|
|
|
|
$c->forward('add'); |
|
156
|
|
|
|
|
|
|
} |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=item do_edit |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
Edits a row and forwards to edit. |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=cut |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub do_edit : Local { |
|
165
|
|
|
|
|
|
|
my ( $self, $c, $id ) = @_; |
|
166
|
|
|
|
|
|
|
$c->form( optional => [ [% table_class %]->columns ] ); |
|
167
|
|
|
|
|
|
|
if ($c->form->has_missing) { |
|
168
|
|
|
|
|
|
|
$c->stash->{message}='You have to fill in all fields.'. |
|
169
|
|
|
|
|
|
|
'the following are missing: <b>'. |
|
170
|
|
|
|
|
|
|
join(', ',$c->form->missing()).'</b>'; |
|
171
|
|
|
|
|
|
|
} elsif ($c->form->has_invalid) { |
|
172
|
|
|
|
|
|
|
$c->stash->{message}='Some fields are correctly filled in.'. |
|
173
|
|
|
|
|
|
|
'the following are invalid: <b>'. |
|
174
|
|
|
|
|
|
|
join(', ',$c->form->invalid()).'</b>'; |
|
175
|
|
|
|
|
|
|
} else { |
|
176
|
|
|
|
|
|
|
[% table_class %]->retrieve($id)->update_from_form( $c->form ); |
|
177
|
|
|
|
|
|
|
$c->stash->{message}='Updated OK'; |
|
178
|
|
|
|
|
|
|
} |
|
179
|
|
|
|
|
|
|
$c->forward('edit'); |
|
180
|
|
|
|
|
|
|
} |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=item edit |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
Sets a template. |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=cut |
|
187
|
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
sub edit : Local { |
|
189
|
|
|
|
|
|
|
my ( $self, $c, $id ) = @_; |
|
190
|
|
|
|
|
|
|
$c->stash->{item} = [% table_class %]->retrieve($id); |
|
191
|
|
|
|
|
|
|
$c->stash->{template} = '[% prefix %]/edit.mhtml'; |
|
192
|
|
|
|
|
|
|
} |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=item list |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
Sets a template. |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=cut |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
sub list : Local { |
|
201
|
|
|
|
|
|
|
my ( $self, $c ) = @_; |
|
202
|
|
|
|
|
|
|
$c->stash->{template} = '[% prefix %]/list.mhtml'; |
|
203
|
|
|
|
|
|
|
} |
|
204
|
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=item view |
|
206
|
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
Fetches a row and sets a template. |
|
208
|
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=cut |
|
210
|
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
sub view : Local { |
|
212
|
|
|
|
|
|
|
my ( $self, $c, $id ) = @_; |
|
213
|
|
|
|
|
|
|
$c->stash->{item} = [% table_class %]->retrieve($id); |
|
214
|
|
|
|
|
|
|
$c->stash->{template} = '[% prefix %]/view.mhtml'; |
|
215
|
|
|
|
|
|
|
} |
|
216
|
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=back |
|
218
|
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=head1 AUTHOR |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
[% author %] |
|
222
|
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=head1 LICENSE |
|
224
|
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
This library is free software . You can redistribute it and/or modify |
|
226
|
|
|
|
|
|
|
it under the same terms as perl itself. |
|
227
|
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=cut |
|
229
|
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
1; |
|
231
|
|
|
|
|
|
|
__add__ |
|
232
|
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
<%args> |
|
234
|
|
|
|
|
|
|
$message=>undef |
|
235
|
|
|
|
|
|
|
</%args> |
|
236
|
|
|
|
|
|
|
<p><%$message%></p> |
|
237
|
|
|
|
|
|
|
<form action="<% $base . '[% uri %]/do_add' %>" method="post"> |
|
238
|
|
|
|
|
|
|
%foreach my $column ([%table_class%]->columns) { |
|
239
|
|
|
|
|
|
|
%next if ($column eq [%table_class%]->primary_column); |
|
240
|
|
|
|
|
|
|
<% $column %><br/> |
|
241
|
|
|
|
|
|
|
<% [%table_class%]->to_field($column)->as_XML %><br/> |
|
242
|
|
|
|
|
|
|
%} |
|
243
|
|
|
|
|
|
|
<input type="submit" value="Add"/> |
|
244
|
|
|
|
|
|
|
<form/> |
|
245
|
|
|
|
|
|
|
<br/> |
|
246
|
|
|
|
|
|
|
<a href="<% $base . '[% uri %]/list' %>">List</a> |
|
247
|
|
|
|
|
|
|
__edit__ |
|
248
|
|
|
|
|
|
|
<%args> |
|
249
|
|
|
|
|
|
|
$message=>undef |
|
250
|
|
|
|
|
|
|
$item |
|
251
|
|
|
|
|
|
|
</%args> |
|
252
|
|
|
|
|
|
|
<p><%$message%></p> |
|
253
|
|
|
|
|
|
|
<form action="<% $base . '[% uri %]/do_edit/' . $item->id %>" |
|
254
|
|
|
|
|
|
|
method="post"> |
|
255
|
|
|
|
|
|
|
%for my $column ($item->columns) { |
|
256
|
|
|
|
|
|
|
%next if ($column eq $item->primary_column); |
|
257
|
|
|
|
|
|
|
<% $column %><br/> |
|
258
|
|
|
|
|
|
|
<% $item->to_field($column)->as_XML %><br/> |
|
259
|
|
|
|
|
|
|
%} |
|
260
|
|
|
|
|
|
|
<input type="submit" value="Edit"/> |
|
261
|
|
|
|
|
|
|
<form/> |
|
262
|
|
|
|
|
|
|
<br/> |
|
263
|
|
|
|
|
|
|
<a href="<% $base . '[% uri %]/list' %>">List</a> |
|
264
|
|
|
|
|
|
|
__list__ |
|
265
|
|
|
|
|
|
|
<table> |
|
266
|
|
|
|
|
|
|
<tr> |
|
267
|
|
|
|
|
|
|
%for my $column ([%table_class%]->columns) { |
|
268
|
|
|
|
|
|
|
%next if ($column eq [%table_class%]->primary_column); |
|
269
|
|
|
|
|
|
|
<th><% $column %></th> |
|
270
|
|
|
|
|
|
|
%} |
|
271
|
|
|
|
|
|
|
<th/> |
|
272
|
|
|
|
|
|
|
</tr> |
|
273
|
|
|
|
|
|
|
%for my $object ([%table_class%]->retrieve_all) { |
|
274
|
|
|
|
|
|
|
<tr> |
|
275
|
|
|
|
|
|
|
% for my $column ([%table_class%]->columns) { |
|
276
|
|
|
|
|
|
|
% next if ($column eq [%table_class%]->primary_column); |
|
277
|
|
|
|
|
|
|
<td><% $object->$column %></td> |
|
278
|
|
|
|
|
|
|
% } |
|
279
|
|
|
|
|
|
|
<td> |
|
280
|
|
|
|
|
|
|
<a href="<% $base . '[% uri %]/view/' . $object->id %>"> |
|
281
|
|
|
|
|
|
|
View |
|
282
|
|
|
|
|
|
|
</a> |
|
283
|
|
|
|
|
|
|
<a href="<% $base . '[% uri %]/edit/' . $object->id %>"> |
|
284
|
|
|
|
|
|
|
Edit |
|
285
|
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
</a> |
|
287
|
|
|
|
|
|
|
<a href="<% $base . '[% uri %]/destroy/' . $object->id %>"> |
|
288
|
|
|
|
|
|
|
Destroy |
|
289
|
|
|
|
|
|
|
</a> |
|
290
|
|
|
|
|
|
|
</td> |
|
291
|
|
|
|
|
|
|
</tr> |
|
292
|
|
|
|
|
|
|
%} |
|
293
|
|
|
|
|
|
|
</table> |
|
294
|
|
|
|
|
|
|
<a href="<% $base . '[% uri %]/add' %>">Add</a> |
|
295
|
|
|
|
|
|
|
__view__ |
|
296
|
|
|
|
|
|
|
<%args> |
|
297
|
|
|
|
|
|
|
$item |
|
298
|
|
|
|
|
|
|
</%args> |
|
299
|
|
|
|
|
|
|
%for my $column ($item->columns) { |
|
300
|
|
|
|
|
|
|
% next if $column eq $item->primary_column; |
|
301
|
|
|
|
|
|
|
<b><% $column %></b><br/> |
|
302
|
|
|
|
|
|
|
<% $item->$column %><br/><br/> |
|
303
|
|
|
|
|
|
|
%} |
|
304
|
|
|
|
|
|
|
<a href="<% $base . '[% uri %]/list' %>">List</a> |