aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpezholio <pezholio@gmail.com>2017-01-17 16:44:50 +0000
committerDave Arter <davea@mysociety.org>2017-02-15 13:28:04 +0000
commitb14319e75b61ce1ee21ebb7d6fa924ebe18ceee9 (patch)
treea127709b1254de587fadce5f3b7aa82decffc09a
parent0b1ab9fb1145ca32dced46b4faefe49da5c76768 (diff)
Add Problem->time_ago for pretty-printed duration
-rw-r--r--perllib/FixMyStreet/DB/Result/Problem.pm14
-rw-r--r--t/app/model/problem.t44
2 files changed, 58 insertions, 0 deletions
diff --git a/perllib/FixMyStreet/DB/Result/Problem.pm b/perllib/FixMyStreet/DB/Result/Problem.pm
index 0092dd8b5..d0a5a3a4f 100644
--- a/perllib/FixMyStreet/DB/Result/Problem.pm
+++ b/perllib/FixMyStreet/DB/Result/Problem.pm
@@ -659,6 +659,20 @@ sub body {
return $body;
}
+
+=head2 time_ago
+ Returns how long ago a problem was reported in an appropriately
+ prettified duration, depending on the duration.
+=cut
+
+sub time_ago {
+ my ( $self, $date ) = @_;
+ $date ||= 'confirmed';
+ my $duration = time() - $self->$date->epoch;
+
+ return Utils::prettify_duration( $duration );
+}
+
=head2 response_templates
Returns all ResponseTemplates attached to this problem's bodies, in alphabetical
diff --git a/t/app/model/problem.t b/t/app/model/problem.t
index 1130078c0..e2c407ffb 100644
--- a/t/app/model/problem.t
+++ b/t/app/model/problem.t
@@ -774,6 +774,50 @@ subtest 'check duplicate reports' => sub {
is $problem2->duplicates->[0]->title, $problem1->title, 'problem2 includes problem1 in duplicates';
};
+subtest 'get report time ago in appropriate format' => sub {
+ my ($problem) = $mech->create_problems_for_body(1, $body_ids{2651}, 'TITLE');
+
+ $problem->update( {
+ confirmed => DateTime->now->subtract( minutes => 2)
+ } );
+ is $problem->time_ago, '2 minutes', 'problem returns time ago in minutes';
+
+ $problem->update( {
+ confirmed => DateTime->now->subtract( hours => 18)
+ } );
+ is $problem->time_ago, '18 hours', 'problem returns time ago in hours';
+
+ $problem->update( {
+ confirmed => DateTime->now->subtract( days => 4)
+ } );
+ is $problem->time_ago, '4 days', 'problem returns time ago in days';
+
+ $problem->update( {
+ confirmed => DateTime->now->subtract( weeks => 3 )
+ } );
+ is $problem->time_ago, '3 weeks', 'problem returns time ago in weeks';
+
+ $problem->update( {
+ confirmed => DateTime->now->subtract( months => 4 )
+ } );
+ is $problem->time_ago, '4 months', 'problem returns time ago in months';
+
+ $problem->update( {
+ confirmed => DateTime->now->subtract( years => 2 )
+ } );
+ is $problem->time_ago, '2 years', 'problem returns time ago in years';
+
+};
+
+subtest 'time ago works with other dates' => sub {
+ my ($problem) = $mech->create_problems_for_body(1, $body_ids{2651}, 'TITLE');
+
+ $problem->update( {
+ lastupdate => DateTime->now->subtract( days => 4)
+ } );
+ is $problem->time_ago('lastupdate'), '4 days', 'problem returns last updated time ago in days';
+};
+
END {
$problem->comments->delete if $problem;
$problem->delete if $problem;