actionview 7.1.2 → 7.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2ec34555888d78ec7dd238a5845d5a0a6861aabf4b43b532b9001de74d7f69f8
4
- data.tar.gz: fa36f1e81bea968392435051307a0ad371e856f8fa4760b6e341df5412a017e5
3
+ metadata.gz: a9dd79e8bea29225f74f53dcebaa86e1b50c8ea861219c4f2744c0d4439389a7
4
+ data.tar.gz: 2f4fad4c58ca55d82627bd5a7fa5d253530652dbc95576e6140c3ab9f4aba607
5
5
  SHA512:
6
- metadata.gz: 229637c660fba19dcba6f8e15e83cf748b1e8ee0c123bc32d2b0417265df296254dfd3f9ef07d64b930f96c1e1b94a3ad9f9304a9c9e4ce734357208924019f7
7
- data.tar.gz: 5ef080a91aa2611698efca00142c79aa3f005ab680cbe804c41636df0318f05ce432429a3420d06515be43f2d249e7d38d8b9e7496d64b58a9ba218451310808
6
+ metadata.gz: 75e144581b10fae5dfc215276c6336c2b3a4f642077635e4fe8eace91fb1b508843d8b362d7425514d50f3dd8cb9db8053f80462149c82860583cdf01a57977c
7
+ data.tar.gz: a25474b380ed70c5c09ba0664212d51c056a5ca8628754adb020220f57238fce0955a22f8ea2cf46b8765d705f1710c4992d90b41cfc83123705191dc2fb8019
data/CHANGELOG.md CHANGED
@@ -1,3 +1,76 @@
1
+ ## Rails 7.1.5 (October 30, 2024) ##
2
+
3
+ * No changes.
4
+
5
+
6
+ ## Rails 7.1.4.2 (October 23, 2024) ##
7
+
8
+ * No changes.
9
+
10
+
11
+ ## Rails 7.1.4.1 (October 15, 2024) ##
12
+
13
+ * No changes.
14
+
15
+
16
+ ## Rails 7.1.4 (August 22, 2024) ##
17
+
18
+ * Action View Test Case `rendered` memoization.
19
+
20
+ *Sean Doyle*
21
+
22
+ * Restore the ability for templates to return any kind of object and not just strings
23
+
24
+ *Jean Boussier*
25
+
26
+ * Fix threading issue with strict locals.
27
+
28
+ *Robert Fletcher*
29
+
30
+
31
+ ## Rails 7.1.3.4 (June 04, 2024) ##
32
+
33
+ * No changes.
34
+
35
+
36
+ ## Rails 7.1.3.3 (May 16, 2024) ##
37
+
38
+ * No changes.
39
+
40
+
41
+ ## Rails 7.1.3.2 (February 21, 2024) ##
42
+
43
+ * No changes.
44
+
45
+
46
+ ## Rails 7.1.3.1 (February 21, 2024) ##
47
+
48
+ * No changes.
49
+
50
+
51
+ ## Rails 7.1.3 (January 16, 2024) ##
52
+
53
+ * Better handle SyntaxError in Action View.
54
+
55
+ *Mario Caropreso*
56
+
57
+ * Fix `word_wrap` with empty string.
58
+
59
+ *Jonathan Hefner*
60
+
61
+ * Rename `ActionView::TestCase::Behavior::Content` to `ActionView::TestCase::Behavior::RenderedViewContent`.
62
+
63
+ Make `RenderedViewContent` inherit from `String`. Make private API with `:nodoc:`.
64
+
65
+ *Sean Doyle*
66
+
67
+ * Fix detection of required strict locals.
68
+
69
+ Further fix `render @collection` compatibility with strict locals
70
+
71
+ *Jean Boussier*
72
+
73
+
1
74
  ## Rails 7.1.2 (November 10, 2023) ##
2
75
 
3
76
  * Fix the `number_to_human_size` view helper to correctly work with negative numbers.
@@ -136,8 +136,7 @@ module ActionView # :nodoc:
136
136
  # end
137
137
  # end
