Jifty/Action/Record.pm

filter: Statementstmt 12.5% 24 / 191 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 76 MC/DCmcdc 0.0% 0 / 47 Subroutinesub 29.6% 8 / 27 Podpod 100% 3 / 3 totaltotal 8.2% 35 / 422 CC 72 SCAR 51.3
CC
72
Cov
7%
_build_class_arguments
548.2
new
156.0
_argument_canonicalizer
72.0
CRAP 168.5
« Jifty/Object.pm ↑ Summary Jifty/ClassLoader.pm »
1 113 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 113 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Jifty::Action::Record;
5  
6 =head1 NAME
7  
8 Jifty::Action::Record -- An action tied to a record in the database.
9  
10 =head1 DESCRIPTION
11  
12 Represents a web-based action that is a create, update, or delete of a
13 L<Jifty::Record> object. This automatically populates the arguments
14 method of L<Jifty::Action> so that you don't need to bother.
15  
16 To actually use this class, you probably want to inherit from one of
17 L<Jifty::Action::Record::Create>, L<Jifty::Action::Record::Update>, or
18 L<Jifty::Action::Record::Delete>. You may need to override the
19 L</record_class> method, if Jifty cannot determine the record class of
20 this action.
21  
22 =cut
23  
24 113 ▶use base qw/Jifty::Action/;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 113 ▶use Scalar::Defer qw/ defer /;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 113 ▶use Scalar::Util qw/ blessed /;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 113 ▶use Clone qw/clone/;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 __PACKAGE__->mk_accessors(qw(record _cached_arguments));
30  
31 113 ▶use constant report_detailed_messages => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 our $ARGUMENT_PROTOTYPE_CACHE = {};
34  
35 =head1 METHODS
36  
37 =head2 record
38  
39 Access to the underlying L<Jifty::Record> object for this action is
40 through the C<record> accessor.
41  
42 =head2 record_class
43  
44 This method can either be overridden to return a string specifying the
45 name of the record class, or the name of the class can be passed to
46 the constructor.
47  
48 =head2 report_detailed_messages
49  
50 If the action returns true for C<report_detailed_message>, put the
51 per-field messages as C<detailed_messages> in the result content for
52 Update actions. The default is true.
53  
54 =cut
55  
56 0 ▶sub record_class {
Subroutine record_class: not called CC=6 SCAR=37.4
57 0 my $self = shift;
58 0 ▶ return $self->{record_class} ||= do {
Condition0%0/3
$self->{"record_class"} ||= do { my $class = ref $self; my $model; if ($class =~ /::(Create|Search|Execute|Update|Delete)([^:]+)$/) { $model = "Jifty"->app_class("Model", $2); undef $model unless grep {$_ eq $model;} "Jifty"->class_loader->models; } ; if ($class eq "Jifty::Action::Record") { $self->log->fatal("Jifty::Action::Record must be subclassed to be used"); } elsif (not $model) { $self->log->fatal("Cannot determine model for Jifty::Action::Record subclass $class"); } ; $model }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"record_class"} ||= do { my $class = ref $self; my $model; if ($class =~ /::(Create|Search|Execute|Update|Delete)([^:]+)$/) { $model = "Jifty"->app_class("Model", $2); undef $model unless grep {$_ eq $model;} "Jifty"->class_loader->models; } ; if ($class eq "Jifty::Action::Record") { $self->log->fatal("Jifty::Action::Record must be subclassed to be used"); } elsif (not $model) { $self->log->fatal("Cannot determine model for Jifty::Action::Record subclass $class"); } ; $model }
$self->{"record_class"} do { my $class = ref $self; my $model; if ($class =~ /::(Create|Search|Execute|Update|Delete)([^:]+)$/) { $model = "Jifty"->app_class("Model", $2); undef $model unless grep {$_ eq $model;} "Jifty"->class_loader->models; } ; if ($class eq "Jifty::Action::Record") { $self->log->fatal("Jifty::Action::Record must be subclassed to be used"); } elsif (not $model) { $self->log->fatal("Cannot determine model for Jifty::Action::Record subclass $class"); } ; $model }
Truth table0%0/3
A ||= B
A= $self->{"record_class"}B= do { my $class = ref $self; my $model; if ($class =~ /::(Create|Search|Execute|Update|Delete)([^:]+)$/) { $model = "Jifty"->app_class("Model", $2); undef $model unless grep {$_ eq $model;} "Jifty"->class_loader->models; } ; if ($class eq "Jifty::Action::Record") { $self->log->fatal("Jifty::Action::Record must be subclassed to be used"); } elsif (not $model) { $self->log->fatal("Cannot determine model for Jifty::Action::Record subclass $class"); } ; $model }
ABresult
1X1
011
000
59 0 my $class = ref($self);
60 0 my $model;
61 0 ▶ if ($class =~ /::(Create|Search|Execute|Update|Delete)([^:]+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class =~ /::(Create|Search|Execute|Update|Delete)([^:]+)$/) 0 0 0
62 0 $model = Jifty->app_class( Model => $2 );
63 0 ▶ undef $model unless grep {$_ eq $model} Jifty->class_loader->models;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $model;} "Jifty"->class_loader->models 0 0 0
64 }
65  
66 0 ▶ if ($class eq "Jifty::Action::Record") {
Branch: 2 branches
BranchTrueFalseTotal
if ($class eq 'Jifty::Action::Record') { } 0 0 0
elsif (not $model) { } 0 0 0
67 0 $self->log->fatal("Jifty::Action::Record must be subclassed to be used");
68 } elsif (not $model) {
69 0 $self->log->fatal("Cannot determine model for Jifty::Action::Record subclass $class");
70 }
71 $model
72 0 };
73 }
74  
75 =head2 new PARAMHASH
76  
77 Construct a new C<Jifty::Action::Record> (as mentioned in
78 L<Jifty::Action>, this should only be called by C<<
79 framework->new_action >>. The C<record> value, if provided in the
80 PARAMHASH, will be used to load the L</record>; otherwise, the
81 primary keys will be loaded from the action's argument values, and
82 the L</record> loaded from those primary keys.
83  
84 =cut
85  
86 0 ▶sub new {
Subroutine new: not called CC=12 SCAR=50.5
87 0 my $class = shift;
88 0 my %args = (
89 record => undef,
90 @_,
91 );
92  
93 0 my $self = $class->SUPER::new(%args);
94  
95 # Look up the record class
96 0 my $record_class = $self->record_class;
97  
98 # Die if we were given a record that wasn't a record
99 0 ▶ if (ref $args{'record'} && !$args{'record'}->isa($record_class)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{"record"} and not $args{"record"}->isa($record_class)) 0 0 0
Condition0%0/3
ref $args{"record"} and not $args{"record"}->isa($record_class)
!ll&&!rl&&r
000
MC/DC0%0/2
ref $args{"record"} and not $args{"record"}->isa($record_class)
ref $args{"record"} not $args{"record"}->isa($record_class)
Truth table0%0/3
A and not(B)
A= ref $args{"record"}B= not $args{"record"}->isa($record_class)
ABresult
0X0
100
111
100 0 Carp::confess($args{'record'}." isn't a $record_class");
101 }
102  
103 # If the record class is a record, use that
104 0 ▶ if ( ref $record_class ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $record_class) { } 0 0 0
elsif (ref $args{'record'} and $args{'record'}->isa($record_class)) { } 0 0 0
105 0 $self->record($record_class);
106 $self->argument_value( $_, $self->record->$_ )
107 0 for @{ $self->record->_primary_keys };
108 }
109  
110 # Otherwise, try to use the record passed to the constructor
111 ▶ elsif ( ref $args{record} and $args{record}->isa($record_class) ) {
Condition0%0/3
ref $args{"record"} and $args{"record"}->isa($record_class)
!ll&&!rl&&r
000
MC/DC0%0/2
ref $args{"record"} and $args{"record"}->isa($record_class)
ref $args{"record"} $args{"record"}->isa($record_class)
Truth table0%0/3
A and B
A= ref $args{"record"}B= $args{"record"}->isa($record_class)
ABresult
0X0
100
111
112 0 $self->record( $args{record} );
113 $self->argument_value( $_, $self->record->$_ )
114 0 for @{ $self->record->_primary_keys };
115 }
116
117 # Otherwise, try to use the arguments to load the record
118 else {
119 # We could leave out the explicit current user, but it'd have
120 # a slight negative performance implications
121 0 $self->record( $record_class->new( current_user => $self->current_user ) );
122 0 my %given_pks = ();
123 0 for my $pk ( @{ $self->record->_primary_keys } ) {
124 0 ▶ $given_pks{$pk} = $self->argument_value($pk)
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->argument_value($pk) and $self->argument_value($pk) ne "" 0 0 0
Condition0%0/3
defined $self->argument_value($pk) and $self->argument_value($pk) ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->argument_value($pk) and $self->argument_value($pk) ne ""
defined $self->argument_value($pk) $self->argument_value($pk) ne ""
Truth table0%0/3
A and B
A= defined $self->argument_value($pk)B= $self->argument_value($pk) ne ""
ABresult
0X0
100
111
125 if defined $self->argument_value($pk) && ($self->argument_value($pk) ne '');
126 }
127 0 ▶ $self->record->load_by_primary_keys(%given_pks) if keys %given_pks;
Branch: 1 branch
BranchTrueFalseTotal
if keys %given_pks 0 0 0
128 }
129  
130 0 return $self;
131 }
132  
133 =head2 arguments
134  
135 Overrides the L<Jifty::Action/arguments> method, to automatically
136 provide a form field for every writable attribute of the underlying
137 L</record>.
138  
139 This also creates built-in validation and autocompletion methods
140 (validate_$fieldname and autocomplete_$fieldname) for action fields
141 that are defined "validate" or "autocomplete". These methods can
142 be overridden in any Action which inherits from this class.
143  
144 Additionally, if our model class defines canonicalize_, validate_, or
145 autocomplete_ FIELD, generate appropriate an appropriate
146 canonicalizer, validator, or autocompleter that will call that method
147 with the value to be validated, canonicalized, or autocompleted.
148  
149 C<validate_FIELD> should return a (success boolean, message) list.
150  
151 C<autocomplete_FIELD> should return a the same kind of list as
152 L<Jifty::Action::_autocomplete_argument|Jifty::Action/_autocomplete_argument>
153  
154 C<canonicalized_FIELD> should return the canonicalized value.
155  
156 =cut
157  
158 0 ▶sub arguments {
Subroutine arguments: not called CC=3 SCAR=24.8
159 0 my $self = shift;
160  
161 # Don't do this twice, it's too expensive
162 0 ▶ unless ( $self->_cached_arguments ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_cached_arguments) 0 0 0
163 0 ▶ $ARGUMENT_PROTOTYPE_CACHE->{ ref($self) }
Condition0%0/3
$$Jifty::Action::Record::ARGUMENT_PROTOTYPE_CACHE{ref $self} ||= $self->_build_class_arguments
l!l&&r!l&&!r
000
MC/DC0%0/2
$$Jifty::Action::Record::ARGUMENT_PROTOTYPE_CACHE{ref $self} ||= $self->_build_class_arguments
$$Jifty::Action::Record::ARGUMENT_PROTOTYPE_CACHE{ref $self} $self->_build_class_arguments
Truth table0%0/3
A ||= B
A= $$Jifty::Action::Record::ARGUMENT_PROTOTYPE_CACHE{ref $self}B= $self->_build_class_arguments
ABresult
1X1
011
000
164 ||= $self->_build_class_arguments();
165 0 $self->_cached_arguments( $self->_fill_in_argument_record_data() );
166 }
167 0 return $self->_cached_arguments();
168 }
169  
170 0 ▶sub _fill_in_argument_record_data {
Subroutine _fill_in_argument_record_data: not called CC=4 SCAR=30.0
171 0 my $self = shift;
172  
173 0 my $arguments = clone( $ARGUMENT_PROTOTYPE_CACHE->{ ref($self) } );
174 0 ▶ return $arguments unless ( $self->record->id );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->record->id 0 0 0
175  
176 0 for my $field ( keys %$arguments ) {
177 0 ▶ if ( my $function = $self->record->can($field) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $function = $self->record->can($field)) 0 0 0
178 0 my $weakself = $self;
179 0 Scalar::Util::weaken $weakself;
180 0 ▶ $arguments->{$field}->{default_value} = defer {
Subroutine __ANON__: not called CC=3 SCAR=24.8
181 0 my $val = $function->( $weakself->record );
182  
183 # If the current value is actually a pointer to
184 # another object, turn it into an ID
185 0 ▶ return $val->id
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($val) and $val->isa("Jifty::Record") 0 0 0
Condition0%0/3
builtin::blessed($val) and $val->isa("Jifty::Record")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($val) and $val->isa("Jifty::Record")
builtin::blessed($val) $val->isa("Jifty::Record")
Truth table0%0/3
A and B
A= builtin::blessed($val)B= $val->isa("Jifty::Record")
ABresult
0X0
100
111
186 if ( blessed($val) and $val->isa('Jifty::Record') );
187 0 return $val;
188 }
189 0 }
190  
191 # The record's current value becomes the widget's default value
192 }
193 0 return $arguments;
194 }
195  
196 0 ▶sub _build_class_arguments {
Subroutine _build_class_arguments: not called CC=24 SCAR=63.1
197 0 my $self = shift;
198  
199 # Get ready to rumble
200 0 my $field_info = {};
201 0 my @columns = $self->possible_columns;
202  
203 # we use a while here because we may be modifying the fields on the fly.
204 0 ▶ while ( my $column = shift @columns ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $column = shift @columns) 0 0 0
205 0 my $info = {};
206 0 my $field = $column->name;
207  
208 # Canonicalize the render_as setting for the column
209 0 ▶ my $render_as = lc( $column->render_as || '' );
Condition0%0/2
$column->render_as || ''
l!l
00
MC/DC0%0/1
$column->render_as || ''
$column->render_as
Truth table0%0/2
A
A= $column->render_as
Aresult
11
00
210  
211 # Use a select box if we have a list of valid values
212 0 ▶ if ( defined( my $valid_values = $column->valid_values ) ) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined(my $valid_values = $column->valid_values)) { } 0 0 0
elsif (defined $column->type and $column->type =~ /^bool/i) { } 0 0 0
elsif ($render_as eq 'password') { } 0 0 0
elsif (defined(my $refers_to = $column->refers_to)) { } 0 0 0
213 0 $info->{valid_values} = $valid_values;
214 0 $info->{render_as} = 'Select';
215 }
216  
217 # Use a checkbox for boolean fields
218 ▶ elsif ( defined $column->type && $column->type =~ /^bool/i ) {
Condition0%0/3
defined $column->type and $column->type =~ /^bool/i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $column->type and $column->type =~ /^bool/i
defined $column->type $column->type =~ /^bool/i
Truth table0%0/3
A and B
A= defined $column->typeB= $column->type =~ /^bool/i
ABresult
0X0
100
111
219 0 $info->{render_as} = 'Checkbox';
220 }
221  
222 # Add an additional _confirm field for passwords
223 elsif ( $render_as eq 'password' ) {
224  
225 # Add a validator to make sure both fields match
226 0 ▶ my $same = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
227 0 my ( $self, $value ) = @_;
228 0 ▶ if ( $value ne $self->argument_value($field) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value ne $self->argument_value($field)) { } 0 0 0
229 0 return $self->validation_error(
230 ( $field . '_confirm' ) => _(
231 "The passwords you typed didn't match each other")
232 );
233 } else {
234 0 return $self->validation_ok( $field . '_confirm' );
235 }
236 0 };
237  
238 # Add the extra confirmation field
239 0 $field_info->{ $field . "_confirm" } = {
240 render_as => 'Password',
241 virtual => '1',
242 validator => $same,
243 sort_order => ( $column->sort_order + .01 ),
244 mandatory => 0
245 };
246 }
247  
248 # Handle the X-to-one references
249 elsif ( defined( my $refers_to = $column->refers_to ) ) {
250  
251 # Render as a select box unless they override
252 0 ▶ if ( UNIVERSAL::isa( $refers_to, 'Jifty::Record' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($refers_to, "Jifty::Record")) 0 0 0
253 0 ▶ $info->{render_as} = $render_as || 'Select';
Condition0%0/2
$render_as || 'Select'
l!l
00
MC/DC0%0/1
$render_as || 'Select'
$render_as
Truth table0%0/2
A
A= $render_as
Aresult
11
00
254  
255 0 ▶ $info->{render_as} = 'Text'
Branch: 1 branch
BranchTrueFalseTotal
unless $column->refers_to->enumerable 0 0 0
256 unless $column->refers_to->enumerable;
257 }
258  
259 # If it's a select box, setup the available values
260 0 ▶ if ( UNIVERSAL::isa( $refers_to, 'Jifty::Record' )
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($refers_to, 'Jifty::Record') and $info->{'render_as'} eq 'Select') { } 0 0 0
Condition0%0/3
UNIVERSAL::isa($refers_to, "Jifty::Record") and $info->{"render_as"} eq "Select"
!ll&&!rl&&r
000
MC/DC0%0/2
UNIVERSAL::isa($refers_to, "Jifty::Record") and $info->{"render_as"} eq "Select"
UNIVERSAL::isa($refers_to, "Jifty::Record") $info->{"render_as"} eq "Select"
Truth table0%0/3
A and B
A= UNIVERSAL::isa($refers_to, "Jifty::Record")B= $info->{"render_as"} eq "Select"
ABresult
0X0
100
111
261 && $info->{render_as} eq 'Select' )
262 {
263 0 $info->{'valid_values'}
264 = $self->_default_valid_values( $column, $refers_to );
265 }
266  
267 # If the reference is X-to-many instead, skip it
268 else {
269  
270 # No need to generate arguments for
271 # JDBI::Collections, as we can't do anything
272 # useful with them yet, anyways.
273  
274 # However, if the column comes with a
275 # "render_as", we can assume that the app
276 # developer know what he/she is doing.
277 # So we just render it as whatever specified.
278  
279 # XXX TODO - the next line seems to be a "next" which meeant to just fall through the else
280 # next unless $render_as;
281 }
282 }
283  
284 #########
285  
286 0 ▶ $info->{'autocompleter'} ||= $self->_argument_autocompleter($column);
Condition0%0/3
$info->{"autocompleter"} ||= $self->_argument_autocompleter($column)
l!l&&r!l&&!r
000
MC/DC0%0/2
$info->{"autocompleter"} ||= $self->_argument_autocompleter($column)
$info->{"autocompleter"} $self->_argument_autocompleter($column)
Truth table0%0/3
A ||= B
A= $info->{"autocompleter"}B= $self->_argument_autocompleter($column)
ABresult
1X1
011
000
287 0 my ( $validator, $ajax_validates )
288 = $self->_argument_validator($column);
289 0 ▶ $info->{validator} ||= $validator;
Condition0%0/3
$info->{"validator"} ||= $validator
l!l&&r!l&&!r
000
MC/DC0%0/2
$info->{"validator"} ||= $validator
$info->{"validator"} $validator
Truth table0%0/3
A ||= B
A= $info->{"validator"}B= $validator
ABresult
1X1
011
000
290 0 ▶ $info->{ajax_validates} ||= $ajax_validates;
Condition0%0/3
$info->{"ajax_validates"} ||= $ajax_validates
l!l&&r!l&&!r
000
MC/DC0%0/2
$info->{"ajax_validates"} ||= $ajax_validates
$info->{"ajax_validates"} $ajax_validates
Truth table0%0/3
A ||= B
A= $info->{"ajax_validates"}B= $ajax_validates
ABresult
1X1
011
000
291 0 my ( $canonicalizer, $ajax_canonicalizes )
292 = $self->_argument_canonicalizer($column);
293 0 ▶ $info->{'canonicalizer'} ||= $canonicalizer;
Condition0%0/3
$info->{"canonicalizer"} ||= $canonicalizer
l!l&&r!l&&!r
000
MC/DC0%0/2
$info->{"canonicalizer"} ||= $canonicalizer
$info->{"canonicalizer"} $canonicalizer
Truth table0%0/3
A ||= B
A= $info->{"canonicalizer"}B= $canonicalizer
ABresult
1X1
011
000
294 0 ▶ $info->{'ajax_canonicalizes'} ||= $ajax_canonicalizes;
Condition0%0/3
$info->{"ajax_canonicalizes"} ||= $ajax_canonicalizes
l!l&&r!l&&!r
000
MC/DC0%0/2
$info->{"ajax_canonicalizes"} ||= $ajax_canonicalizes
$info->{"ajax_canonicalizes"} $ajax_canonicalizes
Truth table0%0/3
A ||= B
A= $info->{"ajax_canonicalizes"}B= $ajax_canonicalizes
ABresult
1X1
011
000
295  
296 # If we're hand-coding a render_as, hints or label, let's use it.
297 0 for (
298 qw(render_as label hints display_length max_length mandatory sort_order container documentation attributes)
299 )
300 {
301 0 ▶ if ( defined( my $val = $column->$_ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $val = $column->$_)) 0 0 0
302 0 $info->{$_} = $val;
303 }
304 }
305  
306 0 $field_info->{$field} = $info;
307 }
308  
309 # After all that, use the schema { ... } params for the final bits
310 0 ▶ if ( $self->can('PARAMS') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can('PARAMS')) { } 0 0 0
311  
312 # User-defined declarative schema fields can override default ones here
313 0 my $params = $self->PARAMS;
314  
315 # We really, really want our sort_order to prevail over user-defined ones
316 # (as opposed to all other param fields). So we do exactly that here.
317 0 ▶ while ( my ( $key, $param ) = each %$params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$params) 0 0 0
318 0 ▶ defined( my $sort_order = $param->sort_order ) or next;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $sort_order = $param->sort_order) or next 0 0 0
319  
320 # The .99 below means that it's autogenerated by Jifty::Param::Schema.
321 0 ▶ if ( $sort_order =~ /\.99$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sort_order =~ /\.99$/) 0 0 0
322 0 $param->sort_order( $field_info->{$key}{sort_order} );
323 }
324 }
325  
326 # Cache the result of merging the Jifty::Action::Record and schema
327 # parameters
328 113 ▶ use Jifty::Param::Schema ();
Subroutine BEGIN: called CC=1 SCAR=0.0
329 0 return Jifty::Param::Schema::merge_params( $field_info, $params );
330 }
331  
332 # No schema { ... } block, so just use what we generated
333 else {
334 0 return $field_info;
335 }
336 }
337  
338 0 ▶sub _argument_validator {
Subroutine _argument_validator: not called CC=3 SCAR=24.8
339 0 my $self = shift;
340 0 my $column = shift;
341 0 my $field = $column->name;
342 0 my $do_ajax = $column->attributes->{ajax_validates};
343 0 my $method;
344  
345 # Figure out what the action's validation method would for this field
346 0 my $validate_method = "validate_" . $field;
347  
348 # Build up a validator sub if the column implements validation
349 # and we're not overriding it at the action level
350 0 ▶ if ( $column->validator and not $self->can($validate_method) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column->validator and not $self->can($validate_method)) 0 0 0
Condition0%0/3
$column->validator and not $self->can($validate_method)
!ll&&!rl&&r
000
MC/DC0%0/2
$column->validator and not $self->can($validate_method)
$column->validator not $self->can($validate_method)
Truth table0%0/3
A and not(B)
A= $column->validatorB= not $self->can($validate_method)
ABresult
0X0
100
111
351 0 $do_ajax = 1;
352 0 ▶ $method = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
353 0 my $self = shift;
354 0 my $value = shift;
355  
356 # Check the column's validator
357 my ( $is_valid, $message )
358 0 = &{ $column->validator }( $self->record, $value, @_ );
359  
360 # The validator reported valid, return OK
361 0 ▶ return $self->validation_ok($field) if ($is_valid);
Branch: 1 branch
BranchTrueFalseTotal
if $is_valid 0 0 0
362  
363 # Bad stuff, report an error
364 0 ▶ unless ($message) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($message) 0 0 0
365 0 $self->log->error(
366 qq{Schema validator for $field didn't explain why the value '$value' is invalid}
367 );
368 }
369 return (
370 0 ▶ $self->validation_error(
Condition0%0/3
$message || _(q[That doesn't look right, but I don't know why])
l!l&&r!l&&!r
000
MC/DC0%0/2
$message || _(q[That doesn't look right, but I don't know why])
$message _(q[That doesn't look right, but I don't know why])
Truth table0%0/3
A || B
A= $messageB= _(q[That doesn't look right, but I don't know why])
ABresult
1X1
011
000
371 $field => (
372 $message || _(
373 "That doesn't look right, but I don't know why")
374 )
375 )
376 );
377 }
378 0 }
379  
380 0 return ( $method, $do_ajax );
381 }
382  
383 0 ▶sub _argument_canonicalizer {
Subroutine _argument_canonicalizer: not called CC=8 SCAR=42.8
384 0 my $self = shift;
385 0 my $column = shift;
386 0 my $field = $column->name;
387 0 my $do_ajax = $column->attributes->{ajax_canonicalizes};
388 0 my $method;
389  
390 # Add a canonicalizer for the column if the record provides one
391 0 ▶ if ( $self->record->has_canonicalizer_for_column($field) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->record->has_canonicalizer_for_column($field)) { } 0 0 0
elsif (defined $column->render_as and lc $column->render_as eq 'date') { } 0 0 0
392 0 ▶ $do_ajax = 1 unless defined $column->render_as and lc( $column->render_as ) eq 'checkbox';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $column->render_as and lc $column->render_as eq "checkbox" 0 0 0
Condition0%0/3
defined $column->render_as and lc $column->render_as eq "checkbox"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $column->render_as and lc $column->render_as eq "checkbox"
defined $column->render_as lc $column->render_as eq "checkbox"
Truth table0%0/3
A and B
A= defined $column->render_asB= lc $column->render_as eq "checkbox"
ABresult
0X0
100
111
393 0 ▶ my $for = $self->isa('Jifty::Action::Record::Create') ? 'create' : 'update';
Branch: 1 branch
BranchTrueFalseTotal
$self->isa('Jifty::Action::Record::Create') ? : 0 0 0
394 0 ▶ $method ||= sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
395 0 my ( $self, $value ) = @_;
396 0 return $self->record->run_canonicalization_for_column(
397 column => $field,
398 value => $value,
399 extra => [$self->argument_values, { for => $for }],
400 );
401 0 ▶ };
Condition0%0/3
$method ||= sub { my($self, $value) = @_; return $self->record->run_canonicalization_for_column("column", $field, "value", $value, "extra", [$self->argument_values, {"for", $for}]); }
l!l&&r!l&&!r
000
MC/DC0%0/2
$method ||= sub { my($self, $value) = @_; return $self->record->run_canonicalization_for_column("column", $field, "value", $value, "extra", [$self->argument_values, {"for", $for}]); }
$method sub { my($self, $value) = @_; return $self->record->run_canonicalization_for_column("column", $field, "value", $value, "extra", [$self->argument_values, {"for", $for}]); }
Truth table0%0/3
A ||= B
A= $methodB= sub { my($self, $value) = @_; return $self->record->run_canonicalization_for_column("column", $field, "value", $value, "extra", [$self->argument_values, {"for", $for}]); }
ABresult
1X1
011
000
402 }
403  
404 # Otherwise, if it's a date, we have a built-in canonicalizer for that
405 ▶ elsif ( defined $column->render_as and lc( $column->render_as ) eq 'date' ) {
Condition0%0/3
defined $column->render_as and lc $column->render_as eq "date"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $column->render_as and lc $column->render_as eq "date"
defined $column->render_as lc $column->render_as eq "date"
Truth table0%0/3
A and B
A= defined $column->render_asB= lc $column->render_as eq "date"
ABresult
0X0
100
111
406 0 $do_ajax = 1;
407 }
408 0 return ( $method, $do_ajax );
409 }
410  
411 0 ▶sub _argument_autocompleter {
Subroutine _argument_autocompleter: not called CC=5 SCAR=34.0
412 0 my $self = shift;
413 0 my $column = shift;
414 0 my $field = $column->name;
415  
416 0 my $autocomplete;
417  
418 # What would the autocomplete method be for this column in the record
419 0 my $autocomplete_method = "autocomplete_" . $field;
420  
421 # Set the autocompleter if the record has one
422 0 ▶ if ( $self->record->can($autocomplete_method) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->record->can($autocomplete_method)) { } 0 0 0
elsif ($column->autocompleted) { } 0 0 0
423 0 ▶ $autocomplete ||= sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
424 0 my ( $self, $value ) = @_;
425 0 my %columns;
426 $columns{$_} = $self->argument_value($_)
427 0 for grep { $_ ne $field } $self->possible_fields;
428 0 return $self->record->$autocomplete_method( $value, %columns );
429 0 ▶ };
Condition0%0/3
$autocomplete ||= sub { my($self, $value) = @_; my %columns; $columns{$_} = $self->argument_value($_) foreach (grep {$_ ne $field;} $self->possible_fields); return $self->record->$autocomplete_method($value, %columns); }
l!l&&r!l&&!r
000
MC/DC0%0/2
$autocomplete ||= sub { my($self, $value) = @_; my %columns; $columns{$_} = $self->argument_value($_) foreach (grep {$_ ne $field;} $self->possible_fields); return $self->record->$autocomplete_method($value, %columns); }
$autocomplete sub { my($self, $value) = @_; my %columns; $columns{$_} = $self->argument_value($_) foreach (grep {$_ ne $field;} $self->possible_fields); return $self->record->$autocomplete_method($value, %columns); }
Truth table0%0/3
A ||= B
A= $autocompleteB= sub { my($self, $value) = @_; my %columns; $columns{$_} = $self->argument_value($_) foreach (grep {$_ ne $field;} $self->possible_fields); return $self->record->$autocomplete_method($value, %columns); }
ABresult
1X1
011
000
430 }
431  
432 # The column requests an automagically generated autocompleter, which
433 # is baed upon the values available in the field
434 elsif ( $column->autocompleted ) {
435  
436 # Auto-generated autocompleter
437 $autocomplete
438 0 ▶ ||= sub { $self->_default_autocompleter( shift, $field ) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$autocomplete ||= sub { $self->_default_autocompleter(shift(), $field); }
l!l&&r!l&&!r
000
MC/DC0%0/2
$autocomplete ||= sub { $self->_default_autocompleter(shift(), $field); }
$autocomplete sub { $self->_default_autocompleter(shift(), $field); }
Truth table0%0/3
A ||= B
A= $autocompleteB= sub { $self->_default_autocompleter(shift(), $field); }
ABresult
1X1
011
000
439  
440 }
441 0 return $autocomplete;
442 }
443  
444 0 ▶sub _default_valid_values {
Subroutine _default_valid_values: not called CC=2 SCAR=17.9
445 0 my $self = shift;
446 0 my $column = shift;
447 0 my $refers_to = shift;
448  
449 0 my @valid;
450  
451 # Get an unlimited collection
452 0 my $collection = Jifty::Collection->new(
453 record_class => $refers_to,
454 current_user => $self->record->current_user,
455 );
456 0 $collection->find_all_rows;
457  
458 # FIXME: we should get value_from with the actualy refered by key
459  
460 # Setup the list of valid values
461 0 @valid = (
462 { display_from => $refers_to->_brief_description(),
463 value_from => 'id',
464 collection => $collection
465 }
466 );
467 0 ▶ unshift @valid,
Branch: 1 branch
BranchTrueFalseTotal
unless $column->mandatory 0 0 0
468 {
469 display => _('no value'),
470 value => ''
471 }
472 unless $column->mandatory;
473 0 return \@valid;
474  
475 }
476  
477 0 ▶sub _default_autocompleter {
Subroutine _default_autocompleter: not called CC=3 SCAR=24.8
478 0 my ( $self, $value, $field ) = @_;
479  
480 0 my $collection = Jifty::Collection->new(
481 record_class => $self->record_class,
482 current_user => $self->record->current_user
483 );
484  
485 # Return the first 20 matches...
486 0 $collection->rows_per_page(20);
487  
488 # ...that start with the value typed...
489 0 ▶ if ( length $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $value) 0 0 0
490 0 $collection->limit(
491 column => $field,
492 value => $value,
493 operator => 'STARTSWITH',
494 entry_aggregator => 'AND'
495 );
496 }
497  
498 # ...but are not NULL...
499 $collection->limit(
500 0 column => $field,
501 value => 'NULL',
502 operator => 'IS NOT',
503 entry_aggregator => 'AND'
504 );
505  
506 # ...and are not empty.
507 0 $collection->limit(
508 column => $field,
509 value => '',
510 operator => '!=',
511 entry_aggregator => 'AND'
512 );
513  
514 # Optimize the query a little bit
515 0 $collection->columns( 'id', $field );
516 0 $collection->order_by( column => $field );
517 0 $collection->group_by( column => $field );
518  
519 # Set up the list of choices to return
520 0 my @choices;
521 0 ▶ while ( my $record = $collection->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $record = $collection->next) 0 0 0
522 0 push @choices, $record->$field;
523 }
524 0 return @choices;
525 }
526  
527 =head2 possible_columns
528  
529 Returns the list of columns objects on the object that the action
530 can update. This defaults to all of the C<containers> or the non-C<private>,
531 non-C<virtual> and non-C<serial> columns of the object.
532  
533 =cut
534  
535 0 ▶sub possible_columns {
Subroutine possible_columns: not called CC=5 SCAR=34.0
Pod documented
536 0 my $self = shift;
537 return grep {
538 0 ▶ $_->container
Condition0%0/12
$_->container || !($_->private || $_->virtual || $_->computed) && $_->type ne 'serial'
l!l&&r!l&&!r
000
not $_->private || $_->virtual || $_->computed and $_->type ne "serial"
!ll&&!rl&&r
000
$_->private || $_->virtual || $_->computed
l!l&&r!l&&!r
000
$_->private or $_->virtual
l!l&&r!l&&!r
000
MC/DC0%0/5
$_->container || !($_->private || $_->virtual || $_->computed) && $_->type ne 'serial'
$_->container $_->private $_->virtual $_->computed $_->type ne "serial"
Truth table0%0/6
A || not(B or C || D) and E
A= $_->containerB= $_->privateC= $_->virtualD= $_->computedE= $_->type ne "serial"
ABCDEresult
1XXXX1
000011
01XXX0
001XX0
0001X0
000000
539 or ( !$_->private and !$_->virtual and !$_->computed and $_->type ne "serial" )
540 } $self->record->columns;
541 }
542  
543 =head2 possible_fields
544  
545 Returns the list of the L</possible_columns>' names.
546  
547 Usually at the end names are required, however for subclassing column objects
548 are better, or this method in a subclass turns out to be "map to column" -
549 "filter" - "map to name" chain.
550  
551 =cut
552  
553 0 ▶sub possible_fields {
Subroutine possible_fields: not called CC=1 SCAR=6.9
Pod documented
554 0 my $self = shift;
555 0 return map { $_->name } $self->possible_columns;
556 }
557  
558 =head2 take_action
559  
560 Throws an error unless it is overridden; use
561 Jifty::Action::Record::Create, ::Update, or ::Delete
562  
563 =cut
564  
565 0 ▶sub take_action {
Subroutine take_action: not called CC=1 SCAR=6.9
Pod documented
566 0 my $self = shift;
567 0 $self->log->fatal(
568 "Use one of the Jifty::Action::Record subclasses, ::Create, ::Update or ::Delete or ::Search"
569 );
570 }
571  
572 =head1 SEE ALSO
573  
574 L<Jifty::Action>, L<Jifty::Record>, L<Jifty::DBI::Record>,
575 L<Jifty::Action::Record::Create>, L<Jifty::Action::Record::Update>,
576 L<Jifty::Action::Record::Delete>
577  
578 =head1 LICENSE
579  
580 Jifty is Copyright 2005-2010 Best Practical Solutions, LLC.
581 Jifty is distributed under the same terms as Perl itself.
582  
583 =cut
584  
585 1;
« Jifty/Object.pm ↑ Summary Jifty/ClassLoader.pm »