Valiant/HTML/Util/Pager.pm untested

filter: Statementstmt 0.0% 0 / 118 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 48 MC/DCmcdc 0.0% 0 / 30 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 292 CC 35 SCAR 47.2
CC
35
Cov
0%
pager_for
306.0
_instantiate_builder
42.0
_to_model
20.0
CRAP 112.2
1 package Valiant::HTML::Util::Pager;
2  
3 use Moo;
4 use Scalar::Util;
5 use Module::Runtime;
6 use Valiant::Naming;
7 use Carp;
8 use Valiant::HTML::FormBuilder::DefaultModel;
9  
10 extends 'Valiant::HTML::Util::FormTags';
11  
12 has 'context' => (is=>'ro', required=>0, predicate=>'has_context');
13 has 'controller' => (is=>'ro', required=>0, predicate=>'has_controller');
14  
15 has 'pager_builder_class' => (
16 is => 'ro',
17 required => 1,
18 lazy => 1,
19 builder => '_default_pager_builder_class',
20 );
21  
22 sub _default_pager_builder_class {
23 my $self = shift;
24 return $self->view->pager_builder_class if $self->view->can('pager_builder_class');
25 return 'Valiant::HTML::PagerBuilder';
26 };
27  
28 sub _to_model {
29 my ($self, $model) = @_;
30 croak "No model provided" unless $model;
31 confess "Model is not an object: $model" unless Scalar::Util::blessed($model);
32 return $model->to_model if $model->can('to_model');
33 return $model;
34 }
35  
36 sub _DEFAULT_ID_DELIM { '_' }
37  
38 sub _dom_class {
39 my ($self, $model, $prefix) = @_;
40 my $singular = $self->_model_name_from_object_or_class($model)->param_key;
41 return $prefix ? "${prefix}@{[ _DEFAULT_ID_DELIM ]}${singular}" : $singular;
42 }
43  
44 sub _dom_id {
45 my ($self, $model, $prefix) = @_;
46 if(my $model_id = _model_id_for_dom_id($model)) {
47 return "@{[ $self->_dom_class($model, $prefix) ]}@{[ _DEFAULT_ID_DELIM ]}${model_id}";
48 } else {
49 $prefix ||= 'new';
50 return $self->_dom_class($model, $prefix)
51 }
52 }
53  
54 sub _model_id_for_dom_id {
55 my $model = shift;
56 return unless $model->can('id') && defined($model->id);
57 return join '_', ($model->id);
58 }
59  
60 sub _model_name_from_object_or_class {
61 my ($self, $proto) = @_;
62 my $model = $self->_to_model($proto);
63 return $model->result_class->model_name if $model->can('model_name');
64 return Valiant::Name->new(Valiant::Naming::prepare_model_name_args($model));
65 }
66  
67 sub _object_from_record_proto {
68 my ($self, $proto) = @_;
69 croak "First argument can't be undef or empty string" unless defined($proto) && length($proto);
70 return $proto->[-1] if ((ref($proto)||'') eq 'ARRAY');
71 return $proto;
72 }
73  
74 # ->pager_for($model|\@model_path, \%options, \&block)
75 # ->pager_for($model|\@model_path, \&block)
76 #
77 # Where %options are:
78 # as: 'name' # Use this name for the pager
79 # pager: the pager object (derived from model if not provided)
80  
81 sub pager_for {
82 my $self = shift;
83 my $proto = shift; # required; at the start
84 my $content_block_coderef = pop; # required; at the end
85  
86 my $empty_block_coderef;
87 if(ref($_[-1]||'') eq 'CODE') {
88 # ok so the second is for empty
89 $empty_block_coderef = $content_block_coderef;
90 $content_block_coderef = pop
91 }
92  
93 my $options = ref($_[-1]||'') eq 'HASH' ? pop : +{};
94  
95 croak "You must provide a content block to pager_for" unless ref($content_block_coderef) eq 'CODE';
96  
97 my ($model, $object_name);
98  
99 if( ref(\$proto) eq 'SCALAR') {
100 $object_name = $proto;
101 if(@_) {
102 $model = shift; # form_for 'name', $model, \%options, \&block
103 } else {
104 # Support form_for 'attribute_name', \%options, \&block
105 $model = $self->view->read_attribute_for_html($object_name)
106 if $self->view->attribute_exists_for_html($object_name);
107 die "Can't find model from view @{[ $self->view ]} for attribute '$object_name'" unless $model;
108 }
109  
110 $options->{as} ||= $object_name if defined $model;
111  
112 } else {
113 my $object = $self->_object_from_record_proto($proto); # Is either arrayref or object
114  
115 croak "First argument can't be undef or empty string" unless defined($object) && length($object);
116 $model = $proto; # Is either arrayref or object
117 $object_name = exists $options->{as} ?
118 $options->{as} : $self->_model_name_from_object_or_class($object)->param_key;
119 $self->_apply_pager_for_options($object, $options);
120 }
121  
122 $options->{model} = $model;
123 $options->{name} = $object_name;
124 $options->{class} ||= $self->_dom_class($model, 'pager');
125 $options->{id} ||= $self->_dom_id($model, 'pager');
126 $options->{controller} ||= $self->controller if $self->has_controller;
127  
128 my $scope = exists $options->{scope} ?
129 delete $options->{scope} :
130 $self->_model_name_from_object_or_class($model)->param_key;
131 my $model_path = ((ref($options->{model})||'') eq 'ARRAY') ? $options->{model} : [$options->{model}];
132 $options->{uri_base} ||= $self->_polymorphic_path_for_model($model_path, $object_name, $options);
133  
134 my $builder = $self->_instantiate_builder($scope, $model, $options);
135 if($builder->pager->total_entries > 0) {
136 my $output = $self->join_tags(
137 $self->tags->div(+{}, sub {
138 my @form_node = $content_block_coderef->($self->view, $builder, $model);
139 return $form_node[0] if scalar(@form_node) == 1;
140 return my $text = $builder->view->safe_concat(@form_node);
141 })
142 );
143 return $output;
144 } else {
145 if($empty_block_coderef) {
146 return $self->tags->div(+{}, sub {
147 my @node = $empty_block_coderef->($self->view, $model);
148 return $builder->view->safe_concat(@node);
149 });
150  
151 } else {
152 return $self->tags->div(+{}, 'No entries found');
153 }
154 }
155 }
156  
157 sub _polymorphic_path_for_model {
158 my ($self, $model_path, $scope, $options) = @_;
159 my $scoped_method = "list_${scope}_uri";
160 my $controller_method = "list_uri";
161 my $controller = $options->{controller};
162  
163 return $self->view->list_uri_for_model($model_path, $scope) if $self->view->can('list_uri_for_model');
164 pop @$model_path if @$model_path > 0; # Remove the last item
165 return $controller->$controller_method($model_path) if $controller && $controller->can($controller_method);
166  
167 return undef;
168 }
169  
170 # _instantiate_builder($object)
171 # _instantiate_builder($object, \%options)
172 # _instantiate_builder($name, $object)
173 # _instantiate_builder($name, $object, \%options)
174  
175 sub _instantiate_builder {
176 my $self = shift;
177 my $options = (ref($_[-1])||'') eq 'HASH' ? pop(@_) : +{};
178 my $object = Scalar::Util::blessed($_[-1]) ? pop(@_) : bless +{}, 'Valiant::HTML::FormBuilder::DefaultModel';
179 my $model_name = scalar(@_) ? shift(@_) : $self->_model_name_from_object_or_class($object)->param_key;
180 my $builder = exists($options->{builder}) && defined($options->{builder}) ?
181 $options->{builder} :
182 $self->pager_builder_class;
183  
184 my %args = (
185 tag_helpers => $self,
186 model => $object,
187 name => $model_name,
188 uri_base => $options->{uri_base},
189 options => $options,
190 view => $self->view,
191 );
192  
193 $options->{builder} = $builder;
194 $self->_merge_attrs(\%args, $options, qw(id index parent_builder));
195  
196 if( exists($options->{parent_builder}) && exists($options->{parent_builder}{theme}) ) {
197 $args{theme} = +{ %{$args{theme}||+{}}, %{$options->{parent_builder}{theme}} };
198 }
199  
200 return Module::Runtime::use_module($builder)->new(%args);
201 }
202  
203  
204 1;
205  
206 =head1 NAME
207  
208 Valiant::HTML::Util::Pager - HTML pager component
209  
210 =head1 SYNOPSIS
211  
212 pager_for $list, +{uri_base => list_uri(+{'todo.status'=>$self->status})}, sub ($self, $pg, $list) {
213 $pg->window_info,
214 table +{class=>'table table-striped table-bordered', style=>'margin-bottom:0.5rem'}, [
215 thead
216 trow [
217 th +{scope=>"col"},'Title',
218 th +{scope=>"col", style=>'width:8em'}, 'Status',
219 ],
220 tbody { repeat=>$list }, sub ($self, $todo, $i) {
221 trow [
222 td a +{ href=>edit_uri([$todo]) }, $todo->title,
223 td $todo->status,
224 ],
225 },
226 tfoot,
227 td {colspan=>2, style=>'background:white'},
228 $pg->navigation_line,
229 ],
230 }, sub ($self, $list) {
231 div { class=>"alert alert-warning", role=>"alert" },
232 "There are no tasks to display."
233 },
234  
235  
236 =head1 DESCRIPTION
237  
238 Given a model with a pager object, or supplied with a pager object, this component
239 will render a pager for the model. The pager object is expected to be a L<Data::Page>
240 object or something that can be coerced into a L<Data::Page> object.
241  
242 =head1 ATTRIBUTES
243  
244 This class defines the following attributes.
245  
246 =head2 context
247  
248 Optional. If provided this is the L<Catalyst> context object. If not provided
249 then the context is expected to be available via the view object.
250  
251 =head2 controller
252  
253 Optional. If provided this is the L<Catalyst> controller object. If not provided
254 then the controller is expected to be available via the view object.
255  
256 =head2 pager_builder_class
257  
258 Optional. If provided this is the class used to instantiate the pager builder object.
259  
260  
261 =head1 INHERITED METHODS
262  
263 This class inherits all methods from L<Valiant::HTML::Util::TagBuilder> and
264 L<Valiant::HTML::Util::FormTags>.
265  
266 =head1 INSTANCE METHODS
267  
268 The following public instance methods are provided by this class.
269  
270 =head2 pager_for
271  
272 $pager->pager_for($model, \%options, \&block, \&empty_block);
273  
274 '\&block' is a coderef that will be called with the following arguments:
275  
276 =over 4
277  
278 =item $self
279  
280 The current object
281  
282 =item $pager
283  
284 The pager object
285  
286 =item $model
287  
288 The model object
289  
290 =back
291  
292 '\&empty_block' is called if the pager has no items and is a coderef that will be
293 called with the following arguments:
294  
295 =over 4
296  
297 =item $self
298  
299 The current object
300  
301 =item $model
302  
303 The model object
304  
305 =back
306  
307 The options hashref can contain the following keys:
308  
309 =over 4
310  
311 =item as
312  
313 Optional. The name of the pager. If not provided the name is derived from the model
314  
315 =item pager
316  
317 Optional. The pager object. If not provided the pager object is derived from the model
318  
319 =item uri_base
320  
321 Optional. The base uri for the pager. If not provided the uri is derived from the either
322 the view or the controller.
323  
324 =item controller
325  
326 Optional. The controller object. If not provided the controller object is derived from $self
327 Used to override if the controller associated with the given model is not the current controller
328  
329 =back
330  
331  
332 =head1 SEE ALSO
333
334 L<Valiant>, L<Valiant::HTML::PagerBuilder>
335  
336 =head1 AUTHOR
337
338 See L<Valiant>
339  
340 =head1 COPYRIGHT & LICENSE
341
342 See L<Valiant>
343  
344 =cut