138
138
  #
139
- # For more information on Builder please consult the {source
140
- # code}[https://github.com/jimweirich/builder].
139
+ # For more information on Builder please consult the {source code}[https://github.com/rails/builder].
141
140
  class Base
142
141
  include Helpers, ::ERB::Util, Context
143
142
 
@@ -9,7 +9,7 @@ module ActionView
9
9
  module VERSION
10
10
  MAJOR = 7
11
11
  MINOR = 1
12
- TINY = 2
12
+ TINY = 5
13
13
  PRE = nil
14
14
 
15
15
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".")
@@ -645,11 +645,11 @@ module ActionView
645
645
  return if response_present && response.sending?
646
646
 
647
647
  if respond_to?(:request) && request
648
- request.send_early_hints("Link" => preload_links.join("\n"))
648
+ request.send_early_hints("link" => preload_links.join(","))
649
649
  end
650
650
 
651
651
  if response_present
652
- header = +response.headers["Link"].to_s
652
+ header = +response.headers["link"].to_s
653
653
  preload_links.each do |link|
654
654
  break if header.bytesize + link.bytesize > max_header_size
655
655
 
@@ -660,7 +660,7 @@ module ActionView
660
660
  end
661
661
  end
662
662
 
663
- response.headers["Link"] = header
663
+ response.headers["link"] = header
664
664
  end
665
665
  end
666
666
  end
@@ -76,11 +76,11 @@ module ActionView
76
76
  # render 'comments/comments'
77
77
  # render('comments/comments')
78
78
  #
79
- # render "header" translates to render("comments/header")
79
+ # render "header" # translates to render("comments/header")
80
80
  #
81
- # render(@topic) translates to render("topics/topic")
82
- # render(topics) translates to render("topics/topic")
83
- # render(message.topics) translates to render("topics/topic")
81
+ # render(@topic) # translates to render("topics/topic")
82
+ # render(topics) # translates to render("topics/topic")
83
+ # render(message.topics) # translates to render("topics/topic")
84
84
  #
85
85
  # It's not possible to derive all render calls like that, though.
86
86
  # Here are a few examples of things that can't be derived:
@@ -1006,8 +1006,8 @@ module ActionView
1006
1006
  # <% end %>
1007
1007
  #
1008
1008
  # When a collection is used you might want to know the index of each
1009
- # object into the array. For this purpose, the <tt>index</tt> method
1010
- # is available in the FormBuilder object.
1009
+ # object in the array. For this purpose, the <tt>index</tt> method is
1010
+ # available in the FormBuilder object.
1011
1011
  #
1012
1012
  # <%= form_for @person do |person_form| %>
1013
1013
  # ...
@@ -1118,6 +1118,8 @@ module ActionView
1118
1118
  # post:
1119
1119
  # cost: "Total cost"
1120
1120
  #
1121
+ # <code></code>
1122
+ #
1121
1123
  # label(:post, :cost)
1122
1124
  # # => <label for="post_cost">Total cost</label>
1123
1125
  #
@@ -1779,12 +1781,12 @@ module ActionView
1779
1781
  #
1780
1782
  # <%= form_for @post do |f| %>
1781
1783
  # <%= f.text_field :title, name: f.field_name(:title, :subtitle) %>
1782
- # <%# => <input type="text" name="post[title][subtitle]">
1784
+ # <%# => <input type="text" name="post[title][subtitle]"> %>
1783
1785
  # <% end %>
1784
1786
  #
1785
1787
  # <%= form_for @post do |f| %>
1786
1788
  # <%= f.text_field :tag, name: f.field_name(:tag, multiple: true) %>
1787
- # <%# => <input type="text" name="post[tag][]">
1789
+ # <%# => <input type="text" name="post[tag][]"> %>
1788
1790
  # <% end %>
1789
1791
  #
1790
1792
  def field_name(method, *methods, multiple: false, index: @options[:index])
@@ -2358,6 +2360,8 @@ module ActionView
2358
2360
  # post:
2359
2361
  # cost: "Total cost"
2360
2362
  #
