Jifty/Record.pm

filter: Statementstmt 8.8% 20 / 227 Branchbran 1.0% 1 / 92 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 12.0% 6 / 50 Podpod 100% 34 / 34 totaltotal 13.4% 61 / 454 CC 63 SCAR 42.1
CC
63
Cov
6%
create
272.0
current_user_can
132.0
jifty_serialize_format
110.0
CRAP 67.5
« Jifty/Plugin/SinglePage.pm ↑ Summary Jifty/Plugin/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::Record;
5  
6 113 ▶use Jifty::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 113 ▶use List::MoreUtils qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Jifty::Record - Represents a Jifty object that lives in the database.
12  
13 =head1 DESCRIPTION
14  
15 C<Jifty::Record> is a kind of L<Jifty::Object> that has a database
16 representation; that is, it is also a L<Jifty::DBI::Record> as well.
17  
18 =cut
19  
20 113 ▶use base qw( Jifty::Object Jifty::DBI::Record);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 113 __PACKAGE__->mk_accessors('_is_readable');
23  
24 0 ▶sub _init {
Subroutine _init: not called CC=1 SCAR=6.9
25 0 my $self = shift;
26 0 my %args = (@_);
27 0 $self->_get_current_user(%args);
28  
29 0 $self->SUPER::_init(@_);
30 }
31  
32 =head1 METHODS
33  
34 =cut
35  
36 =head2 create PARAMHASH
37  
38 C<create> can be called as either a class method or an object method.
39  
40 Takes an array of key-value pairs and inserts a new row into the
41 database representing this object.
42  
43 Overrides L<Jifty::DBI::Record> in these ways:
44  
45 =over 4
46  
47 =item Remove C<id> values unless they are truly numeric
48  
49 =item Automatically load by id after create
50  
51 =item actually stop creating the new record if a field fails to validate.
52  
53 =back
54  
55 =cut
56  
57 0 ▶sub create {
Subroutine create: not called CC=16 SCAR=56.1
Pod documented
58 0 my $class = shift;
59 0 my $self;
60 0 ▶ if ( ref($class) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) { } 0 0 0
61 0 ( $self, $class ) = ( $class, undef );
62 } else {
63 0 $self = $class->new();
64 }
65  
66 0 my %attribs = @_;
67  
68 0 ▶ unless ( $self->check_create_rights(@_) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->check_create_rights(@_)) 0 0 0
69 0 $self->log->error( $self->current_user->id . " tried to create a ",
70 ref $self, " without permission" );
71 0 ▶ wantarray ? return ( 0, _('Permission denied') ) : return (0);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
72 }
73  
74 0 foreach my $key ( keys %attribs ) {
75 $attribs{$key} = $self->run_canonicalization_for_column(
76 column => $key,
77 0 value => $attribs{$key},
78 extra => [\%attribs, { for => 'create' }],
79 );
80 }
81 0 foreach my $key ( keys %attribs ) {
82 0 my $attr = $attribs{$key};
83 my ( $val, $msg ) = $self->run_validation_for_column(
84 column => $key,
85 0 value => $attribs{$key},
86 extra => [\%attribs, { for => 'create' }],
87 );
88 0 ▶ if ( not $val ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($val) 0 0 0
89 0 $self->log->error("There was a validation error for $key");
90 0 ▶ if ($class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class) { } 0 0 0
91 0 return ($self);
92 } else {
93 0 return ( $val, $msg );
94 }
95 }
96  
97 # remove blank values. We'd rather have nulls
98 0 ▶ if ( exists $attribs{$key}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $attribs{$key} and !defined($attr) || !ref($attr) && $attr eq "") 0 0 0
Condition0%0/9
exists $attribs{$key} and !defined($attr) || !ref($attr) && $attr eq ""
!ll&&!rl&&r
000
not defined $attr or not ref $attr and $attr eq ""
l!l&&r!l&&!r
000
not ref $attr and $attr eq ""
!ll&&!rl&&r
000
MC/DC0%0/4
exists $attribs{$key} and !defined($attr) || !ref($attr) && $attr eq ""
exists $attribs{$key} not defined $attr not ref $attr $attr eq ""
Truth table0%0/5
A and not(B) or not(C) and D
A= exists $attribs{$key}B= not defined $attrC= not ref $attrD= $attr eq ""
ABCDresult
0XXX0
100X0
10100
11XX1
10111
99 and ( !defined $attr || ( not ref($attr) and $attr eq '' ) ) )
100 {
101 0 delete $attribs{$key};
102 }
103 }
104  
105 0 my $msg = $self->SUPER::create(%attribs);
106 0 ▶ if ( ref($msg) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $msg) 0 0 0
107  
108 # It's a Class::ReturnValue
109 0 return $msg;
110 }
111 0 my ( $id, $status ) = $msg;
112 0 ▶ $self->load_by_cols( id => $id ) if ($id);
Branch: 1 branch
BranchTrueFalseTotal
if $id 0 0 0
113 0 ▶ if ($class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class) { } 0 0 0
114 0 return $self;
115 } else {
116 0 ▶ return wantarray ? ( $id, $status ) : $id;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
117 }
118 }
119  
120 =head2 id
121  
122 Returns the record id value.
123 This routine short-circuits a much heavier call up through Jifty::DBI
124  
125 =cut
126  
127 0 ▶sub _primary_key {'id'}
Subroutine _primary_key: not called CC=1 SCAR=6.9
128 0 ▶sub id { $_[0]->{'values'}->{'id'} }
Subroutine id: not called CC=1 SCAR=6.9
Pod documented
129  
130 =head2 load_or_create
131  
132 C<load_or_create> can be called as either a class method or an object method.
133 It attempts to load a record with the named parameters passed in. If it
134 can't do so, it creates a new record.
135  
136 =cut
137  
138 0 ▶sub load_or_create {
Subroutine load_or_create: not called CC=3 SCAR=24.8
Pod documented
139 0 my $class = shift;
140 0 my $self;
141 0 ▶ if ( ref($class) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) { } 0 0 0
142 0 ( $self, $class ) = ( $class, undef );
143 } else {
144 0 $self = $class->new();
145 }
146  
147 0 my %args = (@_);
148  
149 0 my ( $id, $msg ) = $self->load_by_cols(%args);
150 0 ▶ unless ( $self->id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->id) 0 0 0
151 0 return $self->create(%args);
152 }
153  
154 0 return ( $id, $msg );
155 }
156  
157 =head2 as_create_action PARAMHASH
158  
159 Returns the L<Jifty::Action::Record::Create> action for this model
160 class.
161  
162 The PARAMHASH allows you to add additional parameters to pass to
163 L<Jifty::Web/new_action>.
164  
165 =cut
166  
167 0 ▶sub _action_from_record {
Subroutine _action_from_record: not called CC=2 SCAR=17.9
168 0 my $self = shift;
169 0 my $verb = shift;
170 0 ▶ my $class = ref $self || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
171 0 $class =~ s/::Model::/::Action::$verb/;
172 0 return $class;
173 }
174  
175 0 ▶sub as_create_action {
Subroutine as_create_action: not called CC=1 SCAR=6.9
Pod documented
176 0 my $self = shift;
177 0 my $action_class = $self->_action_from_record('Create');
178 0 return Jifty->web->new_action( class => $action_class, @_ );
179 }
180  
181 =head2 as_update_action PARAMHASH
182  
183 Returns the L<Jifty::Action::Record::Update> action for this model
184 class. The current record is passed to the constructor.
185  
186 The PARAMHASH allows you to add additional parameters to pass to
187 L<Jifty::Web/new_action>.
188  
189 =cut
190  
191 0 ▶sub as_update_action {
Subroutine as_update_action: not called CC=1 SCAR=6.9
Pod documented
192 0 my $self = shift;
193 0 my $action_class = $self->_action_from_record('Update');
194 0 return Jifty->web->new_action(
195 class => $action_class,
196 record => $self,
197 @_,
198 );
199 }
200  
201 =head2 as_delete_action PARAMHASH
202  
203 Returns the L<Jifty::Action::Record::Delete> action for this model
204 class. The current record is passed to the constructor.
205  
206 The PARAMHASH allows you to add additional parameters to pass to
207 L<Jifty::Web/new_action>.
208  
209 =cut
210  
211 0 ▶sub as_delete_action {
Subroutine as_delete_action: not called CC=1 SCAR=6.9
Pod documented
212 0 my $self = shift;
213 0 my $action_class = $self->_action_from_record('Delete');
214 0 return Jifty->web->new_action(
215 class => $action_class,
216 record => $self,
217 @_,
218 );
219 }
220  
221 =head2 as_search_action PARAMHASH
222  
223 Returns the L<Jifty::Action::Record::Search> action for this model
224 class.
225  
226 The PARAMHASH allows you to add additional parameters to pass to
227 L<Jifty::Web/new_action>.
228  
229 =cut
230  
231 0 ▶sub as_search_action {
Subroutine as_search_action: not called CC=1 SCAR=6.9
Pod documented
232 0 my $self = shift;
233 0 my $action_class = $self->_action_from_record('Search');
234 0 return Jifty->web->new_action(
235 class => $action_class,
236 @_,
237 );
238 }
239  
240 =head2 _guess_table_name
241  
242 Guesses a table name based on the class's last part. In addition to
243 the work performed in L<Jifty::DBI::Record>, this method also prefixes
244 the table name with the plugin table prefix, if the model belongs to a
245 plugin.
246  
247 =cut
248  
249 0 ▶sub _guess_table_name {
Subroutine _guess_table_name: not called CC=4 SCAR=30.0
250 0 my $self = shift;
251 0 my $table = $self->SUPER::_guess_table_name;
252  
253 # Add plugin table prefix if a plugin model
254 0 ▶ my $class = ref($self) ? ref($self) : $self;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 0 0 0
255 0 my $app_plugin_root = Jifty->app_class({require => 0}, 'Plugin');
256 0 ▶ if ( $class =~ /^(?:Jifty::Plugin::|$app_plugin_root)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class =~ /^(?:Jifty::Plugin::|$app_plugin_root)/) 0 0 0
257  
258 # Guess the plugin class name
259 0 my $plugin_class = $class;
260 0 $plugin_class =~ s/::Model::(.*)$//;
261  
262 # Try to load that plugin's configuration
263 0 my ($plugin) = grep { ref $_ eq $plugin_class } Jifty->plugins;
264  
265 # Add the prefix if found
266 0 ▶ if ( defined $plugin ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $plugin) { } 0 0 0
267 0 $table = $plugin->table_prefix . $table;
268 }
269  
270 # Uh oh. Warn, but try to keep going.
271 else {
272 0 warn
273 "Model $class looks like a plugin model, but $plugin_class could not be found.";
274 }
275 }
276  
277 0 return $table;
278 }
279  
280 =head2 current_user_can RIGHT [ATTRIBUTES]
281  
282 Should return true if the current user (C<< $self->current_user >>) is
283 allowed to do I<RIGHT>. Possible values for I<RIGHT> are:
284  
285 =over
286  
287 =item create
288  
289 Called just before an object's C<create> method is called, as well as
290 before parameter validation. ATTRIBUTES is the attributes that
291 the object is trying to be created with, as the attributes aren't on
292 the object yet to be inspected.
293  
294 =item read
295  
296 Called before any attribute is accessed on the object.
297 ATTRIBUTES is a hash with a single key C<column> and a single
298 value, the name of the column being queried.
299  
300 =item update
301  
302 Called before any attribute is changed on the object.
303 ATTRIBUTES is a hash of the arguments passed to _set.
304  
305 =item delete
306  
307 Called before the object is deleted.
308  
309 =back
310  
311 Models wishing to customize authorization checks should override this
312 method. You can do so like this:
313  
314 sub current_user_can {
315 my ($self, $right, %args) = @_;
316  
317 # Make any custom checks that return 1 to allow or return 0 to deny...
318
319 # Fallback upon the default implementation to handle the
320 # SkipAccessControl configuration setting, superuser, bootstrap,
321 # delegation, and the before_access hook
322 return $self->SUPER::current_user_can($right, %args);
323 }
324  
325 If you are sure you don't want your model to fallback using the
326 default implementation, you can replace the last line with whatever
327 fallback policy required.
328  
329 =head3 Authorization steps
330  
331 The default implementation proceeds as follows:
332  
333 =over
334  
335 =item 1.
336  
337 If the C<SkipAccessControl> setting is set to a true value in the
338 framework configuration section of F<etc/config.yml>,
339 C<current_user_can> always returns true.
340  
341 =item 2.
342  
343 The method first attempts to call the C<before_access> hooks to check
344 for any allow or denial. See L</The before_access hook>.
345  
346 =item 3.
347  
348 Next, the default implementation returns true if the current user is a
349 superuser or a bootstrap user.
350  
351 =item 4.
352  
353 Then, if the model can perform delegation, usually by using
354 L<Jifty::RightsFrom>, the access control decision is deferred to
355 another object (via the C<delegate_current_user_can> subroutine).
356  
357 =item 5.
358  
359 Otherwise, it returns false.
360  
361 =back
362  
363 =head3 The before_access hook
364  
365 This implementation may make use of a trigger called C<before_access>
366 to make the decision. A new handler can be added to the trigger point
367 by calling C<add_handler>:
368  
369 $record->add_trigger(
370 name => 'before_access',
371 code => \&before_access,
372 abortable => 1,
373 );
374  
375 The C<before_access> handler will be passed the same arguments that
376 were used to call C<current_user_can>, including the current record
377 object, the operation being checked, and any arguments being passed to
378 the operation.
379  
380 The C<before_access> handler should return one of three strings:
381 C<'deny'>, C<'allow'>, or C<'ignore'>. The C<current_user_can>
382 implementation reacts as follows to these results:
383  
384 =over
385  
386 =item 1.
387  
388 If a handler is abortable and aborts by returning a false value (such
389 as C<undef>), C<current_user_can> returns false.
390  
391 =item 2.
392  
393 If any handler returns 'deny', C<current_user_can> returns false.
394  
395 =item 3.
396  
397 If any handler returns 'allow' and no handler returns 'deny',
398 C<current_user_can> returns true.
399  
400 =item 4.
401  
402 In all other cases, the results of the handlers are ignored and
403 C<current_user_can> proceeds to check using superuser, bootstrap, and
404 delegation.
405  
406 =back
407  
408 =cut
409  
410 0 ▶sub current_user_can {
Subroutine current_user_can: not called CC=11 SCAR=48.8
Pod documented
411 0 my $self = shift;
412 0 my $right = shift;
413  
414 # Turn off access control for the whole application
415 0 ▶ if ( Jifty->config->framework('SkipAccessControl') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Jifty"->config->framework("SkipAccessControl")) 0 0 0
416 0 return 1;
417 }
418  
419 0 my $hook_status = $self->call_trigger( before_access => $right, @_ );
420  
421 # If not aborted...
422 0 ▶ if ( defined $hook_status ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hook_status) { } 0 0 0
423  
424 # Compile the handler results
425 0 my %results;
426 0 $results{ $_->[0] }++ for ( @{ $self->last_trigger_results } );
427  
428 # Deny always takes precedent
429 0 ▶ if ( $results{deny} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($results{'deny'}) { } 0 0 0
elsif ($results{'allow'}) { } 0 0 0
430 0 return 0;
431 }
432  
433 # Then allow...
434 elsif ( $results{allow} ) {
435 0 return 1;
436 }
437  
438 # Otherwise, no instruction from the handlers, move along...
439 }
440  
441 # Abort! Return false for safety if the hook exploded
442 else {
443 0 return 0;
444 }
445  
446  
447 0 ▶ Carp::confess "No current user" unless ( $self->current_user );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->current_user 0 0 0
448 0 ▶ if ( $self->current_user->is_bootstrap_user
Branch: 1 branch
BranchTrueFalseTotal
if ($self->current_user->is_bootstrap_user or $self->current_user->is_superuser) 0 0 0
Condition0%0/3
$self->current_user->is_bootstrap_user or $self->current_user->is_superuser
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->current_user->is_bootstrap_user or $self->current_user->is_superuser
$self->current_user->is_bootstrap_user $self->current_user->is_superuser
Truth table0%0/3
A or B
A= $self->current_user->is_bootstrap_userB= $self->current_user->is_superuser
ABresult
1X1
011
000
449 or $self->current_user->is_superuser )
450 {
451 0 return (1);
452 }
453  
454 0 ▶ if ( $self->can('delegate_current_user_can') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("delegate_current_user_can")) 0 0 0
455 0 return $self->delegate_current_user_can( $right, @_ );
456 }
457  
458 0 ▶ unless ( $self->current_user->isa('Jifty::CurrentUser') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->current_user->isa("Jifty::CurrentUser")) 0 0 0
459 0 $self->log->error(
460 "Hm. called to authenticate without a currentuser - "
461 . $self->current_user );
462 0 return (0);
463 }
464 0 return (0);
465  
466 }
467  
468 =head2 check_create_rights ATTRIBUTES
469  
470 Internal helper to call L</current_user_can> with C<create>.
471  
472 =cut
473  
474 0 ▶sub check_create_rights { return shift->current_user_can( 'create', @_ ) }
Subroutine check_create_rights: not called CC=1 SCAR=6.9
Pod documented
475  
476 =head2 check_read_rights
477  
478 Internal helper to call L</current_user_can> with C<read>.
479  
480 Passes C<column> as a named parameter for the column the user is checking rights on.
481  
482 =cut
483  
484 0 ▶sub check_read_rights {
Subroutine check_read_rights: not called CC=2 SCAR=17.9
Pod documented
485 0 my $self = shift;
486 0 ▶ return (1) if $self->_is_readable;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_readable 0 0 0
487 0 return $self->current_user_can( 'read', column => shift );
488 }
489  
490 =head2 check_update_rights
491  
492 Internal helper to call L</current_user_can> with C<update>.
493  
494 =cut
495  
496 0 ▶sub check_update_rights { return shift->current_user_can( 'update', @_ ) }
Subroutine check_update_rights: not called CC=1 SCAR=6.9
Pod documented
497  
498 =head2 check_delete_rights
499  
500 Internal helper to call L</current_user_can> with C<delete>.
501  
502 =cut
503  
504 0 ▶sub check_delete_rights { return shift->current_user_can( 'delete', @_ ) }
Subroutine check_delete_rights: not called CC=1 SCAR=6.9
Pod documented
505  
506 0 ▶sub _set {
Subroutine _set: not called CC=2 SCAR=17.9
507 0 my $self = shift;
508  
509 0 ▶ unless ( $self->check_update_rights(@_) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->check_update_rights(@_)) 0 0 0
510 0 return ( 0, _('Permission denied') );
511 }
512 0 $self->SUPER::_set(@_);
513 }
514  
515 0 ▶sub _value {
Subroutine _value: not called CC=5 SCAR=34.0
516 0 my $self = shift;
517 0 my $column = shift;
518  
519 0 ▶ unless ( $self->check_read_rights( $column => @_ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->check_read_rights($column, @_)) 0 0 0
520 0 return (undef);
521 }
522 0 my $value = $self->SUPER::_value( $column => @_ );
523 0 ▶ return $value if ref $value or $self->column($column)->type eq 'blob';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value or $self->column($column)->type eq "blob" 0 0 0
Condition0%0/3
ref $value or $self->column($column)->type eq "blob"
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $value or $self->column($column)->type eq "blob"
ref $value $self->column($column)->type eq "blob"
Truth table0%0/3
A or B
A= ref $valueB= $self->column($column)->type eq "blob"
ABresult
1X1
011
000
524  
525 0 ▶ Encode::_utf8_on($value) if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 0 0
526 0 $value;
527 }
528  
529 =head2 as_user CurrentUser
530  
531 Returns a copy of this object with the current_user set to the given
532 current_user. This is a way to act on behalf of a particular user (perhaps the
533 owner of the object)
534  
535 =cut
536  
537 0 ▶sub as_user {
Subroutine as_user: not called CC=1 SCAR=6.9
Pod documented
538 0 my $self = shift;
539 0 my $user = shift;
540  
541 0 my $clone = $self->new( current_user => $user );
542 0 $clone->load( $self->id );
543 0 return $clone;
544 }
545  
546 =head2 as_superuser
547  
548 Returns a copy of this object with the current_user set to the
549 superuser. This is a convenient way to duck around ACLs if you have
550 code that needs to for some reason or another.
551  
552 =cut
553  
554 0 ▶sub as_superuser {
Subroutine as_superuser: not called CC=1 SCAR=6.9
Pod documented
555 0 my $self = shift;
556 0 return $self->as_user( $self->current_user->superuser );
557 }
558  
559 =head2 delete PARAMHASH
560  
561 Overrides L<Jifty::DBI::Record> to check the delete ACL.
562  
563 =cut
564  
565 0 ▶sub delete {
Subroutine delete: not called CC=2 SCAR=17.9
Pod documented
566 0 my $self = shift;
567 0 ▶ unless ( $self->check_delete_rights(@_) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->check_delete_rights(@_)) 0 0 0
568 0 $self->log->logcluck("Permission denied");
569 0 return ( 0, _('Permission denied') );
570 }
571 0 $self->SUPER::delete(@_);
572 }
573  
574 =head2 brief_description
575  
576 Display the friendly name of the record according to _brief_description.
577  
578 To change what this returns, override L<_brief_description> instead.
579  
580 =cut
581  
582 0 ▶sub brief_description {
Subroutine brief_description: not called CC=1 SCAR=6.9
Pod documented
583 0 my $self = shift;
584 0 my $method = $self->_brief_description;
585 0 return $self->$method;
586 }
587  
588 =head2 _brief_description
589  
590 When displaying a list of records, Jifty can display a friendly value rather
591 than the column's unique id. Out of the box, Jifty always tries to display the
592 'name' field from the record. If there is no 'name' field, Jifty falls back to
593 the record id.
594  
595 You can override this method to return the name of a method on your record
596 class which will return a nice short human readable description for this
597 record.
598  
599 =cut
600  
601 0 ▶sub _brief_description {
Subroutine _brief_description: not called CC=2 SCAR=17.9
602 0 my $self = shift;
603 0 ▶ return 'name' if $self->can('name');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("name") 0 0 0
604 0 return 'id';
605 }
606  
607 =head2 null_reference
608  
609 By default, L<Jifty::DBI::Record> returns C<undef> on non-existent
610 related fields; Jifty prefers to get back an object with an undef id.
611  
612 =cut
613  
614 0 ▶sub null_reference { 0 }
Subroutine null_reference: not called CC=1 SCAR=6.9
Pod documented
615  
616 =head2 _new_collection_args
617  
618 Overrides the default arguments which this collection passes to new
619 collections, to pass the C<current_user>.
620  
621 =cut
622  
623 0 ▶sub _new_collection_args {
Subroutine _new_collection_args: not called CC=1 SCAR=6.9
624 0 my $self = shift;
625 0 return ( current_user => $self->current_user );
626 }
627  
628 =head2 _new_record_args
629  
630 Overrides the default arguments which this collection passes to new
631 records, to pass the C<current_user>.
632  
633 =cut
634  
635 0 ▶sub _new_record_args {
Subroutine _new_record_args: not called CC=1 SCAR=6.9
636 0 my $self = shift;
637 0 return ( current_user => $self->current_user );
638 }
639  
640 =head2 cache_key_prefix
641  
642 Returns a unique key for this application for the Memcached cache.
643 This should be global within a given Jifty application instance.
644  
645 =cut
646  
647 0 ▶sub cache_key_prefix {
Subroutine cache_key_prefix: not called CC=1 SCAR=6.9
Pod documented
648 0 Jifty->config->framework('Database')->{'Database'};
649 }
650  
651 0 ▶sub _cache_config {
Subroutine _cache_config: not called CC=1 SCAR=6.9
652 0 { 'cache_p' => 1,
653 'cache_for_sec' => 60,
654 };
655 }
656  
657 =head2 since
658  
659 By default, all models exist since C<undef>, the ur-time when the
660 application was created. Please override it for your model class.
661  
662 =cut
663  
664 =head2 printable_table_schema
665  
666 When called, this method will generate the SQL schema for the current
667 version of this class and return it as a scalar, suitable for printing
668 or execution in your database's command line.
669  
670 =cut
671  
672 0 ▶sub printable_table_schema {
Subroutine printable_table_schema: not called CC=1 SCAR=6.9
Pod documented
673 0 my $class = shift;
674  
675 0 my $schema_gen = $class->_make_schema();
676 0 return $schema_gen->create_table_sql_text;
677 }
678  
679  
680 =head2 table_schema_statements
681  
682 When called, this method will generate the SQL schema statements
683 for the current version of this class and return it as array.
684  
685 =cut
686  
687 0 ▶sub table_schema_statements {
Subroutine table_schema_statements: not called CC=1 SCAR=6.9
Pod documented
688 0 my $class = shift;
689  
690 0 my $schema_gen = $class->_make_schema();
691 0 return $schema_gen->create_table_sql_statements;
692 }
693  
694  
695  
696  
697 =head2 create_table_in_db
698  
699 When called, this method will generate the SQL schema for the current
700 version of this class and insert it into the application's currently
701 open database.
702  
703 =cut
704  
705 0 ▶sub create_table_in_db {
Subroutine create_table_in_db: not called CC=3 SCAR=24.8
Pod documented
706 0 my $class = shift;
707  
708 # Run all CREATE commands
709 0 for my $statement ( $class->table_schema_statements ) {
710 0 my $ret = Jifty->handle->simple_query($statement);
711 0 ▶ $ret or die "error creating table $class: " . $ret->error_message;
Branch: 1 branch
BranchTrueFalseTotal
$ret or die "error creating table ${class}: " . $ret->error_message 0 0 0
712 }
713  
714 }
715  
716 =head2 drop_table_in_db
717  
718 When called, this method will generate the SQL to remove this model's
719 table in the database and execute it in the application's currently
720 open database. This method can destroy a lot of data. Be sure you
721 know what you're doing.
722  
723  
724 =cut
725  
726 0 ▶sub drop_table_in_db {
Subroutine drop_table_in_db: not called CC=2 SCAR=17.9
Pod documented
727 0 my $self = shift;
728 0 my $ret = Jifty->handle->simple_query( 'DROP TABLE ' . $self->table );
729 0 ▶ $ret or die "error removing table $self: " . $ret->error_message;
Branch: 1 branch
BranchTrueFalseTotal
$ret or die "error removing table ${self}: " . $ret->error_message 0 0 0
730 }
731  
732 0 ▶sub _make_schema {
Subroutine _make_schema: not called CC=3 SCAR=24.8
733 0 my $class = shift;
734  
735 0 require Jifty::DBI::SchemaGenerator;
736 0 ▶ my $schema_gen = Jifty::DBI::SchemaGenerator->new( Jifty->handle )
Branch: 1 branch
BranchTrueFalseTotal
my $schema_gen = "Jifty::DBI::SchemaGenerator"->new("Jifty"->handle) or die "Can't make Jifty::DBI::SchemaGenerator" 0 0 0
737 or die "Can't make Jifty::DBI::SchemaGenerator";
738 0 my $ret = $schema_gen->add_model( $class->new );
739 0 ▶ $ret or die "couldn't add model $class: " . $ret->error_message;
Branch: 1 branch
BranchTrueFalseTotal
$ret or die "couldn't add model ${class}: " . $ret->error_message 0 0 0
740  
741 0 return $schema_gen;
742 }
743  
744 =head2 add_column_sql column_name
745  
746 Returns the SQL statement necessary to add C<column_name> to this
747 class's representation in the database
748  
749 =cut
750  
751 0 ▶sub add_column_sql {
Subroutine add_column_sql: not called CC=1 SCAR=6.9
Pod documented
752 0 my $self = shift;
753 0 my $column_name = shift;
754  
755 0 my $col = $self->column($column_name);
756 0 my $definition = $self->_make_schema()
757 ->column_definition_sql( $self->table => $col->name );
758 0 return "ALTER TABLE " . $self->table . " ADD COLUMN " . $definition;
759 }
760  
761 =head2 add_column_in_db column_name
762  
763 Executes the SQL code generated by add_column_sql. Dies on failure.
764  
765 =cut
766  
767 0 ▶sub add_column_in_db {
Subroutine add_column_in_db: not called CC=2 SCAR=17.9
Pod documented
768 0 my $self = shift;
769 0 my $ret = Jifty->handle->simple_query( $self->add_column_sql(@_) );
770 0 ▶ $ret
Branch: 1 branch
BranchTrueFalseTotal
$ret or die 'error adding column ' . $_[0] . " to ${self}: " . $ret->error_message 0 0 0
771 or die "error adding column "
772 . $_[0]
773 . " to $self: "
774 . $ret->error_message;
775  
776 }
777  
778 =head2 drop_column_sql column_name
779  
780 Returns the SQL statement necessary to remove C<column_name> from this
781 class's representation in the database
782  
783 =cut
784  
785 0 ▶sub drop_column_sql {
Subroutine drop_column_sql: not called CC=1 SCAR=6.9
Pod documented
786 0 my $self = shift;
787 0 my $column_name = shift;
788  
789 0 my $col = $self->column($column_name);
790 0 return "ALTER TABLE " . $self->table . " DROP COLUMN " . $col->name;
791 }
792  
793 =head2 drop_column_in_db column_name
794  
795 Executes the SQL code generated by drop_column_sql. Dies on failure.
796  
797 =cut
798  
799 0 ▶sub drop_column_in_db {
Subroutine drop_column_in_db: not called CC=2 SCAR=17.9
Pod documented
800 0 my $self = shift;
801 0 my $ret = Jifty->handle->simple_query( $self->drop_column_sql(@_) );
802 0 ▶ $ret
Branch: 1 branch
BranchTrueFalseTotal
$ret or die 'error dropping column ' . $_[0] . " to ${self}: " . $ret->error_message 0 0 0
803 or die "error dropping column "
804 . $_[0]
805 . " to $self: "
806 . $ret->error_message;
807  
808 }
809  
810 =head2 schema_version
811  
812 This method is used by L<Jifty::DBI::Record> to determine which schema
813 version is in use. It returns the current database version stored in
814 the configuration.
815  
816 Jifty's notion of the schema version is currently broken into two:
817  
818 =over
819  
820 =item 1.
821  
822 The Jifty version is the first. In the case of models defined by Jifty
823 itself, these use the version found in C<$Jifty::VERSION>.
824  
825 =item 2.
826  
827 Any model defined by your application use the database version
828 declared in the configuration. In F<etc/config.yml>, this is located
829 at:
830  
831 framework:
832 Database:
833 Version: 0.0.1
834  
835 =back
836  
837 A model is considered to be defined by Jifty if it the package name
838 starts with "Jifty::". Otherwise, it is assumed to be an application
839 model.
840  
841 =cut
842  
843 36 ▶sub schema_version {
Subroutine schema_version: called CC=2 SCAR=8.4
Pod documented
844 36 my $class = shift;
845  
846 # Return the Jifty schema version
847 36 ▶ if ( $class =~ /^Jifty::Model::/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class =~ /^Jifty::Model::/) { } 36 0 36
848 36 return $Jifty::VERSION;
849 }
850  
851 # TODO need to consider Jifty plugin versions?
852  
853 # Return the application schema version
854 else {
855 0 my $config = Jifty->config();
856 0 return $config->framework('Database')->{'Version'};
857 }
858 }
859  
860 =head2 column_serialized_as
861  
862  
863  
864 =cut
865  
866 0 ▶sub column_serialized_as {
Subroutine column_serialized_as: not called CC=4 SCAR=30.0
Pod documented
867 0 my ($class, $column) = @_;
868 0 ▶ my $meta = $column->attributes->{serialized} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $meta = $column->attributes->{"serialized"} or return 0 0 0
869 0 ▶ $meta->{columns} ||= [$column->refers_to->default_serialized_as_columns]
Branch: 1 branch
BranchTrueFalseTotal
if $column->refers_to 0 0 0
Condition0%0/2
$meta->{"columns"} ||= [$column->refers_to->default_serialized_as_columns]
l!l
00
MC/DC0%0/1
$meta->{"columns"} ||= [$column->refers_to->default_serialized_as_columns]
$meta->{"columns"}
Truth table0%0/2
A
A= $meta->{"columns"}
Aresult
11
00
870 if $column->refers_to;
871 0 return $meta;
872 }
873  
874 =head2 default_serialized_as_columns
875  
876 =cut
877  
878 0 ▶sub default_serialized_as_columns {
Subroutine default_serialized_as_columns: not called CC=1 SCAR=6.9
Pod documented
879 0 my $class = shift;
880 0 return (List::MoreUtils::uniq 'id', $class->_brief_description);
881 }
882  
883 =head2 jifty_serialize_format
884  
885 This is used to create a hash reference of the object's values. Unlike
886 Jifty::DBI::Record->as_hash, this won't transform refers_to columns into JDBI
887 objects. Override this if you want to include calculated values (for use in,
888 say, your REST interface)
889  
890 =cut
891  
892 0 ▶sub jifty_serialize_format {
Subroutine jifty_serialize_format: not called CC=10 SCAR=47.0
Pod documented
893 0 my $record = shift;
894 0 my %data;
895  
896 # XXX: maybe just test ->virtual?
897 0 for my $column (grep { $_->readable } $record->columns ) {
898 0 ▶ next if UNIVERSAL::isa($column->refers_to,
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($column->refers_to, "Jifty::DBI::Collection") 0 0 0
899 'Jifty::DBI::Collection');
900 0 ▶ next if $column->container;
Branch: 1 branch
BranchTrueFalseTotal
if $column->container 0 0 0
901 0 ▶ my $name = $column->aliased_as || $column->name;
Condition0%0/3
$column->aliased_as || $column->name
l!l&&r!l&&!r
000
MC/DC0%0/2
$column->aliased_as || $column->name
$column->aliased_as $column->name
Truth table0%0/3
A || B
A= $column->aliased_asB= $column->name
ABresult
1X1
011
000
902  
903 0 ▶ if ((my $refers_to = $column->refers_to) &&
Branch: 1 branch
BranchTrueFalseTotal
if (my $refers_to = $column->refers_to and my $serialize_meta = $record->column_serialized_as($column)) { } 0 0 0
Condition0%0/3
my $refers_to = $column->refers_to and my $serialize_meta = $record->column_serialized_as($column)
!ll&&!rl&&r
000
MC/DC0%0/2
my $refers_to = $column->refers_to and my $serialize_meta = $record->column_serialized_as($column)
my $refers_to = $column->refers_to my $serialize_meta = $record->column_serialized_as($column)
Truth table0%0/3
A and B
A= my $refers_to = $column->refers_toB= my $serialize_meta = $record->column_serialized_as($column)
ABresult
0X0
100
111
904 (my $serialize_meta = $record->column_serialized_as($column))) {
905 0 my $column_data = $record->$name();
906 0 ▶ if ( $column_data && $column_data->id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column_data and $column_data->id) { } 0 0 0
Condition0%0/3
$column_data and $column_data->id
!ll&&!rl&&r
000
MC/DC0%0/2
$column_data and $column_data->id
$column_data $column_data->id
Truth table0%0/3
A and B
A= $column_dataB= $column_data->id
ABresult
0X0
100
111
907 0 ▶ $name = $serialize_meta->{name} if $serialize_meta->{name};
Branch: 1 branch
BranchTrueFalseTotal
if $serialize_meta->{"name"} 0 0 0
908 0 $data{$name} = { map { $_ => scalar $record->$name->$_ } @{$serialize_meta->{columns} } };
909 }
910 else {
911 0 $data{$name} = undef;
912 }
913 }
914 else {
915 0 $data{$name} = Jifty::Util->stringify($record->_value($name));
916 }
917 }
918  
919 0 return \%data;
920 }
921  
922 =head2 autogenerate_action
923  
924 Controls which of the L<Jifty::Action::Record> subclasses are
925 automatically set up for this model; this subroutine is passed one of
926 the strings C<Create>, C<Update>, C<Delete>, C<Search> or C<Execute>, and should
927 return a true value if that action should be autogenerated.
928  
929 The default method returns 0 for all action classes if the model is
930 marked as L</is_private>. It returns 0 for all actions that are not
931 C<Search> if the model is marked as L</is_protected>; otherwise, it
932 returns true.
933  
934 =cut
935  
936 0 ▶sub autogenerate_action {
Subroutine autogenerate_action: not called CC=4 SCAR=30.0
Pod documented
937 0 my $class = shift;
938 0 my($action) = @_;
939  
940 0 ▶ return 0 if $class->is_private;
Branch: 1 branch
BranchTrueFalseTotal
if $class->is_private 0 0 0
941 0 ▶ return 0 if $class->is_protected and $action ne "Search";
Branch: 1 branch
BranchTrueFalseTotal
if $class->is_protected and $action ne "Search" 0 0 0
Condition0%0/3
$class->is_protected and $action ne "Search"
!ll&&!rl&&r
000
MC/DC0%0/2
$class->is_protected and $action ne "Search"
$class->is_protected $action ne "Search"
Truth table0%0/3
A and B
A= $class->is_protectedB= $action ne "Search"
ABresult
0X0
100
111
942  
943 0 return 1;
944 }
945  
946 =head2 is_private
947  
948 Override this method to return true to not generate any actions for
949 this model, and to hide it from REST introspection.
950  
951 =cut
952  
953 0 ▶sub is_private { 0 }
Subroutine is_private: not called CC=1 SCAR=6.9
Pod documented
954  
955 =head2 is_protected
956  
957 Override this method to return true to only generate Search actions
958 for this model.
959  
960 =cut
961  
962 0 ▶sub is_protected { return shift->is_private }
Subroutine is_protected: not called CC=1 SCAR=6.9
Pod documented
963  
964 =head2 enumerable
965  
966 Controls whether autogenerated actions with columns that refer to this
967 class should attempt to provide a drop-down of possible values or not.
968 This method will be called as a class method, and defaults to true.
969  
970 =cut
971  
972 0 ▶sub enumerable { 1 }
Subroutine enumerable: not called CC=1 SCAR=6.9
Pod documented
973  
974 113 1;
« Jifty/Plugin/SinglePage.pm ↑ Summary Jifty/Plugin/ClassLoader.pm »