actionview 6.1.7.2 → 7.1.3

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.
Files changed (103) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +299 -277
  3. data/MIT-LICENSE +2 -1
  4. data/README.rdoc +3 -3
  5. data/app/assets/javascripts/rails-ujs.esm.js +686 -0
  6. data/app/assets/javascripts/rails-ujs.js +630 -0
  7. data/lib/action_view/base.rb +37 -19
  8. data/lib/action_view/buffers.rb +107 -9
  9. data/lib/action_view/cache_expiry.rb +48 -37
  10. data/lib/action_view/context.rb +1 -1
  11. data/lib/action_view/dependency_tracker/erb_tracker.rb +154 -0
  12. data/lib/action_view/dependency_tracker/ripper_tracker.rb +59 -0
  13. data/lib/action_view/dependency_tracker.rb +6 -147
  14. data/lib/action_view/deprecator.rb +7 -0
  15. data/lib/action_view/digestor.rb +8 -5
  16. data/lib/action_view/flows.rb +4 -4
  17. data/lib/action_view/gem_version.rb +4 -4
  18. data/lib/action_view/helpers/active_model_helper.rb +3 -3
  19. data/lib/action_view/helpers/asset_tag_helper.rb +200 -60
  20. data/lib/action_view/helpers/asset_url_helper.rb +22 -21
  21. data/lib/action_view/helpers/atom_feed_helper.rb +8 -9
  22. data/lib/action_view/helpers/cache_helper.rb +55 -12
  23. data/lib/action_view/helpers/capture_helper.rb +34 -14
  24. data/lib/action_view/helpers/content_exfiltration_prevention_helper.rb +70 -0
  25. data/lib/action_view/helpers/controller_helper.rb +8 -2
  26. data/lib/action_view/helpers/csp_helper.rb +3 -3
  27. data/lib/action_view/helpers/csrf_helper.rb +4 -4
  28. data/lib/action_view/helpers/date_helper.rb +123 -57
  29. data/lib/action_view/helpers/debug_helper.rb +6 -4
  30. data/lib/action_view/helpers/form_helper.rb +253 -97
  31. data/lib/action_view/helpers/form_options_helper.rb +72 -34
  32. data/lib/action_view/helpers/form_tag_helper.rb +189 -58
  33. data/lib/action_view/helpers/javascript_helper.rb +4 -5
  34. data/lib/action_view/helpers/number_helper.rb +43 -335
  35. data/lib/action_view/helpers/output_safety_helper.rb +6 -6
  36. data/lib/action_view/helpers/rendering_helper.rb +6 -7
  37. data/lib/action_view/helpers/sanitize_helper.rb +54 -24
  38. data/lib/action_view/helpers/tag_helper.rb +42 -35
  39. data/lib/action_view/helpers/tags/base.rb +16 -77
  40. data/lib/action_view/helpers/tags/check_box.rb +1 -1
  41. data/lib/action_view/helpers/tags/collection_check_boxes.rb +1 -0
  42. data/lib/action_view/helpers/tags/collection_radio_buttons.rb +1 -0
  43. data/lib/action_view/helpers/tags/collection_select.rb +4 -1
  44. data/lib/action_view/helpers/tags/date_field.rb +1 -1
  45. data/lib/action_view/helpers/tags/date_select.rb +2 -0
  46. data/lib/action_view/helpers/tags/datetime_field.rb +14 -6
  47. data/lib/action_view/helpers/tags/datetime_local_field.rb +11 -2
  48. data/lib/action_view/helpers/tags/file_field.rb +16 -0
  49. data/lib/action_view/helpers/tags/grouped_collection_select.rb +3 -0
  50. data/lib/action_view/helpers/tags/month_field.rb +1 -1
  51. data/lib/action_view/helpers/tags/select.rb +4 -1
  52. data/lib/action_view/helpers/tags/select_renderer.rb +56 -0
  53. data/lib/action_view/helpers/tags/time_field.rb +11 -2
  54. data/lib/action_view/helpers/tags/time_zone_select.rb +3 -0
  55. data/lib/action_view/helpers/tags/week_field.rb +1 -1
  56. data/lib/action_view/helpers/tags/weekday_select.rb +31 -0
  57. data/lib/action_view/helpers/tags.rb +5 -2
  58. data/lib/action_view/helpers/text_helper.rb +180 -97
  59. data/lib/action_view/helpers/translation_helper.rb +14 -45
  60. data/lib/action_view/helpers/url_helper.rb +230 -132
  61. data/lib/action_view/helpers.rb +27 -25
  62. data/lib/action_view/layouts.rb +15 -10
  63. data/lib/action_view/log_subscriber.rb +49 -32
  64. data/lib/action_view/lookup_context.rb +58 -61
  65. data/lib/action_view/model_naming.rb +2 -2
  66. data/lib/action_view/path_registry.rb +57 -0
  67. data/lib/action_view/path_set.rb +28 -35
  68. data/lib/action_view/railtie.rb +44 -9
  69. data/lib/action_view/record_identifier.rb +16 -9
  70. data/lib/action_view/render_parser.rb +188 -0
  71. data/lib/action_view/renderer/abstract_renderer.rb +3 -3
  72. data/lib/action_view/renderer/collection_renderer.rb +10 -2
  73. data/lib/action_view/renderer/partial_renderer/collection_caching.rb +21 -3
  74. data/lib/action_view/renderer/partial_renderer.rb +3 -36
  75. data/lib/action_view/renderer/renderer.rb +6 -4
  76. data/lib/action_view/renderer/streaming_template_renderer.rb +6 -5
  77. data/lib/action_view/renderer/template_renderer.rb +9 -4
  78. data/lib/action_view/rendering.rb +25 -7
  79. data/lib/action_view/ripper_ast_parser.rb +198 -0
  80. data/lib/action_view/routing_url_for.rb +8 -5
  81. data/lib/action_view/template/error.rb +122 -14
  82. data/lib/action_view/template/handlers/builder.rb +4 -4
  83. data/lib/action_view/template/handlers/erb/erubi.rb +23 -27
  84. data/lib/action_view/template/handlers/erb.rb +79 -1
  85. data/lib/action_view/template/handlers.rb +4 -4
  86. data/lib/action_view/template/html.rb +4 -4
  87. data/lib/action_view/template/inline.rb +3 -3
  88. data/lib/action_view/template/raw_file.rb +4 -4
  89. data/lib/action_view/template/renderable.rb +1 -1
  90. data/lib/action_view/template/resolver.rb +96 -313
  91. data/lib/action_view/template/text.rb +4 -4
  92. data/lib/action_view/template/types.rb +25 -32
  93. data/lib/action_view/template.rb +245 -41
  94. data/lib/action_view/template_details.rb +66 -0
  95. data/lib/action_view/template_path.rb +66 -0
  96. data/lib/action_view/test_case.rb +182 -23
  97. data/lib/action_view/testing/resolvers.rb +11 -12
  98. data/lib/action_view/unbound_template.rb +43 -7
  99. data/lib/action_view/version.rb +1 -1
  100. data/lib/action_view/view_paths.rb +19 -28
  101. data/lib/action_view.rb +6 -4
  102. data/lib/assets/compiled/rails-ujs.js +36 -5
  103. metadata +32 -25