2363
+ # <code></code>
2364
+ #
2361
2365
  # label(:cost)
2362
2366
  # # => <label for="post_cost">Total cost</label>
2363
2367
  #
@@ -91,11 +91,11 @@ module ActionView
91
91
  # attribute name.
92
92
  #
93
93
  # <%= label_tag :post, :title %>
94
- # <%= text_field_tag :post, :title, aria: { describedby: field_id(:post, :title, :error) } %>
94
+ # <%= text_field :post, :title, aria: { describedby: field_id(:post, :title, :error) } %>
95
95
  # <%= tag.span("is blank", id: field_id(:post, :title, :error) %>
96
96
  #
97
97
  # In the example above, the <tt><input type="text"></tt> element built by
98
- # the call to <tt>text_field_tag</tt> declares an
98
+ # the call to <tt>text_field</tt> declares an
99
99
  # <tt>aria-describedby</tt> attribute referencing the <tt><span></tt>
100
100
  # element, sharing a common <tt>id</tt> root (<tt>post_title</tt>, in this
101
101
  # case).
@@ -123,11 +123,11 @@ module ActionView
123
123
  # Return the value generated by the <tt>FormBuilder</tt> for the given
124
124
  # attribute name.
125
125
  #
126
- # <%= text_field_tag :post, :title, name: field_name(:post, :title, :subtitle) %>
127
- # <%# => <input type="text" name="post[title][subtitle]">
126
+ # <%= text_field :post, :title, name: field_name(:post, :title, :subtitle) %>
127
+ # <%# => <input type="text" name="post[title][subtitle]"> %>
128
128
  #
129
- # <%= text_field_tag :post, :tag, name: field_name(:post, :tag, multiple: true) %>
130
- # <%# => <input type="text" name="post[tag][]">
129
+ # <%= text_field :post, :tag, name: field_name(:post, :tag, multiple: true) %>
130
+ # <%# => <input type="text" name="post[tag][]"> %>
131
131
  #
132
132
  def field_name(object_name, method_name, *method_names, multiple: false, index: nil)
133
133
  names = method_names.map! { |name| "[#{name}]" }.join
@@ -24,42 +24,14 @@ module ActionView
24
24
  end
25
25
  end
26
26
 
27
- # Formats a +number+ into a phone number (US by default e.g., (555)
28
- # 123-9876). You can customize the format in the +options+ hash.
27
+ # Delegates to ActiveSupport::NumberHelper#number_to_phone.
29
28
  #
30
- # ==== Options
29
+ # Additionally, supports a +:raise+ option that will cause
30
+ # InvalidNumberError to be raised if +number+ is not a valid number:
31
31
  #
32
- # * <tt>:area_code</tt> - Adds parentheses around the area code.
33
- # * <tt>:delimiter</tt> - Specifies the delimiter to use
34
- # (defaults to "-").
35
- # * <tt>:extension</tt> - Specifies an extension to add to the
36
- # end of the generated number.
37
- # * <tt>:country_code</tt> - Sets the country code for the phone
38
- # number.
39
- # * <tt>:pattern</tt> - Specifies how the number is divided into three
40
- # groups with the custom regexp to override the default format.
41
- # * <tt>:raise</tt> - If true, raises +InvalidNumberError+ when
42
- # the argument is invalid.
32
+ # number_to_phone("12x34") # => "12x34"
33
+ # number_to_phone("12x34", raise: true) # => InvalidNumberError
43
34
  #
