DBIx/Class/Valiant/Result/HTML/FormFields.pm untested

filter: Statementstmt 0.0% 0 / 116 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 10 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 20 totaltotal 0.0% 0 / 201 CC 17 SCAR 29.0
CC
17
Cov
0%
_auto_read_attribute_for_html
56.0
register_column
30.0
read_attribute_for_html
20.0
CRAP 18.1
1 package DBIx::Class::Valiant::Result::HTML::FormFields;
2  
3 use base 'DBIx::Class';
4  
5 use warnings;
6 use strict;
7  
8 ## TODO add cache=>1 for simple memory caching of the resultset
9  
10 __PACKAGE__->mk_classdata( __select_options_rs_for => {} );
11 __PACKAGE__->mk_classdata( __checkbox_rs_for => {} );
12 __PACKAGE__->mk_classdata( __radio_button_rs_for => {} );
13 __PACKAGE__->mk_classdata( __radio_buttons_for => {} );
14 __PACKAGE__->mk_classdata( __field_attribute_for => {} );
15  
16 __PACKAGE__->mk_classdata( __tags_by_column => {} );
17 __PACKAGE__->mk_classdata( __columns_by_tag => {} );
18  
19 sub register_column {
20 my ($self, $column, $info, @rest) = @_;
21 $self->next::method($column, $info, @rest);
22  
23 my $tag_info = exists $info->{tag}
24 ? $info->{tag}
25 : exists $info->{tags}
26 ? $info->{tags}
27 : undef;
28  
29 return unless $tag_info;
30  
31 my @tags = (ref($tag_info)||'') eq 'ARRAY'
32 ? @{$tag_info}
33 : ($tag_info);
34  
35 $self->__tags_by_column->{$column} = \@tags;
36
37 foreach my $tag (@tags) {
38 push @{$self->__columns_by_tag->{$tag}}, $column;
39 }
40 }
41  
42 sub tags_by_column {
43 my ($self, $column) = @_;
44 return @{$self->__tags_by_column->{$column}||[]};
45 }
46  
47 sub columns_by_tag {
48 my ($self, $tag) = @_;
49 return @{$self->__columns_by_tag->{$tag}||[]};
50 }
51  
52 sub add_select_options_rs_for {
53 my ($class, $column, $code) = @_;
54 $class->__select_options_rs_for->{$column} = $code;
55 }
56  
57 sub select_options_rs_for {
58 my ($self, $column, %options) = @_;
59 my $code = $self->__select_options_rs_for->{$column};
60 my $rs = $code->($self, %options);
61 my ($value_method, $label_method) = sub {
62 my $class = shift->result_source->result_class;
63 my ($value_method) = $class->columns_by_tag('option_value');
64 my ($label_method) = $class->columns_by_tag('option_label');
65 return ($value_method, $label_method);
66 }->($rs);
67  
68 return $rs, $label_method, $value_method;
69 }
70  
71 sub select_options_for {
72 my ($self, $column, %options) = @_;
73 my ($rs, $label_method, $value_method) = $self->select_options_rs_for($column, %options);
74 my @options = map {[ $_->$label_method, $_->$value_method ]} $rs->all;
75 return \@options;
76 }
77  
78 sub add_checkbox_rs_for {
79 my ($class, $column, $code) = @_;
80 $class->__checkbox_rs_for->{$column} = $code;
81 }
82  
83 sub checkbox_rs_for {
84 my ($self, $column, %options) = @_;
85 my $code = $self->__checkbox_rs_for->{$column};
86 my $rs = $code->($self, %options);
87 my ($value_method, $label_method) = sub {
88 my $class = shift->result_source->result_class;
89 my ($value_method) = $class->columns_by_tag('checkbox_value');
90 my ($label_method) = $class->columns_by_tag('checkbox_label');
91 return ($value_method, $label_method);
92 }->($rs);
93  
94 return $rs, $label_method, $value_method;
95 }
96  
97 sub checkboxes_for {
98 my ($self, $column, %options) = @_;
99 my ($rs, $label_method, $value_method) = $self->checkbox_rs_for($column, %options);
100 my @options = map {[ $_->$label_method, $_->$value_method ]} $rs->all;
101 return \@options;
102 }
103  
104 sub add_radio_button_rs_for {
105 my ($class, $column, $code) = @_;
106 $class->__radio_button_rs_for->{$column} = $code;
107 }
108  
109 sub radio_button_rs_for {
110 my ($self, $column, %options) = @_;
111 my $code = $self->__radio_button_rs_for->{$column};
112 my $rs = $code->($self, %options);
113 my ($value_method, $label_method) = sub {
114 my $class = shift->result_source->result_class;
115 my ($value_method) = $class->columns_by_tag('radio_value');
116 my ($label_method) = $class->columns_by_tag('radio_label');
117 return ($value_method, $label_method);
118 }->($rs);
119  
120 return $rs, $label_method, $value_method;
121 }
122  
123 sub add_radio_buttons_for {
124 my ($class, $column, $code) = @_;
125 $class->__radio_buttons_for->{$column} = $code;
126 }
127 sub radio_buttons_for {
128 my ($self, $column, %options) = @_;
129 my $code = $self->__radio_buttons_for->{$column};
130 my @buttons = $code->($self, %options);
131 return @buttons;
132 }
133  
134 # $class->add_form_field_for($column);
135 # $class->add_form_field_for($column, \&code);
136 # $class->add_form_field_for($column, \%options);
137 # $class->add_form_field_for($column, \%options, \&code);
138  
139 sub add_form_field_for {
140 my ($class, $column) = (shift(@_), shift(@_));
141 my $options = (ref($_[0])||'') eq 'HASH' ? shift(@_) : +{};
142 my $code = (ref($_[0])||'') eq 'CODE' ? shift(@_) : sub { $class->_auto_read_attribute_for_html($column) };
143  
144 $class->__field_attribute_for->{$column} = [$code, $options];
145 }
146  
147 # __PACKAGE__->add_form_field_for(
148 # 'user',
149 # sub ($self) { $self->user},
150 # { label=>..., type=>..., context=>'admin' } );
151  
152 # add 'context' to this so that you can have different forms for same
153 # TODO maybe need to add way to wrap getting label and errors...
154  
155 sub has_form_fields {
156 my ($self) = @_;
157 return scalar keys %{$self->__field_attribute_for};
158 }
159  
160 sub has_form_field {
161 my ($self, $column) = @_;
162 return exists $self->__field_attribute_for->{$column};
163 }
164  
165 sub read_form_field_for {
166 my ($self, $column) = @_;
167 die "Can't find a form field for column '$column'" unless $self->has_form_field($column);
168 return $self->_read_form_field_for($column);
169 }
170  
171 sub _read_form_field_for {
172 my ($self, $column) = @_;
173 my ($code, $options) = @{$self->__field_attribute_for->{$column}};
174 return $code->($self, $column);
175 }
176  
177  
178 sub read_attribute_for_html {
179 my ($self, $attribute) = @_;
180 die "'attribute' is required argument" unless defined $attribute;
181  
182 # If at least one form field is defined for this class, that means the
183 # author wanted fine grained control over the form fields so we'll use that
184 # and only that. Otherwise we'll fall back to the auto reading method
185  
186 if($self->has_form_fields) {
187 return $self->_read_form_field_for($attribute) if $self->has_form_field($attribute);
188 } else {
189 return $self->_auto_read_attribute_for_html($attribute);
190 }
191 }
192  
193 sub _auto_read_attribute_for_html {
194 my ($self, $attribute) = @_;
195  
196 # Handle special case for 'delete' attribute and 'add' attribute
197 return $self->is_marked_for_deletion if $attribute eq '_delete';
198 return 1 if $attribute eq '_add';
199  
200 # First fallback to the normal DBIC way of getting a column value
201 return $self->get_column($attribute) if $self->result_source->has_column($attribute);
202  
203 # Second just look for a method that matches the attribute name
204 return $self->$attribute if $self->can($attribute);
205  
206 # Permit getting the value of a relationship if it's a single relationship
207 if($self->has_relationship($attribute)) {
208 my $rel_data = $self->relationship_info($attribute);
209 my $rel_type = $rel_data->{attrs}{accessor};
210 return $self->$attribute if($rel_type eq 'single');
211 }
212  
213 # If we can't find a value just return because the formhandler will decide
214 # if this is an error or not
215 return bless +{}, 'Valiant::BadAttribute';
216 }
217  
218 1;
219  
220 =head1 NAME
221  
222 DBIx::Class::Valiant::Result::HTML::FormFields - Map DBIC Fields to HTML Form Fields
223  
224 =head1 SYNOPSIS
225  
226 package Example::Schema::Result::Person;
227  
228 use base 'DBIx::Class::Core';
229  
230 __PACKAGE__->load_components('Valiant::Result::HTML::FormFields');
231  
232 Or just add to your base Result class:
233  
234 package Example::Schema::Result;
235  
236 use strict;
237 use warnings;
238 use base 'DBIx::Class::Core';
239  
240 __PACKAGE__->load_components('Valiant::Result::HTML::FormFields');
241  
242 =head1 DESCRIPTION
243  
244 This module extends DBIx::Class to provide functionality for mapping database fields to HTML form fields. It allows for easy generation and handling of form elements based on the schema's result class.
245  
246 =head1 METHODS
247  
248 =head2 register_column
249  
250 $self->register_column($column, $info, @rest);
251  
252 Registers a column with additional tag information. This method is overridden to add tag metadata for columns, which can be used for form generation.
253  
254 Example:
255  
256 __PACKAGE__->register_column('status', { data_type => 'varchar', size => 255, tags => ['select', 'option_value'] });
257  
258 =head2 tags_by_column
259  
260 my @tags = $self->tags_by_column($column);
261  
262 Returns an array of tags associated with a column.
263  
264 Example:
265  
266 my @tags = $result->tags_by_column('status');
267  
268 =head2 columns_by_tag
269  
270 my @columns = $self->columns_by_tag($tag);
271  
272 Returns an array of columns associated with a tag.
273  
274 Example:
275  
276 my @columns = $result->columns_by_tag('select');
277  
278 =head2 add_select_options_rs_for
279  
280 $class->add_select_options_rs_for($column, $code);
281  
282 Adds a result set generator code reference for a select field.
283  
284 Example:
285  
286 __PACKAGE__->add_select_options_rs_for('status', sub {
287 my ($self, %options) = @_;
288 return $self->result_source->schema->resultset('Status')->search({}, \%options);
289 });
290  
291 =head2 select_options_rs_for
292  
293 my ($rs, $label_method, $value_method) = $self->select_options_rs_for($column, %options);
294  
295 Returns a result set, label method, and value method for a select field.
296  
297 Example:
298  
299 my ($rs, $label_method, $value_method) = $result->select_options_rs_for('status');
300  
301 =head2 select_options_for
302  
303 my @options = $self->select_options_for($column, %options);
304  
305 Returns a list of select options for a column. This is a array of arrayrefs where the first element is the label and the second element is the value.
306  
307 Example:
308  
309 my @options = $result->select_options_for('status');
310  
311 =head2 add_checkbox_rs_for
312  
313 $class->add_checkbox_rs_for($column, $code);
314  
315 Adds a result set generator code reference for a checkbox field.
316  
317 Example:
318  
319 __PACKAGE__->add_checkbox_rs_for('roles', sub {
320 my ($self, %options) = @_;
321 return $self->result_source->schema->resultset('Role')->search({}, \%options);
322 });
323  
324 =head2 checkbox_rs_for
325  
326 my $rs = $self->checkbox_rs_for($column, %options);
327  
328 Returns a result set for a checkbox field.
329  
330 Example:
331  
332 my $rs = $result->checkbox_rs_for('roles');
333  
334 =head2 checkboxes_for
335  
336 my @checkboxes = $self->checkboxes_for($column, %options);
337  
338 Returns a list of checkboxes for a column. This is an array of arrayrefs where the first element is the label and the second element is the value.
339  
340 =head2 add_radio_button_rs_for
341  
342 $class->add_radio_button_rs_for($column, $code);
343  
344 Adds a result set generator code reference for a radio button field.
345  
346 Example:
347  
348 __PACKAGE__->add_radio_button_rs_for('gender', sub {
349 my ($self, %options) = @_;
350 return $self->result_source->schema->resultset('Gender')->search({}, \%options);
351 });
352  
353 =head2 radio_button_rs_for
354  
355 my $rs = $self->radio_button_rs_for($column, %options);
356  
357 Returns a result set for a radio button field.
358  
359 Example:
360  
361 my $rs = $result->radio_button_rs_for('gender');
362  
363 =head2 add_radio_buttons_for
364  
365 $class->add_radio_buttons_for($column, $code);
366  
367 Add an array of radio button labels
368 Example:
369  
370 __PACKAGE__->add_radio_buttons_for('status', sub($self, %options) {
371 return $self->status_list;
372 });
373  
374 sub status_list($self) { return qw( pending active inactive ) }
375  
376 =head2 radio_buttons_for
377  
378 my @buttons = $self->radio_buttons_for($column, %options);
379  
380 Returns a list of radio buttons for a column.
381  
382 Example:
383  
384 my @buttons = $result->radio_buttons_for('preferences');
385  
386 =head2 add_form_field_for
387
388 $class->add_form_field_for($column, $code);
389  
390 Adds a form field generator code reference for a column.
391  
392 Example:
393  
394 __PACKAGE__->add_form_field_for('first_name', sub {
395 my ($self, $column) = @_;
396 return $self->first_name;
397 });
398  
399 B<Note:> If no form field is defined for a column,
400 the module will automatically read the value of the column via the C<_auto_read_attribute_for_html> method.
401  
402 =head2 has_form_fields
403  
404 my $bool = $self->has_form_fields;
405  
406 Checks if the result class has form fields.
407  
408 =head2 has_form_field
409  
410 my $bool = $self->has_form_field($attribute);
411  
412 Checks if the result class has a form field for an attribute.
413  
414 =head2 read_form_field_for
415  
416 my $value = $self->read_form_field_for($attribute);
417  
418 Reads the value of a form field for an attribute.
419  
420 =head2 read_attribute_for_html
421  
422 my $value = $self->read_attribute_for_html($attribute);
423  
424 Reads the value of an attribute for HTML.
425  
426 =head2 _auto_read_attribute_for_html
427  
428 my $value = $self->_auto_read_attribute_for_html($attribute);
429  
430 Automatically reads the value of an attribute for HTML.
431  
432 =head1 AUTHOR
433  
434 John Napiorkowski L<email:jjnapiork@cpan.org>
435  
436 =head1 SEE ALSO
437  
438 L<Valiant>, L<DBIx::Class>
439  
440 =head1 COPYRIGHT & LICENSE
441  
442 Copyright 2020, John Napiorkowski L<email:jjnapiork@cpan.org>
443  
444 This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
445  
446 =cut
447  
448