@@ -5,17 +5,18 @@ require "active_support/core_ext/string/output_safety"
5
5
  require "active_support/number_helper"
6
6
 
7
7
  module ActionView
8
- # = Action View Number Helpers
9
- module Helpers #:nodoc:
8
+ module Helpers # :nodoc:
9
+ # = Action View Number \Helpers
10
+ #
10
11
  # Provides methods for converting numbers into formatted strings.
11
12
  # Methods are provided for phone numbers, currency, percentage,
12
- # precision, positional notation, file size and pretty printing.
13
+ # precision, positional notation, file size, and pretty printing.
13
14
  #
14
15
  # Most methods expect a +number+ argument, and will return it
15
16
  # unchanged if can't be converted into a valid number.
16
17
  module NumberHelper
17
18
  # Raised when argument +number+ param given to the helpers is invalid and
18
- # the option :raise is set to +true+.
19
+ # the option +:raise+ is set to +true+.
19
20
  class InvalidNumberError < StandardError
20
21
  attr_accessor :number
21
22
  def initialize(number)
@@ -23,42 +24,14 @@ module ActionView
23
24
  end
24
25
  end
25
26
 
26
- # Formats a +number+ into a phone number (US by default e.g., (555)
27
- # 123-9876). You can customize the format in the +options+ hash.
28
- #
29
- # ==== Options
30
- #
31
- # * <tt>:area_code</tt> - Adds parentheses around the area code.
32
- # * <tt>:delimiter</tt> - Specifies the delimiter to use
33
- # (defaults to "-").
34
- # * <tt>:extension</tt> - Specifies an extension to add to the
35
- # end of the generated number.
36
- # * <tt>:country_code</tt> - Sets the country code for the phone
37
- # number.
38
- # * <tt>:pattern</tt> - Specifies how the number is divided into three
39
- # groups with the custom regexp to override the default format.
40
- # * <tt>:raise</tt> - If true, raises +InvalidNumberError+ when
41
- # the argument is invalid.
42
- #
43
- # ==== Examples
27
+ # Delegates to ActiveSupport::NumberHelper#number_to_phone.
44
28
  #
