Valiant/Result/HTML/FormFields.pm untested

filter: Statementstmt 0.0% 0 / 116 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 20 totaltotal 0.0% 0 / 206 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 # This component is private and unsupported: it is deliberately undocumented
2 # and excluded from the CPAN index (see dist.ini). It remains only for
3 # existing code that already depends on it and may be removed at any time.
4  
5 package DBIx::Class::Valiant::Result::HTML::FormFields;
6  
7 use base 'DBIx::Class';
8  
9 use warnings;
10 use strict;
11  
12 ## TODO add cache=>1 for simple memory caching of the resultset
13  
14 __PACKAGE__->mk_classdata( __select_options_rs_for => {} );
15 __PACKAGE__->mk_classdata( __checkbox_rs_for => {} );
16 __PACKAGE__->mk_classdata( __radio_button_rs_for => {} );
17 __PACKAGE__->mk_classdata( __radio_buttons_for => {} );
18 __PACKAGE__->mk_classdata( __field_attribute_for => {} );
19  
20 __PACKAGE__->mk_classdata( __tags_by_column => {} );
21 __PACKAGE__->mk_classdata( __columns_by_tag => {} );
22  
23 sub register_column {
24 my ($self, $column, $info, @rest) = @_;
25 $self->next::method($column, $info, @rest);
26  
27 my $tag_info = exists $info->{tag}
28 ? $info->{tag}
29 : exists $info->{tags}
30 ? $info->{tags}
31 : undef;
32  
33 return unless $tag_info;
34  
35 my @tags = (ref($tag_info)||'') eq 'ARRAY'
36 ? @{$tag_info}
37 : ($tag_info);
38  
39 $self->__tags_by_column->{$column} = \@tags;
40
41 foreach my $tag (@tags) {
42 push @{$self->__columns_by_tag->{$tag}}, $column;
43 }
44 }
45  
46 sub tags_by_column {
47 my ($self, $column) = @_;
48 return @{$self->__tags_by_column->{$column}||[]};
49 }
50  
51 sub columns_by_tag {
52 my ($self, $tag) = @_;
53 return @{$self->__columns_by_tag->{$tag}||[]};
54 }
55  
56 sub add_select_options_rs_for {
57 my ($class, $column, $code) = @_;
58 $class->__select_options_rs_for->{$column} = $code;
59 }
60  
61 sub select_options_rs_for {
62 my ($self, $column, %options) = @_;
63 my $code = $self->__select_options_rs_for->{$column};
64 my $rs = $code->($self, %options);
65 my ($value_method, $label_method) = sub {
66 my $class = shift->result_source->result_class;
67 my ($value_method) = $class->columns_by_tag('option_value');
68 my ($label_method) = $class->columns_by_tag('option_label');
69 return ($value_method, $label_method);
70 }->($rs);
71  
72 return $rs, $label_method, $value_method;
73 }
74  
75 sub select_options_for {
76 my ($self, $column, %options) = @_;
77 my ($rs, $label_method, $value_method) = $self->select_options_rs_for($column, %options);
78 my @options = map {[ $_->$label_method, $_->$value_method ]} $rs->all;
79 return \@options;
80 }
81  
82 sub add_checkbox_rs_for {
83 my ($class, $column, $code) = @_;
84 $class->__checkbox_rs_for->{$column} = $code;
85 }
86  
87 sub checkbox_rs_for {
88 my ($self, $column, %options) = @_;
89 my $code = $self->__checkbox_rs_for->{$column};
90 my $rs = $code->($self, %options);
91 my ($value_method, $label_method) = sub {
92 my $class = shift->result_source->result_class;
93 my ($value_method) = $class->columns_by_tag('checkbox_value');
94 my ($label_method) = $class->columns_by_tag('checkbox_label');
95 return ($value_method, $label_method);
96 }->($rs);
97  
98 return $rs, $label_method, $value_method;
99 }
100  
101 sub checkboxes_for {
102 my ($self, $column, %options) = @_;
103 my ($rs, $label_method, $value_method) = $self->checkbox_rs_for($column, %options);
104 my @options = map {[ $_->$label_method, $_->$value_method ]} $rs->all;
105 return \@options;
106 }
107  
108 sub add_radio_button_rs_for {
109 my ($class, $column, $code) = @_;
110 $class->__radio_button_rs_for->{$column} = $code;
111 }
112  
113 sub radio_button_rs_for {
114 my ($self, $column, %options) = @_;
115 my $code = $self->__radio_button_rs_for->{$column};
116 my $rs = $code->($self, %options);
117 my ($value_method, $label_method) = sub {
118 my $class = shift->result_source->result_class;
119 my ($value_method) = $class->columns_by_tag('radio_value');
120 my ($label_method) = $class->columns_by_tag('radio_label');
121 return ($value_method, $label_method);
122 }->($rs);
123  
124 return $rs, $label_method, $value_method;
125 }
126  
127 sub add_radio_buttons_for {
128 my ($class, $column, $code) = @_;
129 $class->__radio_buttons_for->{$column} = $code;
130 }
131 sub radio_buttons_for {
132 my ($self, $column, %options) = @_;
133 my $code = $self->__radio_buttons_for->{$column};
134 my @buttons = $code->($self, %options);
135 return @buttons;
136 }
137  
138 # $class->add_form_field_for($column);
139 # $class->add_form_field_for($column, \&code);
140 # $class->add_form_field_for($column, \%options);
141 # $class->add_form_field_for($column, \%options, \&code);
142  
143 sub add_form_field_for {
144 my ($class, $column) = (shift(@_), shift(@_));
145 my $options = (ref($_[0])||'') eq 'HASH' ? shift(@_) : +{};
146 my $code = (ref($_[0])||'') eq 'CODE' ? shift(@_) : sub { $class->_auto_read_attribute_for_html($column) };
147  
148 $class->__field_attribute_for->{$column} = [$code, $options];
149 }
150  
151 # __PACKAGE__->add_form_field_for(
152 # 'user',
153 # sub ($self) { $self->user},
154 # { label=>..., type=>..., context=>'admin' } );
155  
156 # add 'context' to this so that you can have different forms for same
157 # TODO maybe need to add way to wrap getting label and errors...
158  
159 sub has_form_fields {
160 my ($self) = @_;
161 return scalar keys %{$self->__field_attribute_for};
162 }
163  
164 sub has_form_field {
165 my ($self, $column) = @_;
166 return exists $self->__field_attribute_for->{$column};
167 }
168  
169 sub read_form_field_for {
170 my ($self, $column) = @_;
171 die "Can't find a form field for column '$column'" unless $self->has_form_field($column);
172 return $self->_read_form_field_for($column);
173 }
174  
175 sub _read_form_field_for {
176 my ($self, $column) = @_;
177 my ($code, $options) = @{$self->__field_attribute_for->{$column}};
178 return $code->($self, $column);
179 }
180  
181  
182 sub read_attribute_for_html {
183 my ($self, $attribute) = @_;
184 die "'attribute' is required argument" unless defined $attribute;
185  
186 # If at least one form field is defined for this class, that means the
187 # author wanted fine grained control over the form fields so we'll use that
188 # and only that. Otherwise we'll fall back to the auto reading method
189  
190 if($self->has_form_fields) {
191 return $self->_read_form_field_for($attribute) if $self->has_form_field($attribute);
192 } else {
193 return $self->_auto_read_attribute_for_html($attribute);
194 }
195 }
196  
197 sub _auto_read_attribute_for_html {
198 my ($self, $attribute) = @_;
199  
200 # Handle special case for 'delete' attribute and 'add' attribute
201 return $self->is_marked_for_deletion if $attribute eq '_delete';
202 return 1 if $attribute eq '_add';
203  
204 # First fallback to the normal DBIC way of getting a column value
205 return $self->get_column($attribute) if $self->result_source->has_column($attribute);
206  
207 # Second just look for a method that matches the attribute name
208 return $self->$attribute if $self->can($attribute);
209  
210 # Permit getting the value of a relationship if it's a single relationship
211 if($self->has_relationship($attribute)) {
212 my $rel_data = $self->relationship_info($attribute);
213 my $rel_type = $rel_data->{attrs}{accessor};
214 return $self->$attribute if($rel_type eq 'single');
215 }
216  
217 # If we can't find a value just return because the formhandler will decide
218 # if this is an error or not
219 return bless +{}, 'Valiant::BadAttribute';
220 }
221  
222 1;
223