Jifty/Action/Record/Update.pm

filter: Statementstmt 21.6% 16 / 74 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 62 MC/DCmcdc 0.0% 0 / 31 Subroutinesub 38.4% 5 / 13 Podpod 100% 4 / 4 totaltotal 11.5% 25 / 216 CC 42 SCAR 66.3
CC
42
Cov
10%
take_action
1190.0
arguments
40.4
_validate_arguments
12.0
CRAP 755.4
« Jifty/Web/Form/Element.pm ↑ Summary Jifty/Action/Record/Search.pm »
1 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Jifty::Action::Record::Update;
5  
6 =head1 NAME
7  
8 Jifty::Action::Record::Update - Automagic update action
9  
10 =head1 DESCRIPTION
11  
12 This class is used as the base class for L<Jifty::Action>s that are
13 merely updating L<Jifty::Record> objects. To use it, subclass it and
14 override the C<record_class> method to return the name of the
15 L<Jifty::Record> subclass that this action should update.
16  
17 =cut
18  
19 1 ▶use base qw/Jifty::Action::Record/;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶use Scalar::Util qw/ blessed /;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 =head1 METHODS
24  
25 =head2 arguments
26  
27 Overrides the L<Jifty::Action::Record/arguments> method to further
28 specify that all of the primary keys B<must> have values when
29 submitted; that is, they are
30 L<constructors|Jifty::Manual::Glossary/constructors>.
31  
32 =cut
33  
34 0 ▶sub arguments {
Subroutine arguments: not called CC=7 SCAR=37.0
Pod documented
35 0 my $self = shift;
36 0 my $arguments = $self->SUPER::arguments(@_);
37  
38 # Mark read-only columns for read-only display
39 0 for my $column ( $self->possible_columns ) {
40 0 ▶ if ( (not $column->writable) and $column->readable ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $column->writable and $column->readable) 0 0 0
Condition0%0/3
not $column->writable and $column->readable
!ll&&!rl&&r
000
MC/DC0%0/2
not $column->writable and $column->readable
not $column->writable $column->readable
Truth table0%0/3
not(A) and B
A= not $column->writableB= $column->readable
ABresult
0X0
100
111
41 0 $arguments->{$column->name}{'render_mode'} = 'read';
42 }
43 }
44  
45 # Add the primary keys to constructors and make them mandatory
46 0 for my $pk (@{ $self->record->_primary_keys }) {
47 0 $arguments->{$pk}{'constructor'} = 1;
48 0 $arguments->{$pk}{'mandatory'} = 1;
49 0 $arguments->{$pk}{'render_mode'} = 'read';
50 }
51  
52 0 ▶ if ( $self->can('PARAMS') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can('PARAMS')) { } 0 0 0
53 1 ▶ use Jifty::Param::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 0 ▶ return Jifty::Param::Schema::merge_params(
Condition0%0/2
$self->PARAMS || {}
l!l
00
MC/DC0%0/1
$self->PARAMS || {}
$self->PARAMS
Truth table0%0/2
A
A= $self->PARAMS
Aresult
11
00
55 $arguments, ($self->PARAMS || {})
56 );
57 }
58 else {
59 0 return $arguments;
60 }
61 }
62  
63 =head2 validate_arguments
64  
65 We only need to validate arguments that got B<submitted> -- thus, a
66 mandatory argument that isn't submitted isn't invalid, as it's not
67 going to change the record. This is opposed to the behavior inherited
68 from L<Jifty::Action>, where mandatory arguments B<must> be present
69 for the action to run.
70  
71 However, constructor arguments are still required.
72  
73 =cut
74  
75 0 ▶sub _validate_arguments {
Subroutine _validate_arguments: not called CC=3 SCAR=24.8
76 0 my $self = shift;
77  
78 # Only validate the arguments given
79 0 $self->_validate_argument($_) for grep {
80 $self->has_argument($_)
81 or $self->arguments->{$_}->{constructor}
82 0 ▶ } $self->argument_names;
Condition0%0/3
$self->has_argument($_) || $self->arguments->{$_}{'constructor'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->has_argument($_) || $self->arguments->{$_}{'constructor'}
$self->has_argument($_) $self->arguments->{$_}{'constructor'}
Truth table0%0/3
A || B
A= $self->has_argument($_)B= $self->arguments->{$_}{'constructor'}
ABresult
1X1
011
000
83  
84 0 return $self->result->success;
85 }
86  
87 =head2 take_action
88  
89 Overrides the virtual C<take_action> method on L<Jifty::Action> to
90 call the appropriate C<Jifty::Record>'s C<set_> methods when the
91 action is run, thus updating the object in the database.
92  
93 =cut
94  
95 0 ▶sub take_action {
Subroutine take_action: not called CC=34 SCAR=70.8
Pod documented
96 0 my $self = shift;
97 0 my $changed = 0;
98  
99 0 my $detailed_messages = {};
100  
101 0 $self->record->call_trigger('start_update_action');
102  
103 # Iterate through all the possible arguments
104 0 for my $field ( $self->argument_names ) {
105  
106 # Skip values that weren't submitted
107 0 ▶ next unless $self->has_argument($field);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_argument($field) 0 0 0
108  
109 # Load the column object for the field
110 0 my $column = $self->record->column($field);
111  
112 # Skip nonexistent fields
113 0 ▶ next unless $column;
Branch: 1 branch
BranchTrueFalseTotal
unless $column 0 0 0
114  
115 # Grab the value
116 0 my $value = $self->argument_value($field);
117  
118 # Boolean and integer fields should be set to NULL if blank.
119 # (This logic should be moved into SB or something.)
120 0 ▶ $value = undef
Branch: 1 branch
BranchTrueFalseTotal
if defined $column->type and $column->type =~ /^bool/i || $column->type =~ /^int/i and defined $value and $value eq "" 0 0 0
Condition0%0/12
defined $column->type and $column->type =~ /^bool/i || $column->type =~ /^int/i and defined $value and $value eq ""
!ll&&!rl&&r
000
defined $column->type and $column->type =~ /^bool/i || $column->type =~ /^int/i and defined $value
!ll&&!rl&&r
000
defined $column->type and $column->type =~ /^bool/i || $column->type =~ /^int/i
!ll&&!rl&&r
000
$column->type =~ /^bool/i or $column->type =~ /^int/i
l!l&&r!l&&!r
000
MC/DC0%0/5
defined $column->type and $column->type =~ /^bool/i || $column->type =~ /^int/i and defined $value and $value eq ""
defined $column->type $column->type =~ /^bool/i $column->type =~ /^int/i defined $value $value eq ""
Truth table0%0/8
A and B or C and D and E
A= defined $column->typeB= $column->type =~ /^bool/iC= $column->type =~ /^int/iD= defined $valueE= $value eq ""
ABCDEresult
0XXXX0
100XX0
11X0X0
1010X0
11X100
101100
11X111
101111
121 if ( defined $column->type and ( $column->type =~ /^bool/i || $column->type =~ /^int/i )
122 and defined $value and $value eq '' );
123  
124 # Skip file uploads if blank
125 next if defined $self->arguments->{$field}{render_as}
126 0 ▶ and lc $self->arguments->{$field}{render_as} eq "upload"
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->arguments->{$field}{"render_as"} and lc $self->arguments->{$field}{"render_as"} eq "upload" and not defined $value && ref $value 0 0 0
Condition0%0/9
defined $self->arguments->{$field}{"render_as"} and lc $self->arguments->{$field}{"render_as"} eq "upload" and not defined $value && ref $value
!ll&&!rl&&r
000
defined $self->arguments->{$field}{"render_as"} and lc $self->arguments->{$field}{"render_as"} eq "upload"
!ll&&!rl&&r
000
defined $value && ref $value
!ll&&!rl&&r
000
MC/DC0%0/4
defined $self->arguments->{$field}{"render_as"} and lc $self->arguments->{$field}{"render_as"} eq "upload" and not defined $value && ref $value
defined $self->arguments->{$field}{"render_as"} lc $self->arguments->{$field}{"render_as"} eq "upload" defined $value ref $value
Truth table0%0/5
A and B and not(C && D)
A= defined $self->arguments->{$field}{"render_as"}B= lc $self->arguments->{$field}{"render_as"} eq "upload"C= defined $valueD= ref $value
ABCDresult
0XXX0
10XX0
11110
110X1
11101
127 and (not defined $value or not ref $value);
128  
129 # Skip fields that have not changed, but only if we can read the field.
130 # This prevents us from getting an $old value that is wrongly undef
131 # when really we are just denied read access. At the same time, it means
132 # we can keep the change checks before checking if we can update.
133
134 0 ▶ if ( $self->record->current_user_can('read', column => $field) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->record->current_user_can("read", "column", $field)) 0 0 0
135 0 my $old = $self->record->$field;
136  
137 # Handle columns which reference other tables
138 0 my $col = $self->record->column( $field );
139 0 ▶ my $by = defined $col->by ? $col->by : 'id';
Branch: 1 branch
BranchTrueFalseTotal
defined $col->by ? : 0 0 0
140 0 ▶ $old = $old->$by if blessed($old) and $old->isa( 'Jifty::Record' );
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($old) and $old->isa("Jifty::Record") 0 0 0
Condition0%0/3
builtin::blessed($old) and $old->isa("Jifty::Record")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($old) and $old->isa("Jifty::Record")
builtin::blessed($old) $old->isa("Jifty::Record")
Truth table0%0/3
A and B
A= builtin::blessed($old)B= $old->isa("Jifty::Record")
ABresult
0X0
100
111
141  
142 # ID is sometimes passed in, we want to ignore it if it doesn't change
143 0 ▶ next if $field eq 'id'
Branch: 1 branch
BranchTrueFalseTotal
if $field eq "id" and defined $old and defined $value and "$old" eq "$value" 0 0 0
Condition0%0/9
$field eq "id" and defined $old and defined $value and "$old" eq "$value"
!ll&&!rl&&r
000
$field eq "id" and defined $old and defined $value
!ll&&!rl&&r
000
$field eq "id" and defined $old
!ll&&!rl&&r
000
MC/DC0%0/4
$field eq "id" and defined $old and defined $value and "$old" eq "$value"
$field eq "id" defined $old defined $value "$old" eq "$value"
Truth table0%0/5
A and B and C and D
A= $field eq "id"B= defined $oldC= defined $valueD= "$old" eq "$value"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
144 and defined $old
145 and defined $value
146 and "$old" eq "$value";
147  
148 # if both the new and old values are defined and equal, we don't want to change em
149 # XXX TODO "$old" is a cheap hack to scalarize datetime objects
150 0 ▶ next if ( defined $old and defined $value and "$old" eq "$value" );
Branch: 1 branch
BranchTrueFalseTotal
if defined $old and defined $value and "$old" eq "$value" 0 0 0
Condition0%0/6
defined $old and defined $value and "$old" eq "$value"
!ll&&!rl&&r
000
defined $old and defined $value
!ll&&!rl&&r
000
MC/DC0%0/3
defined $old and defined $value and "$old" eq "$value"
defined $old defined $value "$old" eq "$value"
Truth table0%0/4
A and B and C
A= defined $oldB= defined $valueC= "$old" eq "$value"
ABCresult
0XX0
10X0
1100
1111
151  
152 # If _both_ the values are ''
153 0 ▶ next if ( (not defined $old or not length $old)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $old and length $old or defined $value and length $value 0 0 0
Condition0%0/9
defined $old and length $old or defined $value and length $value
l!l&&r!l&&!r
000
defined $old and length $old
!ll&&!rl&&r
000
defined $value and length $value
!ll&&!rl&&r
000
MC/DC0%0/4
defined $old and length $old or defined $value and length $value
defined $old length $old defined $value length $value
Truth table0%0/7
A and B or C and D
A= defined $oldB= length $oldC= defined $valueD= length $value
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
154 and ( not defined $value or not length $value ));
155 }
156  
157 # Error on columns we can't update
158 # <Sartak> ah ha. I think I know why passing due => undef reports
159 # action success
160 # <Sartak> Jifty::Action::Record::Update compares the value of the
161 # field with what you passed in
162 # <Sartak> but since user can't read the field, it returns undef
163 # <Sartak> and so: they're both undef, no change, skip this column
164 # <Sartak> and since that's the only column that changed, it'll notice
165 # that every column it did try to update (which is.. none of
166 # them) succeeded
167 # <Sartak> I don't think we can just skip ACLs for reading the column
168 # -- that's a potential security issue. an attacker could try
169 # every value until the action succeeds because nothing changed
170 # <Sartak> it doesn't matter for HM but for other apps it may
171  
172 0 ▶ unless ($self->record->current_user_can('update', column => $field, value => $value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->record->current_user_can("update", "column", $field, "value", $value)) 0 0 0
173 0 $self->result->field_error($field, _('Permission denied'));
174 0 next;
175 }
176  
177 # Calculate the name of the setter and set; asplode on failure
178 0 my $setter = "set_$field";
179 0 my ( $val, $msg ) = $self->record->$setter( $value );
180 0 ▶ if ($val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val) { } 0 0 0
181 # Remember that we changed something (if we did)
182 0 $changed = 1;
183 0 $detailed_messages->{$field} = $msg;
184 }
185 else {
186 0 ▶ $self->result->field_error($field, $msg || _('Permission denied'));
Condition0%0/3
$msg || _('Permission denied')
l!l&&r!l&&!r
000
MC/DC0%0/2
$msg || _('Permission denied')
$msg _('Permission denied')
Truth table0%0/3
A || B
A= $msgB= _('Permission denied')
ABresult
1X1
011
000
187 }
188 }
189  
190 # Report success if there's a change and no error, otherwise say nah-thing
191 $self->report_success
192 0 ▶ if $changed and not $self->result->failure;
Branch: 1 branch
BranchTrueFalseTotal
if $changed and not $self->result->failure 0 0 0
Condition0%0/3
$changed and not $self->result->failure
!ll&&!rl&&r
000
MC/DC0%0/2
$changed and not $self->result->failure
$changed not $self->result->failure
Truth table0%0/3
A and not(B)
A= $changedB= not $self->result->failure
ABresult
0X0
100
111
193  
194 0 $self->result->content(id => $self->record->id);
195  
196 0 ▶ $self->result->content( detailed_messages => $detailed_messages )
Branch: 1 branch
BranchTrueFalseTotal
if $self->report_detailed_messages 0 0 0
197 if $self->report_detailed_messages;
198  
199 0 $self->record->call_trigger('end_update_action');
200  
201 0 return 1;
202 }
203  
204 =head2 report_success
205  
206 Sets the L<Jifty::Result/message> to default success message,
207 "Updated". Override this if you want to report some other more
208 user-friendly result.
209  
210 =cut
211  
212 0 ▶sub report_success {
Subroutine report_success: not called CC=1 SCAR=6.9
Pod documented
213 0 my $self = shift;
214 0 $self->result->message(_("Updated"))
215 }
216  
217  
218 =head2 possible_columns
219  
220 Update actions do not provide fields for columns marked as C<private>
221 or C<protected>.
222  
223 =cut
224  
225 0 ▶sub possible_columns {
Subroutine possible_columns: not called CC=1 SCAR=6.9
Pod documented
226 0 my $self = shift;
227 0 return grep { not $_->protected } $self->SUPER::possible_columns;
228 }
229  
230 =head2 _extra_validator_args
231  
232 Passes C<< for => 'update' >> to validators.
233  
234 =cut
235  
236 0 ▶sub _extra_validator_args {
Subroutine _extra_validator_args: not called CC=1 SCAR=6.9
237 0 return { for => 'update' };
238 }
239  
240 =head2 _extra_canonicalizer_args
241  
242 Passes C<< for => 'update' >> to canonicalizers.
243  
244 =cut
245  
246 0 ▶sub _extra_canonicalizer_args {
Subroutine _extra_canonicalizer_args: not called CC=1 SCAR=6.9
247 0 return { for => 'update' };
248 }
249  
250 =head2 _extra_autocompleter_args
251  
252 Passes C<< for => 'update' >> to autocompleters.
253  
254 =cut
255  
256 0 ▶sub _extra_autocompleter_args {
Subroutine _extra_autocompleter_args: not called CC=1 SCAR=6.9
257 0 return { for => 'update' };
258 }
259  
260 =head1 SEE ALSO
261  
262 L<Jifty::Action::Record>, L<Jifty::Record>
263  
264 =head1 LICENSE
265  
266 Jifty is Copyright 2005-2010 Best Practical Solutions, LLC.
267 Jifty is distributed under the same terms as Perl itself.
268  
269 =cut
270  
271 1 1;
« Jifty/Web/Form/Element.pm ↑ Summary Jifty/Action/Record/Search.pm »