45
- # number_to_phone(5551234) # => 555-1234
46
- # number_to_phone("5551234") # => 555-1234
47
- # number_to_phone(1235551234) # => 123-555-1234
48
- # number_to_phone(1235551234, area_code: true) # => (123) 555-1234
49
- # number_to_phone(1235551234, delimiter: " ") # => 123 555 1234
50
- # number_to_phone(1235551234, area_code: true, extension: 555) # => (123) 555-1234 x 555
51
- # number_to_phone(1235551234, country_code: 1) # => +1-123-555-1234
52
- # number_to_phone("123a456") # => 123a456
53
- # number_to_phone("1234a567", raise: true) # => InvalidNumberError
29
+ # Additionally, supports a +:raise+ option that will cause
30
+ # InvalidNumberError to be raised if +number+ is not a valid number:
54
31
  #
55
- # number_to_phone(1235551234, country_code: 1, extension: 1343, delimiter: ".")
56
- # # => +1.123.555.1234 x 1343
32
+ # number_to_phone("12x34") # => "12x34"
33
+ # number_to_phone("12x34", raise: true) # => InvalidNumberError
57
34
  #
58
- # number_to_phone(75561234567, pattern: /(\d{1,4})(\d{4})(\d{4})$/, area_code: true)
59
- # # => "(755) 6123-4567"
60
- # number_to_phone(13312345678, pattern: /(\d{3})(\d{4})(\d{4})$/)
61
- # # => "133-1234-5678"
62
35
  def number_to_phone(number, options = {})
63
36
  return unless number
64
37
  options = options.symbolize_keys
@@ -67,338 +40,73 @@ module ActionView
67
40
  ERB::Util.html_escape(ActiveSupport::NumberHelper.number_to_phone(number, options))
68
41
  end
69
42
 
70
- # Formats a +number+ into a currency string (e.g., $13.65). You
71
- # can customize the format in the +options+ hash.
72
- #
73
- # The currency unit and number formatting of the current locale will be used
74
- # unless otherwise specified in the provided options. No currency conversion
75
- # is performed. If the user is given a way to change their locale, they will
76
- # also be able to change the relative value of the currency displayed with
77
- # this helper. If your application will ever support multiple locales, you
78
- # may want to specify a constant <tt>:locale</tt> option or consider
79
- # using a library capable of currency conversion.
80
- #
81
- # ==== Options
82
- #
83
- # * <tt>:locale</tt> - Sets the locale to be used for formatting
84
- # (defaults to current locale).
85
- # * <tt>:precision</tt> - Sets the level of precision (defaults
86
- # to 2).
87
- # * <tt>:unit</tt> - Sets the denomination of the currency
88
- # (defaults to "$").
89
- # * <tt>:separator</tt> - Sets the separator between the units
90
- # (defaults to ".").
91
- # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
92
- # to ",").
93
- # * <tt>:format</tt> - Sets the format for non-negative numbers
94
- # (defaults to "%u%n"). Fields are <tt>%u</tt> for the
95
- # currency, and <tt>%n</tt> for the number.
96
- # * <tt>:negative_format</tt> - Sets the format for negative
97
- # numbers (defaults to prepending a hyphen to the formatted
98
- # number given by <tt>:format</tt>). Accepts the same fields
99
- # than <tt>:format</tt>, except <tt>%n</tt> is here the
100
- # absolute value of the number.
101
- # * <tt>:raise</tt> - If true, raises +InvalidNumberError+ when
102
- # the argument is invalid.
103
- # * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
104
- # insignificant zeros after the decimal separator (defaults to
105
- # +false+).
106
- #
107
- # ==== Examples
43
+ # Delegates to ActiveSupport::NumberHelper#number_to_currency.
108
44
  #
