Skip to content

Commit 87609e8

Browse files
Merge pull request #49791 from Earlopain/number-human-size-negative
Handle negative numbers in `NumberToHumanSizeConverter`
2 parents 56b7357 + c6dcb11 commit 87609e8

File tree

5 files changed

+32
-2
lines changed

5 files changed

+32
-2
lines changed

actionview/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Fix the `number_to_human_size` view helper to correctly work with negative numbers.
2+
3+
*Earlopain*
4+
15
* Automatically discard the implicit locals injected by collection rendering for template that can't accept them
26

37
When rendering a collection, two implicit variables are injected, which breaks templates with strict locals.

activesupport/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Fix `#to_fs(:human_size)` to correctly work with negative numbers.
2+
3+
*Earlopain*
4+
15
* Fix `BroadcastLogger#dup` so that it duplicates the logger's `broadcasts`.
26

37
*Andrew Novoselac*

activesupport/lib/active_support/number_helper/number_to_human_size_converter.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ def storage_unit_key
4343

4444
def exponent
4545
max = STORAGE_UNITS.size - 1
46-
exp = (Math.log(number) / Math.log(base)).to_i
46+
exp = (Math.log(number.abs) / Math.log(base)).to_i
4747
exp = max if exp > max # avoid overflow for the highest unit
4848
exp
4949
end
5050

5151
def smaller_than_base?
52-
number.to_i < base
52+
number.to_i.abs < base
5353
end
5454

5555
def base

activesupport/test/core_ext/numeric_ext_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,16 @@ def test_to_fs__human_size
310310
assert_equal "10 Bytes", 10.to_fs(:human_size)
311311
end
312312

313+
def test_to_fs__human_size_with_negative_number
314+
assert_equal "-1 Bytes", -1.to_fs(:human_size)
315+
assert_equal "-3 Bytes", -3.14159265.to_fs(:human_size)
316+
assert_equal "-123 Bytes", -123.to_fs(:human_size)
317+
assert_equal "-12.1 KB", -12345.to_fs(:human_size)
318+
assert_equal "-444 KB", kilobytes(-444).to_fs(:human_size)
319+
assert_equal "-1.12 TB", -1234567890123.to_fs(:human_size)
320+
assert_equal "-1.01 KB", kilobytes(-1.0100).to_fs(:human_size, precision: 4)
321+
end
322+
313323
def test_to_fs__human_size_with_options_hash
314324
assert_equal "1.2 MB", 1234567.to_fs(:human_size, precision: 2)
315325
assert_equal "3 Bytes", 3.14159265.to_fs(:human_size, precision: 4)

activesupport/test/number_helper_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,18 @@ def test_number_number_to_human_size
282282
end
283283
end
284284

285+
def test_number_number_to_human_size_with_negative_number
286+
[@instance_with_helpers, TestClassWithClassNumberHelpers, ActiveSupport::NumberHelper].each do |number_helper|
287+
assert_equal "-1 Bytes", number_helper.number_to_human_size(-1)
288+
assert_equal "-3 Bytes", number_helper.number_to_human_size(-3.14159265)
289+
assert_equal "-123 Bytes", number_helper.number_to_human_size(-123)
290+
assert_equal "-12.1 KB", number_helper.number_to_human_size(-12345)
291+
assert_equal "-444 KB", number_helper.number_to_human_size(kilobytes(-444))
292+
assert_equal "-1.12 TB", number_helper.number_to_human_size(-1234567890123)
293+
assert_equal "-1.01 KB", number_helper.number_to_human_size(kilobytes(-1.0100), precision: 4)
294+
end
295+
end
296+
285297
def test_number_to_human_size_with_options_hash
286298
[@instance_with_helpers, TestClassWithClassNumberHelpers, ActiveSupport::NumberHelper].each do |number_helper|
287299
assert_equal "1.2 MB", number_helper.number_to_human_size(1234567, precision: 2)

0 commit comments

Comments
 (0)