44
- # ==== Examples
45
- #
46
- # number_to_phone(5551234) # => 555-1234
47
- # number_to_phone("5551234") # => 555-1234
48
- # number_to_phone(1235551234) # => 123-555-1234
49
- # number_to_phone(1235551234, area_code: true) # => (123) 555-1234
50
- # number_to_phone(1235551234, delimiter: " ") # => 123 555 1234
51
- # number_to_phone(1235551234, area_code: true, extension: 555) # => (123) 555-1234 x 555
52
- # number_to_phone(1235551234, country_code: 1) # => +1-123-555-1234
53
- # number_to_phone("123a456") # => 123a456
54
- # number_to_phone("1234a567", raise: true) # => InvalidNumberError
55
- #
56
- # number_to_phone(1235551234, country_code: 1, extension: 1343, delimiter: ".")
57
- # # => +1.123.555.1234 x 1343
58
- #
59
- # number_to_phone(75561234567, pattern: /(\d{1,4})(\d{4})(\d{4})$/, area_code: true)
60
- # # => "(755) 6123-4567"
61
- # number_to_phone(13312345678, pattern: /(\d{3})(\d{4})(\d{4})$/)
62
- # # => "133-1234-5678"
63
35
  def number_to_phone(number, options = {})
64
36
  return unless number
65
37
  options = options.symbolize_keys
@@ -68,339 +40,73 @@ module ActionView
68
40
  ERB::Util.html_escape(ActiveSupport::NumberHelper.number_to_phone(number, options))
69
41
  end
70
42
 
71
- # Formats a +number+ into a currency string (e.g., $13.65). You
72
- # can customize the format in the +options+ hash.
73
- #
74
- # The currency unit and number formatting of the current locale will be used
75
- # unless otherwise specified in the provided options. No currency conversion
76
- # is performed. If the user is given a way to change their locale, they will
77
- # also be able to change the relative value of the currency displayed with
78
- # this helper. If your application will ever support multiple locales, you
79
- # may want to specify a constant <tt>:locale</tt> option or consider
80
- # using a library capable of currency conversion.
43
+ # Delegates to ActiveSupport::NumberHelper#number_to_currency.
81
44
  #
82
- # ==== Options
45
+ # Additionally, supports a +:raise+ option that will cause
46
+ # InvalidNumberError to be raised if +number+ is not a valid number:
83
47
  #
84
- # * <tt>:locale</tt> - Sets the locale to be used for formatting
85
- # (defaults to current locale).
86
- # * <tt>:precision</tt> - Sets the level of precision (defaults
87
- # to 2).
88
- # * <tt>:unit</tt> - Sets the denomination of the currency
89
- # (defaults to "$").
90
- # * <tt>:separator</tt> - Sets the separator between the units
91
- # (defaults to ".").
92
- # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
93
- # to ",").
94
- # * <tt>:format</tt> - Sets the format for non-negative numbers
95
- # (defaults to "%u%n"). Fields are <tt>%u</tt> for the
96
- # currency, and <tt>%n</tt> for the number.
97
- # * <tt>:negative_format</tt> - Sets the format for negative
98
- # numbers (defaults to prepending a hyphen to the formatted
99
- # number given by <tt>:format</tt>). Accepts the same fields
100
- # than <tt>:format</tt>, except <tt>%n</tt> is here the
101
- # absolute value of the number.
102
- # * <tt>:raise</tt> - If true, raises +InvalidNumberError+ when
103
- # the argument is invalid.
104
- # * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
105
- # insignificant zeros after the decimal separator (defaults to
106
- # +false+).
48
+ # number_to_currency("12x34") # => "$12x34"
49
+ # number_to_currency("12x34", raise: true) # => InvalidNumberError
107
50
  #
108
- # ==== Examples
109
- #
110
- # number_to_currency(1234567890.50) # => $1,234,567,890.50
111
- # number_to_currency(1234567890.506) # => $1,234,567,890.51
112
- # number_to_currency(1234567890.506, precision: 3) # => $1,234,567,890.506
113
- # number_to_currency(1234567890.506, locale: :fr) # => 1 234 567 890,51 €
114
- # number_to_currency("123a456") # => $123a456
115
- #
116
- # number_to_currency("123a456", raise: true) # => InvalidNumberError
117
- #
118
- # number_to_currency(-0.456789, precision: 0)
119
- # # => "$0"
120
- # number_to_currency(-1234567890.50, negative_format: "(%u%n)")
121
- # # => ($1,234,567,890.50)
122
- # number_to_currency(1234567890.50, unit: "R$", separator: ",", delimiter: "")
123
- # # => R$1234567890,50
124
- # number_to_currency(1234567890.50, unit: "R$", separator: ",", delimiter: "", format: "%n %u")
125
- # # => 1234567890,50 R$
126
- # number_to_currency(1234567890.50, strip_insignificant_zeros: true)
127
- # # => "$1,234,567,890.5"
128
51
  def number_to_currency(number, options = {})