109
- # number_to_currency(1234567890.50) # => $1,234,567,890.50
110
- # number_to_currency(1234567890.506) # => $1,234,567,890.51
111
- # number_to_currency(1234567890.506, precision: 3) # => $1,234,567,890.506
112
- # number_to_currency(1234567890.506, locale: :fr) # => 1 234 567 890,51 €
113
- # number_to_currency("123a456") # => $123a456
45
+ # Additionally, supports a +:raise+ option that will cause
46
+ # InvalidNumberError to be raised if +number+ is not a valid number:
114
47
  #
115
- # number_to_currency("123a456", raise: true) # => InvalidNumberError
48
+ # number_to_currency("12x34") # => "$12x34"
49
+ # number_to_currency("12x34", raise: true) # => InvalidNumberError
116
50
  #
117
- # number_to_currency(-0.456789, precision: 0)
118
- # # => "$0"
119
- # number_to_currency(-1234567890.50, negative_format: "(%u%n)")
120
- # # => ($1,234,567,890.50)
121
- # number_to_currency(1234567890.50, unit: "R$", separator: ",", delimiter: "")
122
- # # => R$1234567890,50
123
- # number_to_currency(1234567890.50, unit: "R$", separator: ",", delimiter: "", format: "%n %u")
124
- # # => 1234567890,50 R$
125
- # number_to_currency(1234567890.50, strip_insignificant_zeros: true)
126
- # # => "$1,234,567,890.5"
127
51
  def number_to_currency(number, options = {})
128
52
  delegate_number_helper_method(:number_to_currency, number, options)
129
53
  end
130
54
 
131
- # Formats a +number+ as a percentage string (e.g., 65%). You can
132
- # customize the format in the +options+ hash.
55
+ # Delegates to ActiveSupport::NumberHelper#number_to_percentage.
133
56
  #
134
- # ==== Options
57
+ # Additionally, supports a +:raise+ option that will cause
58
+ # InvalidNumberError to be raised if +number+ is not a valid number:
135
59
  #
136
- # * <tt>:locale</tt> - Sets the locale to be used for formatting
137
- # (defaults to current locale).
138
- # * <tt>:precision</tt> - Sets the precision of the number
139
- # (defaults to 3).
140
- # * <tt>:significant</tt> - If +true+, precision will be the number
141
- # of significant_digits. If +false+, the number of fractional
142
- # digits (defaults to +false+).
143
- # * <tt>:separator</tt> - Sets the separator between the
144
- # fractional and integer digits (defaults to ".").
145
- # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
146
- # to "").
147
- # * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
148
- # insignificant zeros after the decimal separator (defaults to
149
- # +false+).
150
- # * <tt>:format</tt> - Specifies the format of the percentage
151
- # string The number field is <tt>%n</tt> (defaults to "%n%").
152
- # * <tt>:raise</tt> - If true, raises +InvalidNumberError+ when
153
- # the argument is invalid.
60
+ # number_to_percentage("99x") # => "99x%"
61
+ # number_to_percentage("99x", raise: true) # => InvalidNumberError
154
62
  #
155
- # ==== Examples
156
- #
157
- # number_to_percentage(100) # => 100.000%
158
- # number_to_percentage("98") # => 98.000%
159
- # number_to_percentage(100, precision: 0) # => 100%
160
- # number_to_percentage(1000, delimiter: '.', separator: ',') # => 1.000,000%
161
- # number_to_percentage(302.24398923423, precision: 5) # => 302.24399%
162
- # number_to_percentage(1000, locale: :fr) # => 1 000,000%
163
- # number_to_percentage("98a") # => 98a%
164
- # number_to_percentage(100, format: "%n %") # => 100.000 %
165
- #
166
- # number_to_percentage("98a", raise: true) # => InvalidNumberError
167
63
  def number_to_percentage(number, options = {})
