mirror of https://github.com/rails/rails
Handle negative numbers in NumberToHumanSizeConverter
This commit is contained in:
parent
ea4c48ad39
commit
c6dcb11691
|
@ -1,3 +1,7 @@
|
|||
* Fix the `number_to_human_size` view helper to correctly work with negative numbers.
|
||||
|
||||
*Earlopain*
|
||||
|
||||
* Automatically discard the implicit locals injected by collection rendering for template that can't accept them
|
||||
|
||||
When rendering a collection, two implicit variables are injected, which breaks templates with strict locals.
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
* Fix `#to_fs(:human_size)` to correctly work with negative numbers.
|
||||
|
||||
*Earlopain*
|
||||
|
||||
* Fix `BroadcastLogger#dup` so that it duplicates the logger's `broadcasts`.
|
||||
|
||||
*Andrew Novoselac*
|
||||
|
|
|
@ -43,13 +43,13 @@ module ActiveSupport
|
|||
|
||||
def exponent
|
||||
max = STORAGE_UNITS.size - 1
|
||||
exp = (Math.log(number) / Math.log(base)).to_i
|
||||
exp = (Math.log(number.abs) / Math.log(base)).to_i
|
||||
exp = max if exp > max # avoid overflow for the highest unit
|
||||
exp
|
||||
end
|
||||
|
||||
def smaller_than_base?
|
||||
number.to_i < base
|
||||
number.to_i.abs < base
|
||||
end
|
||||
|
||||
def base
|
||||
|
|
|
@ -310,6 +310,16 @@ class NumericExtFormattingTest < ActiveSupport::TestCase
|
|||
assert_equal "10 Bytes", 10.to_fs(:human_size)
|
||||
end
|
||||
|
||||
def test_to_fs__human_size_with_negative_number
|
||||
assert_equal "-1 Bytes", -1.to_fs(:human_size)
|
||||
assert_equal "-3 Bytes", -3.14159265.to_fs(:human_size)
|
||||
assert_equal "-123 Bytes", -123.to_fs(:human_size)
|
||||
assert_equal "-12.1 KB", -12345.to_fs(:human_size)
|
||||
assert_equal "-444 KB", kilobytes(-444).to_fs(:human_size)
|
||||
assert_equal "-1.12 TB", -1234567890123.to_fs(:human_size)
|
||||
assert_equal "-1.01 KB", kilobytes(-1.0100).to_fs(:human_size, precision: 4)
|
||||
end
|
||||
|
||||
def test_to_fs__human_size_with_options_hash
|
||||
assert_equal "1.2 MB", 1234567.to_fs(:human_size, precision: 2)
|
||||
assert_equal "3 Bytes", 3.14159265.to_fs(:human_size, precision: 4)
|
||||
|
|
|
@ -282,6 +282,18 @@ module ActiveSupport
|
|||
end
|
||||
end
|
||||
|
||||
def test_number_number_to_human_size_with_negative_number
|
||||
[@instance_with_helpers, TestClassWithClassNumberHelpers, ActiveSupport::NumberHelper].each do |number_helper|
|
||||
assert_equal "-1 Bytes", number_helper.number_to_human_size(-1)
|
||||
assert_equal "-3 Bytes", number_helper.number_to_human_size(-3.14159265)
|
||||
assert_equal "-123 Bytes", number_helper.number_to_human_size(-123)
|
||||
assert_equal "-12.1 KB", number_helper.number_to_human_size(-12345)
|
||||
assert_equal "-444 KB", number_helper.number_to_human_size(kilobytes(-444))
|
||||
assert_equal "-1.12 TB", number_helper.number_to_human_size(-1234567890123)
|
||||
assert_equal "-1.01 KB", number_helper.number_to_human_size(kilobytes(-1.0100), precision: 4)
|
||||
end
|
||||
end
|
||||
|
||||
def test_number_to_human_size_with_options_hash
|
||||
[@instance_with_helpers, TestClassWithClassNumberHelpers, ActiveSupport::NumberHelper].each do |number_helper|
|
||||
assert_equal "1.2 MB", number_helper.number_to_human_size(1234567, precision: 2)
|
||||
|
|
Loading…
Reference in New Issue