129
52
  delegate_number_helper_method(:number_to_currency, number, options)
130
53
  end
131
54
 
132
- # Formats a +number+ as a percentage string (e.g., 65%). You can
133
- # customize the format in the +options+ hash.
134
- #
135
- # ==== Options
55
+ # Delegates to ActiveSupport::NumberHelper#number_to_percentage.
136
56
  #
137
- # * <tt>:locale</tt> - Sets the locale to be used for formatting
138
- # (defaults to current locale).
139
- # * <tt>:precision</tt> - Sets the precision of the number
140
- # (defaults to 3).
141
- # * <tt>:significant</tt> - If +true+, precision will be the number
142
- # of significant_digits. If +false+, the number of fractional
143
- # digits (defaults to +false+).
144
- # * <tt>:separator</tt> - Sets the separator between the
145
- # fractional and integer digits (defaults to ".").
146
- # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
147
- # to "").
148
- # * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
149
- # insignificant zeros after the decimal separator (defaults to
150
- # +false+).
151
- # * <tt>:format</tt> - Specifies the format of the percentage
152
- # string The number field is <tt>%n</tt> (defaults to "%n%").
153
- # * <tt>:raise</tt> - If true, raises +InvalidNumberError+ when
154
- # the argument is invalid.
57
+ # Additionally, supports a +:raise+ option that will cause
58
+ # InvalidNumberError to be raised if +number+ is not a valid number:
155
59
  #
156
- # ==== Examples
60
+ # number_to_percentage("99x") # => "99x%"
61
+ # number_to_percentage("99x", raise: true) # => InvalidNumberError
157
62
  #
158
- # number_to_percentage(100) # => 100.000%
159
- # number_to_percentage("98") # => 98.000%
160
- # number_to_percentage(100, precision: 0) # => 100%
161
- # number_to_percentage(1000, delimiter: '.', separator: ',') # => 1.000,000%
162
- # number_to_percentage(302.24398923423, precision: 5) # => 302.24399%
163
- # number_to_percentage(1000, locale: :fr) # => 1 000,000%
164
- # number_to_percentage("98a") # => 98a%
165
- # number_to_percentage(100, format: "%n %") # => 100.000 %
166
- #
167
- # number_to_percentage("98a", raise: true) # => InvalidNumberError
168
63
  def number_to_percentage(number, options = {})
169
64
  delegate_number_helper_method(:number_to_percentage, number, options)
170
65
  end
171
66
 
172
- # Formats a +number+ with grouped thousands using +delimiter+
173
- # (e.g., 12,324). You can customize the format in the +options+
174
- # hash.
175
- #
176
- # ==== Options
177
- #
178
- # * <tt>:locale</tt> - Sets the locale to be used for formatting
179
- # (defaults to current locale).
180
- # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
181
- # to ",").
182
- # * <tt>:separator</tt> - Sets the separator between the
183
- # fractional and integer digits (defaults to ".").
184
- # * <tt>:delimiter_pattern</tt> - Sets a custom regular expression used for
185
- # deriving the placement of delimiter. Helpful when using currency formats
186
- # like INR.
187
- # * <tt>:raise</tt> - If true, raises +InvalidNumberError+ when
188
- # the argument is invalid.
189
- #
190
- # ==== Examples
67
+ # Delegates to ActiveSupport::NumberHelper#number_to_delimited.
191
68
  #