168
64
  delegate_number_helper_method(:number_to_percentage, number, options)
169
65
  end
170
66
 
171
- # Formats a +number+ with grouped thousands using +delimiter+
172
- # (e.g., 12,324). You can customize the format in the +options+
173
- # hash.
174
- #
175
- # ==== Options
176
- #
177
- # * <tt>:locale</tt> - Sets the locale to be used for formatting
178
- # (defaults to current locale).
179
- # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
180
- # to ",").
181
- # * <tt>:separator</tt> - Sets the separator between the
182
- # fractional and integer digits (defaults to ".").
183
- # * <tt>:delimiter_pattern</tt> - Sets a custom regular expression used for
184
- # deriving the placement of delimiter. Helpful when using currency formats
185
- # like INR.
186
- # * <tt>:raise</tt> - If true, raises +InvalidNumberError+ when
187
- # the argument is invalid.
67
+ # Delegates to ActiveSupport::NumberHelper#number_to_delimited.
188
68
  #
189
- # ==== Examples
69
+ # Additionally, supports a +:raise+ option that will cause
70
+ # InvalidNumberError to be raised if +number+ is not a valid number:
190
71
  #
191
- # number_with_delimiter(12345678) # => 12,345,678
192
- # number_with_delimiter("123456") # => 123,456
193
- # number_with_delimiter(12345678.05) # => 12,345,678.05
194
- # number_with_delimiter(12345678, delimiter: ".") # => 12.345.678
195
- # number_with_delimiter(12345678, delimiter: ",") # => 12,345,678
196
- # number_with_delimiter(12345678.05, separator: " ") # => 12,345,678 05
197
- # number_with_delimiter(12345678.05, locale: :fr) # => 12 345 678,05
198
- # number_with_delimiter("112a") # => 112a
199
- # number_with_delimiter(98765432.98, delimiter: " ", separator: ",")
200
- # # => 98 765 432,98
72
+ # number_with_delimiter("12x34") # => "12x34"
73
+ # number_with_delimiter("12x34", raise: true) # => InvalidNumberError
201
74
  #
202
- # number_with_delimiter("123456.78",
203
- # delimiter_pattern: /(\d+?)(?=(\d\d)+(\d)(?!\d))/) # => "1,23,456.78"
204
- #
205
- # number_with_delimiter("112a", raise: true) # => raise InvalidNumberError
206
75
  def number_with_delimiter(number, options = {})
207
76
  delegate_number_helper_method(:number_to_delimited, number, options)
208
77
  end
209
78
 
210
- # Formats a +number+ with the specified level of
211
- # <tt>:precision</tt> (e.g., 112.32 has a precision of 2 if
212
- # +:significant+ is +false+, and 5 if +:significant+ is +true+).
213
- # You can customize the format in the +options+ hash.
214
- #
215
- # ==== Options
216
- #
217
- # * <tt>:locale</tt> - Sets the locale to be used for formatting
218
- # (defaults to current locale).
219
- # * <tt>:precision</tt> - Sets the precision of the number
220
- # (defaults to 3).
221
- # * <tt>:significant</tt> - If +true+, precision will be the number
222
- # of significant_digits. If +false+, the number of fractional
223
- # digits (defaults to +false+).
224
- # * <tt>:separator</tt> - Sets the separator between the
225
- # fractional and integer digits (defaults to ".").
226
- # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
227
- # to "").
228
- # * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
229
- # insignificant zeros after the decimal separator (defaults to
230
- # +false+).
231
- # * <tt>:raise</tt> - If true, raises +InvalidNumberError+ when
232
- # the argument is invalid.
79
+ # Delegates to ActiveSupport::NumberHelper#number_to_rounded.
233
80
  #
234
- # ==== Examples
81
+ # Additionally, supports a +:raise+ option that will cause
82
+ # InvalidNumberError to be raised if +number+ is not a valid number:
235
83
  #
