SD/Model/Ticket.pm untested

filter: Statementstmt 0.0% 0 / 100 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 191 CC 12 SCAR 18.8
CC
12
Cov
0%
canonicalize_prop_due
20.0
props_to_show
12.0
is_overdue
12.0
CRAP 6.6
1 package App::SD::Model::Ticket;
2 use Any::Moose;
3 extends 'App::SD::Record';
4  
5 use Term::ANSIColor;
6 use App::SD::Util;
7  
8 use constant collection_class => 'App::SD::Collection::Ticket';
9 has '+type' => ( default => 'ticket');
10  
11 our $ACTIVE_STATUSES;
12  
13 __PACKAGE__->register_reference( comments => 'App::SD::Collection::Comment', by => 'ticket');
14 __PACKAGE__->register_reference( attachments => 'App::SD::Collection::Attachment', by => 'ticket');
15  
16 sub BUILD {
17 my $self = shift;
18 $ACTIVE_STATUSES ||= $self->app_handle->setting(label => 'active_statuses')->get();
19 }
20  
21 sub default_prop_component {
22 my $self = shift;
23 return $self->app_handle->setting(label => 'default_component')->get()->[0];
24 }
25  
26 sub default_prop_milestone {
27 my $self = shift;
28 return $self->app_handle->setting(label => 'default_milestone')->get()->[0];
29 }
30  
31 =head2 default_prop_status
32  
33 Returns a string of the default value of the C<status> prop.
34  
35 =cut
36  
37 sub default_prop_status {
38 my $self = shift;
39 return $self->app_handle->setting(label => 'default_status')->get()->[0];
40 }
41  
42 sub has_active_status {
43 my $self = shift;
44 return 1 if grep { $_ eq $self->prop('status') } @{$ACTIVE_STATUSES};
45 }
46  
47 =head2 default_prop_reporter
48  
49 Returns a string of the default value of the C<reporter> prop.
50 (Currently, this is the config variable C<email_address> or
51 the environmental variable C<EMAIL>.)
52  
53 =cut
54  
55 sub default_prop_reporter {
56 my $self = shift;
57 my $reporter = $self->app_handle->current_user_email;
58 if ( $reporter ) {
59 return $reporter;
60 }
61 else {
62 die "Cannot determine an email to attribute your changes to."
63 ." You can\nfix this by setting the config variable"
64 ." 'user.email-address'.\n";
65 }
66 }
67  
68 =head2 canonicalize_prop_status
69  
70 resolved is called closed.
71  
72 =cut
73  
74 my %canonicalize_status = (
75 resolved => 'closed',
76 );
77  
78 sub canonicalize_prop_status {
79 my $self = shift;
80 my %args = @_;
81  
82 my $props = $args{props};
83  
84 if (defined $canonicalize_status{ $props->{status} }) {
85 $props->{status} = $canonicalize_status{ $props->{status} };
86 }
87  
88 return 1;
89 }
90  
91  
92 sub canonicalize_prop_due {
93 my $self = shift;
94 my %args = @_;
95 my $props = $args{props};
96 # skip blank
97 return 1 unless $props->{due};
98 #skip well formed
99 return 1 if $props->{due} =~ /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/;
100 require DateTime::Format::Natural;
101 my $parser = DateTime::Format::Natural->new;
102 my $dt = $parser->parse_datetime($props->{due});
103 if ($parser->success) {
104 # operate on $dt/@dt, for example:
105 $props->{due} = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $dt->year, $dt->month, $dt->day, $dt->hour, $dt->min, $dt->sec);
106 }
107 return 1;
108 }
109  
110  
111 =head2 _default_summary_format
112  
113 The default ticket summary format (used for displaying tickets in a
114 list, generally).
115  
116 =cut
117  
118 sub _default_summary_format { '%s,$luid | %s,summary | %s,status' }
119  
120 =head2 validate_prop_status { props = $hashref, errors = $hashref }
121  
122 Determines whether the status prop value given in C<$args{props}{status}>
123 is valid.
124  
125 Returns true if the status is valid. If the status is invalid, sets
126 C<$args{errors}{status}> to an error message and returns false.
127  
128 =cut
129  
130 sub validate_prop_status {
131 my ( $self, %args ) = @_;
132 return $self->validate_prop_from_recommended_values( 'status', \%args );
133 }
134  
135 sub validate_prop_component {
136 my ( $self, %args ) = @_;
137 return $self->validate_prop_from_recommended_values( 'component', \%args );
138 }
139  
140 sub validate_prop_milestone {
141 my ( $self, %args ) = @_;
142 return $self->validate_prop_from_recommended_values( 'milestone', \%args );
143 }
144  
145 sub _recommended_values_for_prop_milestone {
146 return @{ shift->app_handle->setting( label => 'milestones' )->get() };
147 }
148  
149 sub _recommended_values_for_prop_status {
150 return @{ shift->app_handle->setting( label => 'statuses' )->get() };
151 }
152  
153 sub _recommended_values_for_prop_component {
154 return @{ shift->app_handle->setting( label => 'components' )->get() };
155 }
156  
157 =head2 props_to_show { 'verbose' => 1, update => 0 }
158  
159 A list of which properties to display for the C<show> command (in order
160 from first to last).
161  
162 If called with 'verbose' as a true value, will return all the
163 declared props of a ticket rather than the predefined list of ones
164 to show. (Should not be called this way during a ticket create as
165 new tickets have no declared properties.)
166  
167 If called with 'update' as a true value, props in the prop ordering
168 setting will still be returned in the list even if the record
169 doesn't have that property. (Because we often want to not show
170 blank properties, but still have the option of adding them in
171 an update.)
172  
173 =cut
174  
175 sub props_to_show {
176 my $self = shift;
177 my $args = shift || {};
178 my @props_from_config = split(',',
179 ($self->app_handle->config->get( key => 'ticket.common-props') || ''));
180 my $props_list = @props_from_config ? \@props_from_config :
181 $self->app_handle->setting(label => 'common_ticket_props')->get();
182  
183 return @{$props_list} unless $args->{'verbose'};
184  
185 return _create_prop_ordering( hash_to_order => $self->get_props,
186 order => $props_list,
187 update => $args->{update});
188 }
189  
190 =head2 _create_prop_ordering hash_to_order => $hashref, order => $arrayref [, update => 1 ]
191  
192 Given references to a hash and an array, return an array of the keys of the
193 hash in the order specified by the array, with any extra keys at the end of the
194 ordering.
195  
196 If called with update as a true value, will add keys in the ordering to the
197 returned order even if they're not in the hash.
198  
199 =cut
200  
201 sub _create_prop_ordering {
202 my %args = @_;
203 my %props = %{$args{hash_to_order}};
204 my @order = @{$args{order}};
205 my @new_props_list;
206  
207 # if props in the ordering are in the hash, add them to
208 # the new ordering
209 for my $prop (@order) {
210 if ( $props{$prop} || $prop eq 'id' || $args{update} ) {
211 push @new_props_list, $prop;
212 delete $props{$prop};
213 }
214 }
215 # add hash keys not in the ordering to the end of the new ordering
216 push @new_props_list, keys %props;
217  
218 return @new_props_list;
219 }
220  
221 =head2 immutable_props
222  
223 A pattern of props not to show in an editor (when creating or updating a
224 ticket, for example). Could also be used to determine which props shouldn't be
225 user-modifiable.
226  
227 =cut
228  
229 sub immutable_props { qw(id creator created original_replica) }
230  
231 =head2 is_overdue [$date]
232  
233 Takes an ISO date (or uses the C<date> prop value if no date is given).
234  
235 Returns false if the date is not a valid ISO date or its due date is
236 in the future. Returns true if the due date has passed.
237  
238 =cut
239  
240 sub is_overdue {
241 my $self = shift;
242 my $date = shift || $self->prop('due');
243  
244 my $then = App::SD::Util::string_to_datetime($date);
245  
246 if (!$then) {
247 warn "Unknown date format '$date'";
248 return 0;
249 }
250  
251 if ($then < DateTime->now()){
252 return 1;
253 }
254  
255 return 0;
256 }
257  
258  
259 __PACKAGE__->meta->make_immutable;
260 no Any::Moose;
261 1;