192
- # number_with_delimiter(12345678) # => 12,345,678
193
- # number_with_delimiter("123456") # => 123,456
194
- # number_with_delimiter(12345678.05) # => 12,345,678.05
195
- # number_with_delimiter(12345678, delimiter: ".") # => 12.345.678
196
- # number_with_delimiter(12345678, delimiter: ",") # => 12,345,678
197
- # number_with_delimiter(12345678.05, separator: " ") # => 12,345,678 05
198
- # number_with_delimiter(12345678.05, locale: :fr) # => 12 345 678,05
199
- # number_with_delimiter("112a") # => 112a
200
- # number_with_delimiter(98765432.98, delimiter: " ", separator: ",")
201
- # # => 98 765 432,98
69
+ # Additionally, supports a +:raise+ option that will cause
70
+ # InvalidNumberError to be raised if +number+ is not a valid number:
202
71
  #
203
- # number_with_delimiter("123456.78",
204
- # delimiter_pattern: /(\d+?)(?=(\d\d)+(\d)(?!\d))/) # => "1,23,456.78"
72
+ # number_with_delimiter("12x34") # => "12x34"
73
+ # number_with_delimiter("12x34", raise: true) # => InvalidNumberError
205
74
  #
206
- # number_with_delimiter("112a", raise: true) # => raise InvalidNumberError
207
75
  def number_with_delimiter(number, options = {})
208
76
  delegate_number_helper_method(:number_to_delimited, number, options)
209
77
  end
210
78
 
211
- # Formats a +number+ with the specified level of
212
- # <tt>:precision</tt> (e.g., 112.32 has a precision of 2 if
213
- # +:significant+ is +false+, and 5 if +:significant+ is +true+).
214
- # You can customize the format in the +options+ hash.
215
- #
216
- # ==== Options
217
- #
218
- # * <tt>:locale</tt> - Sets the locale to be used for formatting
219
- # (defaults to current locale).
220
- # * <tt>:precision</tt> - Sets the precision of the number
221
- # (defaults to 3).
222
- # * <tt>:significant</tt> - If +true+, precision will be the number
223
- # of significant_digits. If +false+, the number of fractional
224
- # digits (defaults to +false+).
225
- # * <tt>:separator</tt> - Sets the separator between the
226
- # fractional and integer digits (defaults to ".").
227
- # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
228
- # to "").
229
- # * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
230
- # insignificant zeros after the decimal separator (defaults to
231
- # +false+).
232
- # * <tt>:raise</tt> - If true, raises +InvalidNumberError+ when
233
- # the argument is invalid.
234
- #
235
- # ==== Examples
79
+ # Delegates to ActiveSupport::NumberHelper#number_to_rounded.
236
80
  #
237
- # number_with_precision(111.2345) # => 111.235
238
- # number_with_precision(111.2345, precision: 2) # => 111.23
239
- # number_with_precision(13, precision: 5) # => 13.00000
240
- # number_with_precision(389.32314, precision: 0) # => 389
241
- # number_with_precision(111.2345, significant: true) # => 111
242
- # number_with_precision(111.2345, precision: 1, significant: true) # => 100
243
- # number_with_precision(13, precision: 5, significant: true) # => 13.000
244
- # number_with_precision(111.234, locale: :fr) # => 111,234
81
+ # Additionally, supports a +:raise+ option that will cause
82
+ # InvalidNumberError to be raised if +number+ is not a valid number:
245
83
  #
246
- # number_with_precision(13, precision: 5, significant: true, strip_insignificant_zeros: true)
247
- # # => 13
84
+ # number_with_precision("12x34") # => "12x34"
85
+ # number_with_precision("12x34", raise: true) # => InvalidNumberError
248
86
  #
249
- # number_with_precision(389.32314, precision: 4, significant: true) # => 389.3
250
- # number_with_precision(1111.2345, precision: 2, separator: ',', delimiter: '.')
251
- # # => 1.111,23
252
87
  def number_with_precision(number, options = {})
253
88
  delegate_number_helper_method(:number_to_rounded, number, options)
254
89
  end
255
90
 
256
- # Formats the bytes in +number+ into a more understandable
257
- # representation (e.g., giving it 1500 yields 1.46 KB). This
258
- # method is useful for reporting file sizes to users. You can
259
- # customize the format in the +options+ hash.
91
+ # Delegates to ActiveSupport::NumberHelper#number_to_human_size.
260
92
  #