236
- # number_with_precision(111.2345) # => 111.235
237
- # number_with_precision(111.2345, precision: 2) # => 111.23
238
- # number_with_precision(13, precision: 5) # => 13.00000
239
- # number_with_precision(389.32314, precision: 0) # => 389
240
- # number_with_precision(111.2345, significant: true) # => 111
241
- # number_with_precision(111.2345, precision: 1, significant: true) # => 100
242
- # number_with_precision(13, precision: 5, significant: true) # => 13.000
243
- # number_with_precision(111.234, locale: :fr) # => 111,234
84
+ # number_with_precision("12x34") # => "12x34"
85
+ # number_with_precision("12x34", raise: true) # => InvalidNumberError
244
86
  #
245
- # number_with_precision(13, precision: 5, significant: true, strip_insignificant_zeros: true)
246
- # # => 13
247
- #
248
- # number_with_precision(389.32314, precision: 4, significant: true) # => 389.3
249
- # number_with_precision(1111.2345, precision: 2, separator: ',', delimiter: '.')
250
- # # => 1.111,23
251
87
  def number_with_precision(number, options = {})
252
88
  delegate_number_helper_method(:number_to_rounded, number, options)
253
89
  end
254
90
 
255
- # Formats the bytes in +number+ into a more understandable
256
- # representation (e.g., giving it 1500 yields 1.46 KB). This
257
- # method is useful for reporting file sizes to users. You can
258
- # customize the format in the +options+ hash.
259
- #
260
- # See <tt>number_to_human</tt> if you want to pretty-print a
261
- # generic number.
91
+ # Delegates to ActiveSupport::NumberHelper#number_to_human_size.
262
92
  #
263
- # ==== Options
93
+ # Additionally, supports a +:raise+ option that will cause
94
+ # InvalidNumberError to be raised if +number+ is not a valid number:
264
95
  #
265
- # * <tt>:locale</tt> - Sets the locale to be used for formatting
266
- # (defaults to current locale).
267
- # * <tt>:precision</tt> - Sets the precision of the number
268
- # (defaults to 3).
269
- # * <tt>:significant</tt> - If +true+, precision will be the number
270
- # of significant_digits. If +false+, the number of fractional
271
- # digits (defaults to +true+)
272
- # * <tt>:separator</tt> - Sets the separator between the
273
- # fractional and integer digits (defaults to ".").
274
- # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
275
- # to "").
276
- # * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
277
- # insignificant zeros after the decimal separator (defaults to
278
- # +true+)
279
- # * <tt>:raise</tt> - If true, raises +InvalidNumberError+ when
280
- # the argument is invalid.
96
+ # number_to_human_size("12x34") # => "12x34"
97
+ # number_to_human_size("12x34", raise: true) # => InvalidNumberError
281
98
  #
282
- # ==== Examples
283
- #
284
- # number_to_human_size(123) # => 123 Bytes
285
- # number_to_human_size(1234) # => 1.21 KB
286
- # number_to_human_size(12345) # => 12.1 KB
287
- # number_to_human_size(1234567) # => 1.18 MB
288
- # number_to_human_size(1234567890) # => 1.15 GB
289
- # number_to_human_size(1234567890123) # => 1.12 TB
290
- # number_to_human_size(1234567890123456) # => 1.1 PB
291
- # number_to_human_size(1234567890123456789) # => 1.07 EB
292
- # number_to_human_size(1234567, precision: 2) # => 1.2 MB
293
- # number_to_human_size(483989, precision: 2) # => 470 KB
294
- # number_to_human_size(1234567, precision: 2, separator: ',') # => 1,2 MB
295
- # number_to_human_size(1234567890123, precision: 5) # => "1.1228 TB"
296
- # number_to_human_size(524288000, precision: 5) # => "500 MB"
297
99
  def number_to_human_size(number, options = {})
298
100
  delegate_number_helper_method(:number_to_human_size, number, options)
299
101
  end
300
102
 
