]> git.openstreetmap.org Git - rails.git/commitdiff
Fix case when user block durations are slightly negative
authorAndy Allan <git@gravitystorm.co.uk>
Wed, 9 Jun 2021 14:14:14 +0000 (15:14 +0100)
committerAndy Allan <git@gravitystorm.co.uk>
Wed, 9 Jun 2021 14:16:35 +0000 (15:16 +0100)
The output from ActiveSupport::Duration is wildly unhelpful in those cases

Fixes #3210

app/helpers/user_blocks_helper.rb
test/helpers/user_blocks_helper_test.rb

index 0528140f4cd8bbad5a4627f5eb2fa1c886e014e5..95b6cb600c739786701c0cb95c6fe8470c4428a3 100644 (file)
@@ -26,6 +26,8 @@ module UserBlocksHelper
   end
 
   def block_duration_in_words(duration)
+    # Ensure the requested duration isn't negative, even by a millisecond
+    duration = 0 if duration.negative?
     parts = ActiveSupport::Duration.build(duration).parts
     if duration < 1.day
       t("user_blocks.helper.block_duration.hours", :count => parts.fetch(:hours, 0))
index 7d0b664fe2f0d7cebc8e4b409ad374af553c8a17..c4afa6c83b6e7e991d8ad8b869903af75d7932f9 100644 (file)
@@ -27,5 +27,10 @@ class UserBlocksHelperTest < ActionView::TestCase
 
     words = block_duration_in_words(0)
     assert_equal "0 hours", words
+
+    # Ensure that (slightly) negative durations don't mess everything up
+    # This can happen on zero hour blocks when ends_at is a millisecond before created_at
+    words = block_duration_in_words(-0.001)
+    assert_equal "0 hours", words
   end
 end