actionview 7.1.1 → 7.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ee3c480e4d31e8f0993748f63f865fdc83fd933b17cc38cdcf40bb6f2f5a851f
4
- data.tar.gz: 55cc85a03a85c26914ca1efcf502749f7d68d2c60ccb9ff1d7da04817f9d3abf
3
+ metadata.gz: 922d7936de34cfa626cd9cec0f32208e1fe48086f75be08b08e78a28911fa1fb
4
+ data.tar.gz: d24e4bcf03ad6c510536f5b423664f3877ac07873cfccdbff1cd087c081e7455
5
5
  SHA512:
6
- metadata.gz: 458e12bf5a056fa2302d7be0b8fe01ba73403a1d02e442adaecb9dc791400b08bf8ec8cc0107e221284c5836b811668a038a8e11246201bec2306b23a5059fa9
7
- data.tar.gz: 85e8be0ed9174c2768d415360f00dfccf0a4d51df657581fd9407a2c9d5c7d91b1e19c604964ecc03b8f3300960f62e2baaf66f01c332b7fa927de67da4b214e
6
+ metadata.gz: 9b99bd07d8343f338a2a20bb0739efd3bf2ce7e05efba079f7971905772e282675b995ecc4cc56d731c7cc75ab9b1ea252c64d1d6041b303d46d05f2bbb1427a
7
+ data.tar.gz: 5f9702c425eab497ce8d6c2123ad8a8b57a17c1708286bf1b3b9ed8ad447dec4bf0c22c0e1011f6a88de4d4305efc5484b16532f5af8752d2e7f3e9d78876edc
data/CHANGELOG.md CHANGED
@@ -1,3 +1,52 @@
1
+ ## Rails 7.1.3 (January 16, 2024) ##
2
+
3
+ * Better handle SyntaxError in Action View.
4
+
5
+ *Mario Caropreso*
6
+
7
+ * Fix `word_wrap` with empty string.
8
+
9
+ *Jonathan Hefner*
10
+
11
+ * Rename `ActionView::TestCase::Behavior::Content` to `ActionView::TestCase::Behavior::RenderedViewContent`.
12
+
13
+ Make `RenderedViewContent` inherit from `String`. Make private API with `:nodoc:`.
14
+
15
+ *Sean Doyle*
16
+
17
+ * Fix detection of required strict locals.
18
+
19
+ Further fix `render @collection` compatibility with strict locals
20
+
21
+ *Jean Boussier*
22
+
23
+
24
+ ## Rails 7.1.2 (November 10, 2023) ##
25
+
26
+ * Fix the `number_to_human_size` view helper to correctly work with negative numbers.
27
+
28
+ *Earlopain*
29
+
30
+ * Automatically discard the implicit locals injected by collection rendering for template that can't accept them
31
+
32
+ When rendering a collection, two implicit variables are injected, which breaks templates with strict locals.
33
+
34
+ Now they are only passed if the template will actually accept them.
35
+
36
+ *Yasha Krasnou*, *Jean Boussier*
37
+
38
+ * Fix `@rails/ujs` calling `start()` an extra time when using bundlers
39
+
40
+ *Hartley McGuire*, *Ryunosuke Sato*
41
+
42
+ * Fix the `capture` view helper compatibility with HAML and Slim
43
+
44
+ When a blank string was captured in HAML or Slim (and possibly other template engines)
45
+ it would instead return the entire buffer.
46
+
47
+ *Jean Boussier*
48
+
49
+
1
50
  ## Rails 7.1.1 (October 11, 2023) ##
2
51
 
3
52
  * Updated `@rails/ujs` files to ignore certain data-* attributes when element is contenteditable.
@@ -683,11 +683,4 @@ if (typeof jQuery !== "undefined" && jQuery && jQuery.ajax) {
683
683
  }));
684
684
  }
685
685
 