301
- # Pretty prints (formats and approximates) a number in a way it
302
- # is more readable by humans (e.g.: 1200000000 becomes "1.2
303
- # Billion"). This is useful for numbers that can get very large
304
- # (and too hard to read).
305
- #
306
- # See <tt>number_to_human_size</tt> if you want to print a file
307
- # size.
308
- #
309
- # You can also define your own unit-quantifier names if you want
310
- # to use other decimal units (e.g.: 1500 becomes "1.5
311
- # kilometers", 0.150 becomes "150 milliliters", etc). You may
312
- # define a wide range of unit quantifiers, even fractional ones
313
- # (centi, deci, mili, etc).
314
- #
315
- # ==== Options
316
- #
317
- # * <tt>:locale</tt> - Sets the locale to be used for formatting
318
- # (defaults to current locale).
319
- # * <tt>:precision</tt> - Sets the precision of the number
320
- # (defaults to 3).
321
- # * <tt>:significant</tt> - If +true+, precision will be the number
322
- # of significant_digits. If +false+, the number of fractional
323
- # digits (defaults to +true+)
324
- # * <tt>:separator</tt> - Sets the separator between the
325
- # fractional and integer digits (defaults to ".").
326
- # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
327
- # to "").
328
- # * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
329
- # insignificant zeros after the decimal separator (defaults to
330
- # +true+)
331
- # * <tt>:units</tt> - A Hash of unit quantifier names. Or a
332
- # string containing an i18n scope where to find this hash. It
333
- # might have the following keys:
334
- # * *integers*: <tt>:unit</tt>, <tt>:ten</tt>,
335
- # <tt>:hundred</tt>, <tt>:thousand</tt>, <tt>:million</tt>,
336
- # <tt>:billion</tt>, <tt>:trillion</tt>,
337
- # <tt>:quadrillion</tt>
338
- # * *fractionals*: <tt>:deci</tt>, <tt>:centi</tt>,
339
- # <tt>:mili</tt>, <tt>:micro</tt>, <tt>:nano</tt>,
340
- # <tt>:pico</tt>, <tt>:femto</tt>
341
- # * <tt>:format</tt> - Sets the format of the output string
342
- # (defaults to "%n %u"). The field types are:
343
- # * %u - The quantifier (ex.: 'thousand')
344
- # * %n - The number
345
- # * <tt>:raise</tt> - If true, raises +InvalidNumberError+ when
346
- # the argument is invalid.
347
- #
348
- # ==== Examples
349
- #
350
- # number_to_human(123) # => "123"
351
- # number_to_human(1234) # => "1.23 Thousand"
352
- # number_to_human(12345) # => "12.3 Thousand"
353
- # number_to_human(1234567) # => "1.23 Million"
354
- # number_to_human(1234567890) # => "1.23 Billion"
355
- # number_to_human(1234567890123) # => "1.23 Trillion"
356
- # number_to_human(1234567890123456) # => "1.23 Quadrillion"
357
- # number_to_human(1234567890123456789) # => "1230 Quadrillion"
358
- # number_to_human(489939, precision: 2) # => "490 Thousand"
359
- # number_to_human(489939, precision: 4) # => "489.9 Thousand"
360
- # number_to_human(1234567, precision: 4,
361
- # significant: false) # => "1.2346 Million"
362
- # number_to_human(1234567, precision: 1,
363
- # separator: ',',
364
- # significant: false) # => "1,2 Million"
365
- #
366
- # number_to_human(500000000, precision: 5) # => "500 Million"
367
- # number_to_human(12345012345, significant: false) # => "12.345 Billion"
368
- #
369
- # Non-significant zeros after the decimal separator are stripped
370
- # out by default (set <tt>:strip_insignificant_zeros</tt> to
371
- # +false+ to change that):
372
- #
373
- # number_to_human(12.00001) # => "12"
374
- # number_to_human(12.00001, strip_insignificant_zeros: false) # => "12.0"
375
- #
376
- # ==== Custom Unit Quantifiers
377
- #
378
- # You can also use your own custom unit quantifiers:
379
- # number_to_human(500000, units: {unit: "ml", thousand: "lt"}) # => "500 lt"
380
- #
381
- # If in your I18n locale you have:
382
- # distance:
383
- # centi:
384
- # one: "centimeter"
385
- # other: "centimeters"
386
- # unit:
387
- # one: "meter"
388
- # other: "meters"
389
- # thousand:
390
- # one: "kilometer"
391
- # other: "kilometers"
392
- # billion: "gazillion-distance"
103
+ # Delegates to ActiveSupport::NumberHelper#number_to_human.
393
104
  #
394
- # 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:
395
107
  #
