Skip to content
This repository
Browse code

Moved TextHelper#human_size to NumberHelper#number_to_human_size, but…

… kept an deprecated alias to the old method name

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1147 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 7140f65355a7863b81028fbb2cb0b0dbcf93e8c0 1 parent 3b9e90a
David Heinemeier Hansson dhh authored
2  actionpack/CHANGELOG
... ... @@ -1,5 +1,7 @@
1 1 *SVN*
2 2
  3 +* Moved TextHelper#human_size to NumberHelper#number_to_human_size, but kept an deprecated alias to the old method name
  4 +
3 5 * Fixed that the content-type for some browsers could include an additional \r which made wonky things happen #1067 [Thomas Fuchs]
4 6
5 7 * Fixed that radio buttons shouldn't have a default size attribute #1074 [hendrik@mans.de]
22 actionpack/lib/action_view/helpers/number_helper.rb
@@ -77,6 +77,28 @@ def number_with_delimiter(number, delimiter=",")
77 77 number.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
78 78 end
79 79
  80 + # Returns a formatted-for-humans file size.
  81 + #
  82 + # Examples:
  83 + # human_size(123) => 123 Bytes
  84 + # human_size(1234) => 1.2 KB
  85 + # human_size(12345) => 12.1 KB
  86 + # human_size(1234567) => 1.2 MB
  87 + # human_size(1234567890) => 1.1 GB
  88 + def number_to_human_size(size)
  89 + begin
  90 + return "%d Bytes" % size if size < 1.kilobytes
  91 + return "%.1f KB" % (size/1.0.kilobytes) if size < 1.megabytes
  92 + return "%.1f MB" % (size/1.0.megabytes) if size < 1.gigabytes
  93 + return "%.1f GB" % (size/1.0.gigabytes) if size < 1.terabytes
  94 + return "%.1f TB" % (size/1.0.terabytes)
  95 + rescue
  96 + # just return nothing
  97 + end
  98 + end
  99 +
  100 + alias_method :human_size, :number_to_human_size # deprecated alias
  101 +
80 102 # Formats a +number+ with a level of +precision+.
81 103 # Example:
82 104 # number_with_precision(111.2345) => 111.235
20 actionpack/lib/action_view/helpers/text_helper.rb
@@ -129,26 +129,6 @@ def strip_links(text)
129 129 text.gsub(/<a.*>(.*)<\/a>/m, '\1')
130 130 end
131 131
132   - # Returns a formatted-for-humans file size.
133   - #
134   - # Examples:
135   - # human_size(123) => 123 Bytes
136   - # human_size(1234) => 1.2 KB
137   - # human_size(12345) => 12.1 KB
138   - # human_size(1234567) => 1.2 MB
139   - # human_size(1234567890) => 1.1 GB
140   - def human_size(size)
141   - begin
142   - return "%d Bytes" % size if size < 1.kilobytes
143   - return "%.1f KB" % (size/1.0.kilobytes) if size < 1.megabytes
144   - return "%.1f MB" % (size/1.0.megabytes) if size < 1.gigabytes
145   - return "%.1f GB" % (size/1.0.gigabytes) if size < 1.terabytes
146   - return "%.1f TB" % (size/1.0.terabytes)
147   - rescue
148   - # just return nothing
149   - end
150   - end
151   -
152 132 private
153 133 # Returns a version of the text that's safe to use in a regular expression without triggering engine features.
154 134 def escape_regexp(text)
23 actionpack/test/template/number_helper_test.rb
... ... @@ -0,0 +1,23 @@
  1 +require 'test/unit'
  2 +require File.dirname(__FILE__) + '/../../lib/action_view/helpers/number_helper'
  3 +require File.dirname(__FILE__) + '/../../../activesupport/lib/active_support/core_ext/numeric' # for human_size
  4 +
  5 +class NumberHelperTest < Test::Unit::TestCase
  6 + include ActionView::Helpers::NumberHelper
  7 +
  8 + def test_number_to_human_size
  9 + assert_equal("0 Bytes", number_to_human_size(0))
  10 + assert_equal("3 Bytes", number_to_human_size(3.14159265))
  11 + assert_equal("123 Bytes", number_to_human_size(123.0))
  12 + assert_equal("123 Bytes", number_to_human_size(123))
  13 + assert_equal("1.2 KB", number_to_human_size(1234))
  14 + assert_equal("12.1 KB", number_to_human_size(12345))
  15 + assert_equal("1.2 MB", number_to_human_size(1234567))
  16 + assert_equal("1.1 GB", number_to_human_size(1234567890))
  17 + assert_equal("1.1 TB", number_to_human_size(1234567890123))
  18 + assert_equal("444.0 KB", number_to_human_size(444.kilobytes))
  19 + assert_equal("1023.0 MB", number_to_human_size(1023.megabytes))
  20 + assert_equal("3.0 TB", number_to_human_size(3.terabytes))
  21 + assert_nil number_to_human_size('x')
  22 + end
  23 +end
16 actionpack/test/template/text_helper_test.rb
@@ -53,22 +53,6 @@ def test_highlighter_with_regexp
53 53 highlight("This is a beautiful? morning", "beautiful? morning")
54 54 )
55 55 end
56   -
57   - def test_human_size
58   - assert_equal("0 Bytes", human_size(0))
59   - assert_equal("3 Bytes", human_size(3.14159265))
60   - assert_equal("123 Bytes", human_size(123.0))
61   - assert_equal("123 Bytes", human_size(123))
62   - assert_equal("1.2 KB", human_size(1234))
63   - assert_equal("12.1 KB", human_size(12345))
64   - assert_equal("1.2 MB", human_size(1234567))
65   - assert_equal("1.1 GB", human_size(1234567890))
66   - assert_equal("1.1 TB", human_size(1234567890123))
67   - assert_equal("444.0 KB", human_size(444.kilobytes))
68   - assert_equal("1023.0 MB", human_size(1023.megabytes))
69   - assert_equal("3.0 TB", human_size(3.terabytes))
70   - assert_nil human_size('x')
71   - end
72 56
73 57 def test_excerpt
74 58 assert_equal("...is a beautiful morni...", excerpt("This is a beautiful morning", "beautiful", 5))

0 comments on commit 7140f65

Please sign in to comment.
Something went wrong with that request. Please try again.