686
- if (typeof exports !== "object" && typeof module === "undefined") {
687
- window.Rails = Rails;
688
- if (fire(document, "rails:attachBindings")) {
689
- start();
690
- }
691
- }
692
-
693
686
  export { Rails as default };
@@ -9,7 +9,7 @@ module ActionView
9
9
  module VERSION
10
10
  MAJOR = 7
11
11
  MINOR = 1
12
- TINY = 1
12
+ TINY = 3
13
13
  PRE = nil
14
14
 
15
15
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".")
@@ -378,8 +378,8 @@ module ActionView
378
378
  # You can add HTML attributes using the +options+. The +options+ supports
379
379
  # additional keys for convenience and conformance:
380
380
  #
381
- # * <tt>:size</tt> - Supplied as "{Width}x{Height}" or "{Number}", so "30x45" becomes
382
- # width="30" and height="45", and "50" becomes width="50" and height="50".
381
+ # * <tt>:size</tt> - Supplied as <tt>"#{width}x#{height}"</tt> or <tt>"#{number}"</tt>, so <tt>"30x45"</tt> becomes
382
+ # <tt>width="30" height="45"</tt>, and <tt>"50"</tt> becomes <tt>width="50" height="50"</tt>.
383
383
  # <tt>:size</tt> will be ignored if the value is not in the correct format.
384
384
  # * <tt>:srcset</tt> - If supplied as a hash or array of <tt>[source, descriptor]</tt>
385
385
  # pairs, each image path will be expanded before the list is formatted as a string.
@@ -511,8 +511,8 @@ module ActionView
511
511
  #
512
512
  # * <tt>:poster</tt> - Set an image (like a screenshot) to be shown
513
513
  # before the video loads. The path is calculated like the +src+ of +image_tag+.
514
- # * <tt>:size</tt> - Supplied as "{Width}x{Height}" or "{Number}", so "30x45" becomes
515
- # width="30" and height="45", and "50" becomes width="50" and height="50".
514
+ # * <tt>:size</tt> - Supplied as <tt>"#{width}x#{height}"</tt> or <tt>"#{number}"</tt>, so <tt>"30x45"</tt> becomes
515
+ # <tt>width="30" height="45"</tt>, and <tt>"50"</tt> becomes <tt>width="50" height="50"</tt>.
516
516
  # <tt>:size</tt> will be ignored if the value is not in the correct format.
517
517
  # * <tt>:poster_skip_pipeline</tt> will bypass the asset pipeline when using
518
518
  # the <tt>:poster</tt> option instead using an asset in the public folder.
@@ -49,7 +49,13 @@ module ActionView
49
49
  @output_buffer ||= ActionView::OutputBuffer.new
50
50
  buffer = @output_buffer.capture { value = yield(*args) }
51
51
 
52
- case string = buffer.presence || value
52
+ string = if @output_buffer.equal?(value)
53
+ buffer
54
+ else
55
+ buffer.presence || value
56
+ end
57
+
58
+ case string
53
59
  when OutputBuffer
54
60
  string.to_s
55
61
  when ActiveSupport::SafeBuffer
@@ -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
- # <%= f.field_tag :tag, name: f.field_name(:tag, multiple: true) %>
1787
- # <%# => <input type="text" name="post[tag][]">
1788
+ # <%= f.text_field :tag, name: f.field_name(:tag, multiple: true) %>
1789
+ # <%# => <input type="text" name="post[tag][]"> %>
1788
1790
  # <% end %>
1789
1791
  #
1790
1792
  def field_name(method, *methods, multiple: false, index: @options[:index])
@@ -2263,7 +2265,7 @@ module ActionView
2263
2265
  # <% end %>
2264
2266
  #
2265
2267
  # When a collection is used you might want to know the index of each
2266
- # object into the array. For this purpose, the <tt>index</tt> method
2268
+ # object in the array. For this purpose, the <tt>index</tt> method
2267
2269
  # is available in the FormBuilder object.
2268
2270
  #
2269
2271
  # <%= form_for @person do |person_form| %>
@@ -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)