396
- # number_to_human(543934, units: :distance) # => "544 kilometers"
397
- # number_to_human(54393498, units: :distance) # => "54400 kilometers"
398
- # number_to_human(54393498000, units: :distance) # => "54.4 gazillion-distance"
399
- # number_to_human(343, units: :distance, precision: 1) # => "300 meters"
400
- # number_to_human(1, units: :distance) # => "1 meter"
401
- # number_to_human(0.34, units: :distance) # => "34 centimeters"
108
+ # number_to_human("12x34") # => "12x34"
109
+ # number_to_human("12x34", raise: true) # => InvalidNumberError
402
110
  #
403
111
  def number_to_human(number, options = {})
404
112
  delegate_number_helper_method(:number_to_human, number, options)
@@ -448,9 +156,9 @@ module ActionView
448
156
  end
449
157
 
450
158
  def parse_float(number, raise_error)
451
- Float(number)
452
- rescue ArgumentError, TypeError
453
- raise InvalidNumberError, number if raise_error
159
+ result = Float(number, exception: false)
160
+ raise InvalidNumberError, number if result.nil? && raise_error
161
+ result
454
162
  end
455
163
  end
456
164
  end
@@ -2,19 +2,19 @@
2
2
 
3
3
  require "active_support/core_ext/string/output_safety"
4
4
 
5
- module ActionView #:nodoc:
6
- # = Action View Raw Output Helper
7
- module Helpers #:nodoc:
5
+ module ActionView # :nodoc:
6
+ module Helpers # :nodoc:
7
+ # = Action View Raw Output \Helpers
8
8
  module OutputSafetyHelper
9
9
  # This method outputs without escaping a string. Since escaping tags is
10
- # now default, this can be used when you don't want Rails to automatically
10
+ # now default, this can be used when you don't want \Rails to automatically
11
11
  # escape tags. This is not recommended if the data is coming from the user's
12
12
  # input.
13
13
  #
14
14
  # For example:
15
15
  #
16
- # raw @user.name
17
- # # => 'Jimmy <alert>Tables</alert>'
16
+ # raw @user.name
17
+ # # => 'Jimmy <alert>Tables</alert>'
18
18
  def raw(stringish)
19
19
  stringish.to_s.html_safe
20
20
  end
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActionView
4
- module Helpers #:nodoc:
5
- # = Action View Rendering
4
+ module Helpers # :nodoc:
5
+ # = Action View \Rendering \Helpers
6
6
  #
7
7
  # Implements methods that allow rendering from a view context.
8
8
  # In order to use this module, all you need is to implement
@@ -10,8 +10,8 @@ module ActionView
10
10
  module RenderingHelper
11
11
  # Returns the result of a render that's dictated by the options hash. The primary options are:
12
12
  #
13
- # * <tt>:partial</tt> - See <tt>ActionView::PartialRenderer</tt>.
14
- # * <tt>:file</tt> - Renders an explicit template file (this used to be the old default), add :locals to pass in those.
13
+ # * <tt>:partial</tt> - See ActionView::PartialRenderer.
14
+ # * <tt>:file</tt> - Renders an explicit template file (this used to be the old default), add +:locals+ to pass in those.
15
15
  # * <tt>:inline</tt> - Renders an inline template similar to how it's done in the controller.
16
16
  # * <tt>:plain</tt> - Renders the text passed in out. Setting the content
17
17
  # type as <tt>text/plain</tt>.
@@ -19,8 +19,7 @@ module ActionView
19
19
  # performs HTML escape on the string first. Setting the content type as
20
20
  # <tt>text/html</tt>.
21
21
  # * <tt>:body</tt> - Renders the text passed in, and inherits the content
22
- # type of <tt>text/plain</tt> from <tt>ActionDispatch::Response</tt>
23
- # object.
22
+ # type of <tt>text/plain</tt> from ActionDispatch::Response object.
24
23
  #
25
24
  # If no <tt>options</tt> hash is passed or if <tt>:update</tt> is specified, then:
26
25
  #
@@ -47,7 +46,7 @@ module ActionView
47
46
  end
48
47
  end
49
48
 
50
- # Overwrites _layout_for in the context object so it supports the case a block is
49
+ # Overrides _layout_for in the context object so it supports the case a block is
51
50
  # passed to a partial. Returns the contents that are yielded to a layout, given a
52
51
  # name or a block.
53
52
  #