blib/lib/Maypole/Plugin/QuickTable.pm

filter: Statementstmt 16.8% 17 / 101 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 35 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 41.6% 5 / 12 Podpod 100% 4 / 4 totaltotal 11.7% 26 / 221 CC 39 SCAR 43.7
CC
39
Cov
9%
orderby_link
182.0
tabulate
90.0
_tabulate_object
56.0
CRAP 79.3
↑ Summary
1 package Maypole::Plugin::QuickTable;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use URI();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use NEXT;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use HTML::QuickTable;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 our $VERSION = 0.422;
13  
14 =head1 NAME
15  
16 Maypole::Plugin::QuickTable - HTML::QuickTable convenience
17  
18 =head1 SYNOPSIS
19  
20 use Maypole::Application qw( LinkTools QuickTable );
21  
22 =head1 METHODS
23  
24 =over
25  
26 =item setup
27  
28 =cut
29  
30 sub setup
31 0 ▶{
Subroutine setup: not called CC=5 SCAR=34.0
Pod documented
32 0 my $r = shift;
33
34 0 $r->NEXT::DISTINCT::setup( @_ );
35  
36 0 ▶ warn "Running " . __PACKAGE__ . " setup for $r" if $r->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $r->debug 0 0 0
37
38 0 ▶ my $model = $r->config->model ||
Condition0%0/2
$r->config->model || die("Please configure a model in $r before calling setup()")
l!l
00
MC/DC0%0/1
$r->config->model || die("Please configure a model in $r before calling setup()")
$r->config->model
Truth table0%0/2
A
A= $r->config->model
Aresult
11
00
39 die "Please configure a model in $r before calling setup()";
40
41 0 ▶ die __PACKAGE__ . " needs Maypole::Plugin::LinkTools"
Branch: 1 branch
BranchTrueFalseTotal
unless $r->can("maybe_many_link_views") 0 0 0
42 unless $r->can( 'maybe_many_link_views' );
43
44 0 ▶ warn "quicktable_defaults are shared by ALL models - cf. fb_defaults, which had this same bug"
Branch: 1 branch
BranchTrueFalseTotal
if $r->debug > 1 0 0 0
45 if $r->debug > 1;
46 0 $model->mk_classdata( 'quicktable_defaults', {} );
47 }
48  
49 =item quick_table
50  
51 Returns a L<HTML::QuickTable|HTML::QuickTable> object for formatting data.
52  
53 print $request->quick_table( %args )->render( $data );
54  
55 The method gathers arguments from the C<quicktable_defaults> method on the model class. This
56 is a L<Class::Data::Inheritable|Class::Data::Inheritable> method, so you can set global
57 defaults on the main model class, and then override them in model subclasses. To preserve
58 most settings and override others, say something like
59  
60 $sub_model->quicktable_defaults( { %{ $model->quicktable_defaults }, %hash_of_overrides } );
61  
62 Arguments passed in the method call override those stored on the model.
63  
64 Arguments are passed directly to C<< HTML::QuickTable->new >>, so see L<HTML::QuickTable> for a
65 description.
66  
67 Additional arguments are:
68  
69 object => a Maypole/CDBI object
70  
71 Pass a Maypole/CDBI object in the C<object> slot, and its data will be extracted
72 and C<< $qt->render >> called for you:
73  
74 print $request->quick_table( %args, object => $object );
75
76 Related objects will be displayed as links to their view template.
77  
78 If no object is supplied, a L<HTML::QuickTable> object is returned. If an object is
79 supplied, it is passed to C<tabulate> to extract its data, and the data passed to the
80 C<render> method of the L<HTML::QuickTable> object.
81  
82 To render a subset of an object's columns, say:
83  
84 my @data = $request->tabulate( objects => $object, with_colnames => 1, fields => [ qw( foo bar ) ] );
85
86 $request->quick_table( @data );
87  
88 =cut
89  
90 sub quick_table
91 0 ▶{
Subroutine quick_table: not called CC=4 SCAR=30.0
Pod documented
92 0 my ( $self, %args ) = @_;
93
94 0 my $object = delete $args{object};
95
96 # this allows the caller to pass in some prepackaged data and get a table back
97 0 ▶ return HTML::QuickTable->new( %args ) unless $object;
Branch: 1 branch
BranchTrueFalseTotal
unless $object 0 0 0
98
99 0 ▶ my $model_class = ref( $object ) || $object;
Condition0%0/3
ref $object || $object
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $object || $object
ref $object $object
Truth table0%0/3
A || B
A= ref $objectB= $object
ABresult
1X1
011
000
100
101 0 %args = ( %{ $model_class->quicktable_defaults }, %args );
102
103 0 ▶ $args{labels} ||= 1;
Condition0%0/2
$args{"labels"} ||= 1
l!l
00
MC/DC0%0/1
$args{"labels"} ||= 1
$args{"labels"}
Truth table0%0/2
A
A= $args{"labels"}
Aresult
11
00
104
105 0 my $qt = HTML::QuickTable->new( %args );
106
107 0 return $qt->render( [ $self->tabulate( objects => $object, with_colnames => 1 ) ] );
108 }
109  
110 =item tabulate( $object|$arrayref_of_objects, %args )
111  
112 Extract data from a Maypole/CDBI object (or multiple objects), ready to pass to C<< quick_table->render >>.
113 Data will start with a row of column names if C<$args{with_colnames}> is true.
114  
115 A callback subref can be passed in C<$args{callback}>. It will be called in turn with each object as
116 its argument. The result(s) of the call will be added to the row of data for that object. See
117 the C<list> template in L<Maypole::FormBuilder|Maypole::FormBuilder>, which uses this technique
118 to add C<edit> and C<delete> buttons to each row.
119  
120 Similarly, a C<field_callback> coderef will be called during rendering of each field, receiving the
121 object and the current field as arguments. See the C<addmany> template for an example.
122  
123 Arguments:
124  
125 callback coderef
126 field_callback coderef
127 with_colnames boolean
128 fields defaults to ( $request->model_class->display_columns, $request->model_class->related )
129 objects defaults to $request->objects
130  
131 =cut
132  
133 # HTML::QuickTable seems to accept an array of arrayrefs, which is undocumented, but
134 # simplifies this code - just pass whatever this returns, directly to render(). In fact,
135 # HTML::QuickTable::render() puts the data into an arrayref if it's supplied as an array,
136 # so it seems safe to rely on.
137 sub tabulate
138 0 ▶{
Subroutine tabulate: not called CC=9 SCAR=45.0
Pod documented
139 0 my ( $self, %args ) = @_;
140
141 0 ▶ my $objects = $args{objects} || $self->objects;
Condition0%0/3
$args{'objects'} || $self->objects
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'objects'} || $self->objects
$args{'objects'} $self->objects
Truth table0%0/3
A || B
A= $args{'objects'}B= $self->objects
ABresult
1X1
011
000
142
143 0 ▶ my @objects = ref( $objects ) eq 'ARRAY' ? @$objects : ( $objects );
Branch: 1 branch
BranchTrueFalseTotal
ref $objects eq 'ARRAY' ? : 0 0 0
144  
145 # assumes all objects are in the same class
146 0 ▶ my $model_class = ref( $objects[0] ) || $objects[0];
Condition0%0/3
ref $objects[0] || $objects[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $objects[0] || $objects[0]
ref $objects[0] $objects[0]
Truth table0%0/3
A || B
A= ref $objects[0]B= $objects[0]
ABresult
1X1
011
000
147
148 # If we're tabulating a set of search results, and the search returned no results,
149 # there are no objects. I'm not sure at the moment whether this will return the correct
150 # class in all cases - there might have been a template switcheroo, which was why this
151 # method looks at the object's class and not the request's model class anyway. But
152 # for the moment there's nothing else available:
153 0 ▶ $model_class ||= $self->model_class;
Condition0%0/3
$model_class ||= $self->model_class
l!l&&r!l&&!r
000
MC/DC0%0/2
$model_class ||= $self->model_class
$model_class $self->model_class
Truth table0%0/3
A ||= B
A= $model_classB= $self->model_class
ABresult
1X1
011
000
154
155 0 ▶ my @fields = $args{fields} ? @{ $args{fields} } :
Branch: 1 branch
BranchTrueFalseTotal
$args{'fields'} ? : 0 0 0
156 ( $model_class->view_columns, $model_class->view_fields );
157
158 0 my @data = map { $self->_tabulate_object( $_, \@fields, $args{callback}, $args{field_callback} ) } @objects;
159
160 0 ▶ return @data unless $args{with_colnames};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"with_colnames"} 0 0 0
161
162 # If no rows (e.g. no search results), return 1 empty row to cause the table
163 # headers to be printed correctly.
164 0 ▶ @data = ( [ ( '' ) x @fields ] ) unless @data;
Branch: 1 branch
BranchTrueFalseTotal
unless @data 0 0 0
165
166 0 my %names = ( $model_class->column_names, $model_class->field_names );
167
168 my @headers = $self->action eq 'list' ? $self->_make_linked_headers( $model_class, \@fields ) :
169 0 ▶ map { $names{ $_ } } @fields;
Branch: 1 branch
BranchTrueFalseTotal
$self->action eq 'list' ? : 0 0 0
170  
171 0 unshift @data, \@headers;
172
173 0 return @data;
174 }
175  
176 # build clickable column headers to control sorting - from Ron McClain
177 sub _make_linked_headers
178 0 ▶{
Subroutine _make_linked_headers: not called CC=2 SCAR=17.9
179 0 my ( $self, $model_class, $fields ) = @_;
180  
181 0 my @headers;
182
183 0 foreach my $field ( @$fields )
184 {
185 0 push @headers, $self->orderby_link( $field, $model_class );
186 }
187 0 return @headers;
188 }
189  
190 =item orderby_link( $field, [ $model_class ] )
191  
192 Build a link for a column header. Controls whether the table should be sorted by that
193 column. Toggles sort direction.
194  
195 The C<$model_class> parameter is only necessary when building a table for a class different
196 from the current model class for the request.
197  
198 =cut
199  
200 # build clickable column headers to control sorting - from Ron McClain
201 sub orderby_link
202 0 ▶{
Subroutine orderby_link: not called CC=13 SCAR=52.0
Pod documented
203 0 my ( $self, $field, $model_class ) = @_;
204
205 0 ▶ $model_class ||= $self->model_class;
Condition0%0/3
$model_class ||= $self->model_class
l!l&&r!l&&!r
000
MC/DC0%0/2
$model_class ||= $self->model_class
$model_class $self->model_class
Truth table0%0/3
A ||= B
A= $model_classB= $self->model_class
ABresult
1X1
011
000
206
207 0 my %names = $model_class->column_names;
208  
209 # take a copy so we can delete things from it without removing data used elsewhere
210 0 my %params = %{ $self->params };
211
212 # these come from the search form on the initial search
213 0 my($order_by, $order_dir);
214 0 ▶ ( $order_by, $order_dir ) = split /\s+/, $params{search_opt_order_by} if $params{search_opt_order_by};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"search_opt_order_by"} 0 0 0
215  
216 # otherwise, from the header links
217 0 ▶ $order_by = $params{order} if $params{order};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"order"} 0 0 0
218 0 ▶ $order_dir ||= $params{o2} || 'desc';
Condition0%0/5
$order_dir ||= $params{"o2"} || "desc"
l!l&&r!l&&!r
000
$params{'o2'} || 'desc'
l!l
00
MC/DC0%0/2
$order_dir ||= $params{"o2"} || "desc"
$order_dir $params{'o2'}
Truth table0%0/3
A ||= B
A= $order_dirB= $params{'o2'}
ABresult
1X1
011
000
219 0 ▶ $order_dir = ( $order_dir eq 'desc' ) ? 'asc' : 'desc';
Branch: 1 branch
BranchTrueFalseTotal
$order_dir eq 'desc' ? : 0 0 0
220 0 delete $params{search_opt_order_by};
221 0 delete $params{order_by};
222 0 delete $params{o2};
223 0 delete $params{page};
224  
225 # is this a column? - it might be a has_many field instead
226 0 ▶ if ( $names{ $field } )
Branch: 1 branch
BranchTrueFalseTotal
if ($names{$field}) { } 0 0 0
227 {
228 0 my $uri = URI->new;
229
230 0 ▶ if ( $self->action eq 'do_search' )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->action eq 'do_search') { } 0 0 0
elsif ($self->action eq 'list') { } 0 0 0
231 {
232 0 $params{search_opt_order_by} = "$field $order_dir"
233 }
234 elsif ( $self->action eq 'list' )
235 {
236 0 $params{order} = $field;
237 0 $params{o2} = $order_dir;
238 }
239 else
240 {
241 0 %params = ( order => $field,
242 o2 => $order_dir
243 );
244 }
245
246 0 $uri->query_form( %params );
247
248 0 my $arrow = '';
249
250 0 ▶ if ( $order_by and $order_by eq $field )
Branch: 1 branch
BranchTrueFalseTotal
if ($order_by and $order_by eq $field) 0 0 0
Condition0%0/3
$order_by and $order_by eq $field
!ll&&!rl&&r
000
MC/DC0%0/2
$order_by and $order_by eq $field
$order_by $order_by eq $field
Truth table0%0/3
A and B
A= $order_byB= $order_by eq $field
ABresult
0X0
100
111
251 {
252 0 ▶ $arrow = $order_dir eq 'asc' ? '&nbsp;&darr;' : '&nbsp;&uarr;';
Branch: 1 branch
BranchTrueFalseTotal
$order_dir eq 'asc' ? : 0 0 0
253 }
254
255 0 my $args = "?".$uri->equery;
256
257 return $self->link( table => $self->model_class->table,
258 action => $self->action,
259 additional => $args,
260 0 label => $names{ $field } . $arrow,
261 );
262 }
263 else
264 {
265 # has_many, might_have fields
266 0 my $related_class = $self->model_class->related_class( $self, $field );
267 0 my $field_name = $related_class->plural_moniker;
268 0 return ucfirst( $field_name );
269 }
270 }
271  
272 # Return an arrayref of values for a single object, which will be passed to
273 # QuickTable and rendered as a row in the table. The callback is optional, and
274 # can be used to add extra entries to the row. Column values that inflate to CDBI
275 # objects will be rendered as links to the view template. Column values that inflate
276 # to non-CDBI objects will be returned as the object, which will presumably be evaluated
277 # in string context at some point in QT render.
278 sub _tabulate_object
279 0 ▶{
Subroutine _tabulate_object: not called CC=7 SCAR=40.3
280 0 my ( $self, $object, $cols, $callback, $field_callback ) = @_;
281
282 0 ▶ my $str_col = $object->stringify_column || ''; # '' to silence warnings in the map
Condition0%0/2
$object->stringify_column || ''
l!l
00
MC/DC0%0/1
$object->stringify_column || ''
$object->stringify_column
Truth table0%0/2
A
A= $object->stringify_column
Aresult
11
00
283
284 0 ▶ if ( $self->debug && ! $str_col )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug and not $str_col) 0 0 0
Condition0%0/3
$self->debug and not $str_col
!ll&&!rl&&r
000
MC/DC0%0/2
$self->debug and not $str_col
$self->debug not $str_col
Truth table0%0/3
A and not(B)
A= $self->debugB= not $str_col
ABresult
0X0
100
111
285 {
286 0 warn sprintf "No stringify_column specified in %s - please define a 'Stringify' column " .
287 "group with a single column", ref( $object );
288 }
289
290 my $lister = sub
291 0 ▶ {
Subroutine __ANON__: not called CC=5 SCAR=34.0
292 0 ▶ return '' unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
293 0 ▶ return @_ if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 0 0
294 #return join( "\n", '<ol>', ( map { "<li>$_</li>" } @_ ), '</ol>' );
295 0 ▶ return join ', ', @_ if @_ < 3 or $self->template =~ /view/;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 3 or $self->template =~ /view/ 0 0 0
Condition0%0/3
@_ < 3 or $self->template =~ /view/
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ < 3 or $self->template =~ /view/
@_ < 3 $self->template =~ /view/
Truth table0%0/3
A or B
A= @_ < 3B= $self->template =~ /view/
ABresult
1X1
011
000
296 0 return join ', ', $_[0], $_[1], @_ - 2 . ' more...';
297 0 };
298
299 # XXX: getting a 'Use of uninitialized value in string eq warning' - looks like
300 # $object->stringify_column can return undef?
301 0 my @data = map { $self->maybe_link_view( $_ ) }
302
303 # for the stringification column (e.g. 'name'), return the object, which
304 # will be translated into a link to the 'view' template by
305 # maybe_link_view. Otherwise, return the value, which will be rendered
306 # verbatim, unless it is an object in a related class, in which case
307 # it will be rendered as a link to the view template.
308 0 ▶ map { $_ eq $str_col ? $object : $lister->( $self->maybe_many_link_views( $object->$_ ) ) }
Branch: 1 branch
BranchTrueFalseTotal
$_ eq $str_col ? : 0 0 0
309 @$cols;
310
311 0 ▶ if ( $field_callback )
Branch: 1 branch
BranchTrueFalseTotal
if ($field_callback) 0 0 0
312 {
313 0 @data = map { [ $_, $field_callback->( $object, shift( @$cols ) ) ] } @data;
314 }
315
316 0 ▶ push( @data, $callback->( $object ) ) if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 0 0 0
317
318 0 return \@data;
319 }
320  
321 =back
322  
323 =head1 AUTHOR
324  
325 David Baird, C<< <cpan@riverside-cms.co.uk> >>
326  
327 =head1 BUGS
328  
329 Please report any bugs or feature requests to
330 C<bug-maypole-plugin-quicktable@rt.cpan.org>, or through the web interface at
331 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Maypole-Plugin-QuickTable>.
332 I will be notified, and then you'll automatically be notified of progress on
333 your bug as I make changes.
334  
335 =head1 COPYRIGHT & LICENSE
336  
337 Copyright 2005 David Baird, All Rights Reserved.
338  
339 This program is free software; you can redistribute it and/or modify it
340 under the same terms as Perl itself.
341  
342 =cut
343  
344 2 1; # End of Maypole::Plugin::QuickTable
↑ Summary