261
- # See <tt>number_to_human</tt> if you want to pretty-print a
262
- # generic number.
93
+ # Additionally, supports a +:raise+ option that will cause
94
+ # InvalidNumberError to be raised if +number+ is not a valid number:
263
95
  #
264
- # ==== Options
96
+ # number_to_human_size("12x34") # => "12x34"
97
+ # number_to_human_size("12x34", raise: true) # => InvalidNumberError
265
98
  #
266
- # * <tt>:locale</tt> - Sets the locale to be used for formatting
267
- # (defaults to current locale).
268
- # * <tt>:precision</tt> - Sets the precision of the number
269
- # (defaults to 3).
270
- # * <tt>:significant</tt> - If +true+, precision will be the number
271
- # of significant_digits. If +false+, the number of fractional
272
- # digits (defaults to +true+)
273
- # * <tt>:separator</tt> - Sets the separator between the
274
- # fractional and integer digits (defaults to ".").
275
- # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
276
- # to "").
277
- # * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
278
- # insignificant zeros after the decimal separator (defaults to
279
- # +true+)
280
- # * <tt>:raise</tt> - If true, raises +InvalidNumberError+ when
281
- # the argument is invalid.
282
- #
283
- # ==== Examples
284
- #
285
- # number_to_human_size(123) # => 123 Bytes
286
- # number_to_human_size(1234) # => 1.21 KB
287
- # number_to_human_size(12345) # => 12.1 KB
288
- # number_to_human_size(1234567) # => 1.18 MB
289
- # number_to_human_size(1234567890) # => 1.15 GB
290
- # number_to_human_size(1234567890123) # => 1.12 TB
291
- # number_to_human_size(1234567890123456) # => 1.1 PB
292
- # number_to_human_size(1234567890123456789) # => 1.07 EB
293
- # number_to_human_size(1234567, precision: 2) # => 1.2 MB
294
- # number_to_human_size(483989, precision: 2) # => 470 KB
295
- # number_to_human_size(1234567, precision: 2, separator: ',') # => 1,2 MB
296
- # number_to_human_size(1234567890123, precision: 5) # => "1.1228 TB"
297
- # number_to_human_size(524288000, precision: 5) # => "500 MB"
298
99
  def number_to_human_size(number, options = {})
299
100
  delegate_number_helper_method(:number_to_human_size, number, options)
300
101
  end
301
102
 
302
- # Pretty prints (formats and approximates) a number in a way it
303
- # is more readable by humans (e.g.: 1200000000 becomes "1.2
304
- # Billion"). This is useful for numbers that can get very large
305
- # (and too hard to read).
306
- #
307
- # See <tt>number_to_human_size</tt> if you want to print a file
308
- # size.
309
- #
310
- # You can also define your own unit-quantifier names if you want
311
- # to use other decimal units (e.g.: 1500 becomes "1.5
312
- # kilometers", 0.150 becomes "150 milliliters", etc). You may
313
- # define a wide range of unit quantifiers, even fractional ones
314
- # (centi, deci, mili, etc).
315
- #
316
- # ==== Options
317
- #
318
- # * <tt>:locale</tt> - Sets the locale to be used for formatting
319
- # (defaults to current locale).
320
- # * <tt>:precision</tt> - Sets the precision of the number
321
- # (defaults to 3).
322
- # * <tt>:significant</tt> - If +true+, precision will be the number
323
- # of significant_digits. If +false+, the number of fractional
324
- # digits (defaults to +true+)
325
- # * <tt>:separator</tt> - Sets the separator between the
326
- # fractional and integer digits (defaults to ".").
327
- # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
328
- # to "").
329
- # * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
330
- # insignificant zeros after the decimal separator (defaults to
331
- # +true+)
332
- # * <tt>:units</tt> - A Hash of unit quantifier names. Or a
333
- # string containing an i18n scope where to find this hash. It
334
- # might have the following keys:
335
- # * *integers*: <tt>:unit</tt>, <tt>:ten</tt>,
336
- # <tt>:hundred</tt>, <tt>:thousand</tt>, <tt>:million</tt>,
337
- # <tt>:billion</tt>, <tt>:trillion</tt>,
338
- # <tt>:quadrillion</tt>
339
- # * *fractionals*: <tt>:deci</tt>, <tt>:centi</tt>,
340
- # <tt>:mili</tt>, <tt>:micro</tt>, <tt>:nano</tt>,
341
- # <tt>:pico</tt>, <tt>:femto</tt>
342
- # * <tt>:format</tt> - Sets the format of the output string
343
- # (defaults to "%n %u"). The field types are:
344
- # * %u - The quantifier (ex.: 'thousand')
345
- # * %n - The number
346
- # * <tt>:raise</tt> - If true, raises +InvalidNumberError+ when
347
- # the argument is invalid.
348
- #
349
- # ==== Examples
350
- #
351
- # number_to_human(123) # => "123"
352
- # number_to_human(1234) # => "1.23 Thousand"
353
- # number_to_human(12345) # => "12.3 Thousand"
354
- # number_to_human(1234567) # => "1.23 Million"
355
- # number_to_human(1234567890) # => "1.23 Billion"
356
- # number_to_human(1234567890123) # => "1.23 Trillion"
357
- # number_to_human(1234567890123456) # => "1.23 Quadrillion"
358
- # number_to_human(1234567890123456789) # => "1230 Quadrillion"
359
- # number_to_human(489939, precision: 2) # => "490 Thousand"
360
- # number_to_human(489939, precision: 4) # => "489.9 Thousand"
361
- # number_to_human(1234567, precision: 4,
362
- # significant: false) # => "1.2346 Million"
363
- # number_to_human(1234567, precision: 1,
364
- # separator: ',',
365
- # significant: false) # => "1,2 Million"
366
- #
367
- # number_to_human(500000000, precision: 5) # => "500 Million"
368
- # number_to_human(12345012345, significant: false) # => "12.345 Billion"
369
- #
370
- # Non-significant zeros after the decimal separator are stripped
371
- # out by default (set <tt>:strip_insignificant_zeros</tt> to
372
- # +false+ to change that):
373
- #
374
- # number_to_human(12.00001) # => "12"
375
- # number_to_human(12.00001, strip_insignificant_zeros: false) # => "12.0"
376
- #
377
- # ==== Custom Unit Quantifiers
378
- #
379
- # You can also use your own custom unit quantifiers:
380
- #
381
- # number_to_human(500000, units: {unit: "ml", thousand: "lt"}) # => "500 lt"
382
- #
383
- # If in your I18n locale you have:
384
- # distance:
385
- # centi:
386
- # one: "centimeter"
387
- # other: "centimeters"
388
- # unit:
389
- # one: "meter"
390
- # other: "meters"
391
- # thousand:
392
- # one: "kilometer"
393
- # other: "kilometers"
394
- # billion: "gazillion-distance"
103
+ # Delegates to ActiveSupport::NumberHelper#number_to_human.
395
104
  #
396
- # Then you could do:
105
+ # Additionally, supports a +:raise+ option that will cause
106
+ # InvalidNumberError to be raised if +number+ is not a valid number:
397
107
  #
398
- # number_to_human(543934, units: :distance) # => "544 kilometers"
399
- # number_to_human(54393498, units: :distance) # => "54400 kilometers"
400
- # number_to_human(54393498000, units: :distance) # => "54.4 gazillion-distance"
401
- # number_to_human(343, units: :distance, precision: 1) # => "300 meters"
402
- # number_to_human(1, units: :distance) # => "1 meter"
403
- # number_to_human(0.34, units: :distance) # => "34 centimeters"
108
+ # number_to_human("12x34") # => "12x34"
109
+ # number_to_human("12x34", raise: true) # => InvalidNumberError
404
110
  #
405
111
  def number_to_human(number, options = {})
406
112
  delegate_number_helper_method(:number_to_human, number, options)