activesupport 5.1.7 → 5.2.4.3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activesupport might be problematic. Click here for more details.

Files changed (242) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +401 -541
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +3 -3
  5. data/lib/active_support.rb +5 -13
  6. data/lib/active_support/all.rb +2 -0
  7. data/lib/active_support/array_inquirer.rb +2 -0
  8. data/lib/active_support/backtrace_cleaner.rb +2 -0
  9. data/lib/active_support/benchmarkable.rb +2 -0
  10. data/lib/active_support/builder.rb +2 -0
  11. data/lib/active_support/cache.rb +197 -83
  12. data/lib/active_support/cache/file_store.rb +5 -4
  13. data/lib/active_support/cache/mem_cache_store.rb +39 -39
  14. data/lib/active_support/cache/memory_store.rb +2 -0
  15. data/lib/active_support/cache/null_store.rb +2 -0
  16. data/lib/active_support/cache/redis_cache_store.rb +466 -0
  17. data/lib/active_support/cache/strategy/local_cache.rb +33 -2
  18. data/lib/active_support/cache/strategy/local_cache_middleware.rb +2 -0
  19. data/lib/active_support/callbacks.rb +28 -39
  20. data/lib/active_support/concern.rb +10 -4
  21. data/lib/active_support/concurrency/share_lock.rb +2 -0
  22. data/lib/active_support/configurable.rb +2 -0
  23. data/lib/active_support/core_ext.rb +3 -1
  24. data/lib/active_support/core_ext/array.rb +2 -0
  25. data/lib/active_support/core_ext/array/access.rb +4 -2
  26. data/lib/active_support/core_ext/array/conversions.rb +2 -0
  27. data/lib/active_support/core_ext/array/extract_options.rb +2 -0
  28. data/lib/active_support/core_ext/array/grouping.rb +2 -0
  29. data/lib/active_support/core_ext/array/inquiry.rb +2 -0
  30. data/lib/active_support/core_ext/array/prepend_and_append.rb +4 -2
  31. data/lib/active_support/core_ext/array/wrap.rb +2 -0
  32. data/lib/active_support/core_ext/benchmark.rb +2 -0
  33. data/lib/active_support/core_ext/big_decimal.rb +2 -0
  34. data/lib/active_support/core_ext/big_decimal/conversions.rb +2 -0
  35. data/lib/active_support/core_ext/class.rb +2 -0
  36. data/lib/active_support/core_ext/class/attribute.rb +34 -16
  37. data/lib/active_support/core_ext/class/attribute_accessors.rb +2 -0
  38. data/lib/active_support/core_ext/class/subclasses.rb +1 -2
  39. data/lib/active_support/core_ext/date.rb +2 -0
  40. data/lib/active_support/core_ext/date/acts_like.rb +2 -0
  41. data/lib/active_support/core_ext/date/blank.rb +2 -0
  42. data/lib/active_support/core_ext/date/calculations.rb +2 -0
  43. data/lib/active_support/core_ext/date/conversions.rb +10 -9
  44. data/lib/active_support/core_ext/date/zones.rb +2 -0
  45. data/lib/active_support/core_ext/date_and_time/calculations.rb +50 -16
  46. data/lib/active_support/core_ext/date_and_time/compatibility.rb +3 -1
  47. data/lib/active_support/core_ext/date_and_time/zones.rb +2 -0
  48. data/lib/active_support/core_ext/date_time.rb +2 -0
  49. data/lib/active_support/core_ext/date_time/acts_like.rb +2 -0
  50. data/lib/active_support/core_ext/date_time/blank.rb +2 -0
  51. data/lib/active_support/core_ext/date_time/calculations.rb +2 -0
  52. data/lib/active_support/core_ext/date_time/compatibility.rb +7 -5
  53. data/lib/active_support/core_ext/date_time/conversions.rb +2 -0
  54. data/lib/active_support/core_ext/digest.rb +3 -0
  55. data/lib/active_support/core_ext/digest/uuid.rb +3 -1
  56. data/lib/active_support/core_ext/enumerable.rb +8 -1
  57. data/lib/active_support/core_ext/file.rb +2 -0
  58. data/lib/active_support/core_ext/file/atomic.rb +3 -1
  59. data/lib/active_support/core_ext/hash.rb +2 -0
  60. data/lib/active_support/core_ext/hash/compact.rb +2 -0
  61. data/lib/active_support/core_ext/hash/conversions.rb +4 -2
  62. data/lib/active_support/core_ext/hash/deep_merge.rb +8 -12
  63. data/lib/active_support/core_ext/hash/except.rb +2 -0
  64. data/lib/active_support/core_ext/hash/indifferent_access.rb +2 -0
  65. data/lib/active_support/core_ext/hash/keys.rb +2 -0
  66. data/lib/active_support/core_ext/hash/reverse_merge.rb +5 -2
  67. data/lib/active_support/core_ext/hash/slice.rb +4 -4
  68. data/lib/active_support/core_ext/hash/transform_values.rb +2 -0
  69. data/lib/active_support/core_ext/integer.rb +2 -0
  70. data/lib/active_support/core_ext/integer/inflections.rb +2 -0
  71. data/lib/active_support/core_ext/integer/multiple.rb +2 -0
  72. data/lib/active_support/core_ext/integer/time.rb +7 -14
  73. data/lib/active_support/core_ext/kernel.rb +2 -0
  74. data/lib/active_support/core_ext/kernel/agnostics.rb +2 -0
  75. data/lib/active_support/core_ext/kernel/concern.rb +2 -0
  76. data/lib/active_support/core_ext/kernel/reporting.rb +2 -0
  77. data/lib/active_support/core_ext/kernel/singleton_class.rb +2 -0
  78. data/lib/active_support/core_ext/load_error.rb +2 -7
  79. data/lib/active_support/core_ext/marshal.rb +2 -0
  80. data/lib/active_support/core_ext/module.rb +3 -0
  81. data/lib/active_support/core_ext/module/aliasing.rb +2 -0
  82. data/lib/active_support/core_ext/module/anonymous.rb +2 -0
  83. data/lib/active_support/core_ext/module/attr_internal.rb +2 -0
  84. data/lib/active_support/core_ext/module/attribute_accessors.rb +21 -24
  85. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +2 -0
  86. data/lib/active_support/core_ext/module/concerning.rb +7 -8
  87. data/lib/active_support/core_ext/module/delegation.rb +31 -29
  88. data/lib/active_support/core_ext/module/deprecation.rb +2 -0
  89. data/lib/active_support/core_ext/module/introspection.rb +2 -0
  90. data/lib/active_support/core_ext/module/reachable.rb +3 -0
  91. data/lib/active_support/core_ext/module/redefine_method.rb +49 -0
  92. data/lib/active_support/core_ext/module/remove_method.rb +5 -23
  93. data/lib/active_support/core_ext/name_error.rb +7 -0
  94. data/lib/active_support/core_ext/numeric.rb +2 -0
  95. data/lib/active_support/core_ext/numeric/bytes.rb +2 -0
  96. data/lib/active_support/core_ext/numeric/conversions.rb +9 -7
  97. data/lib/active_support/core_ext/numeric/inquiry.rb +2 -0
  98. data/lib/active_support/core_ext/numeric/time.rb +7 -15
  99. data/lib/active_support/core_ext/object.rb +2 -0
  100. data/lib/active_support/core_ext/object/acts_like.rb +12 -1
  101. data/lib/active_support/core_ext/object/blank.rb +12 -1
  102. data/lib/active_support/core_ext/object/conversions.rb +2 -0
  103. data/lib/active_support/core_ext/object/deep_dup.rb +2 -0
  104. data/lib/active_support/core_ext/object/duplicable.rb +10 -8
  105. data/lib/active_support/core_ext/object/inclusion.rb +2 -0
  106. data/lib/active_support/core_ext/object/instance_variables.rb +2 -0
  107. data/lib/active_support/core_ext/object/json.rb +8 -0
  108. data/lib/active_support/core_ext/object/to_param.rb +2 -0
  109. data/lib/active_support/core_ext/object/to_query.rb +2 -0
  110. data/lib/active_support/core_ext/object/try.rb +2 -0
  111. data/lib/active_support/core_ext/object/with_options.rb +3 -1
  112. data/lib/active_support/core_ext/range.rb +4 -1
  113. data/lib/active_support/core_ext/range/compare_range.rb +61 -0
  114. data/lib/active_support/core_ext/range/conversions.rb +9 -1
  115. data/lib/active_support/core_ext/range/each.rb +5 -1
  116. data/lib/active_support/core_ext/range/include_range.rb +2 -22
  117. data/lib/active_support/core_ext/range/include_time_with_zone.rb +23 -0
  118. data/lib/active_support/core_ext/range/overlaps.rb +2 -0
  119. data/lib/active_support/core_ext/regexp.rb +2 -0
  120. data/lib/active_support/core_ext/securerandom.rb +2 -0
  121. data/lib/active_support/core_ext/string.rb +2 -0
  122. data/lib/active_support/core_ext/string/access.rb +2 -0
  123. data/lib/active_support/core_ext/string/behavior.rb +2 -0
  124. data/lib/active_support/core_ext/string/conversions.rb +2 -0
  125. data/lib/active_support/core_ext/string/exclude.rb +2 -0
  126. data/lib/active_support/core_ext/string/filters.rb +2 -0
  127. data/lib/active_support/core_ext/string/indent.rb +2 -0
  128. data/lib/active_support/core_ext/string/inflections.rb +26 -12
  129. data/lib/active_support/core_ext/string/inquiry.rb +2 -0
  130. data/lib/active_support/core_ext/string/multibyte.rb +4 -0
  131. data/lib/active_support/core_ext/string/output_safety.rb +6 -7
  132. data/lib/active_support/core_ext/string/starts_ends_with.rb +2 -0
  133. data/lib/active_support/core_ext/string/strip.rb +2 -0
  134. data/lib/active_support/core_ext/string/zones.rb +2 -0
  135. data/lib/active_support/core_ext/time.rb +2 -0
  136. data/lib/active_support/core_ext/time/acts_like.rb +2 -0
  137. data/lib/active_support/core_ext/time/calculations.rb +23 -15
  138. data/lib/active_support/core_ext/time/compatibility.rb +4 -2
  139. data/lib/active_support/core_ext/time/conversions.rb +2 -0
  140. data/lib/active_support/core_ext/time/zones.rb +6 -4
  141. data/lib/active_support/core_ext/uri.rb +6 -6
  142. data/lib/active_support/current_attributes.rb +195 -0
  143. data/lib/active_support/dependencies.rb +25 -26
  144. data/lib/active_support/dependencies/autoload.rb +2 -0
  145. data/lib/active_support/dependencies/interlock.rb +2 -0
  146. data/lib/active_support/deprecation.rb +4 -2
  147. data/lib/active_support/deprecation/behaviors.rb +28 -9
  148. data/lib/active_support/deprecation/constant_accessor.rb +4 -2
  149. data/lib/active_support/deprecation/instance_delegator.rb +2 -0
  150. data/lib/active_support/deprecation/method_wrappers.rb +30 -17
  151. data/lib/active_support/deprecation/proxy_wrappers.rb +5 -2
  152. data/lib/active_support/deprecation/reporting.rb +5 -3
  153. data/lib/active_support/descendants_tracker.rb +2 -0
  154. data/lib/active_support/digest.rb +20 -0
  155. data/lib/active_support/duration.rb +11 -7
  156. data/lib/active_support/duration/iso8601_parser.rb +4 -2
  157. data/lib/active_support/duration/iso8601_serializer.rb +4 -2
  158. data/lib/active_support/encrypted_configuration.rb +49 -0
  159. data/lib/active_support/encrypted_file.rb +99 -0
  160. data/lib/active_support/evented_file_update_checker.rb +2 -0
  161. data/lib/active_support/execution_wrapper.rb +2 -0
  162. data/lib/active_support/executor.rb +2 -0
  163. data/lib/active_support/file_update_checker.rb +2 -0
  164. data/lib/active_support/gem_version.rb +5 -3
  165. data/lib/active_support/gzip.rb +2 -0
  166. data/lib/active_support/hash_with_indifferent_access.rb +55 -1
  167. data/lib/active_support/i18n.rb +3 -1
  168. data/lib/active_support/i18n_railtie.rb +4 -6
  169. data/lib/active_support/inflections.rb +2 -0
  170. data/lib/active_support/inflector.rb +2 -0
  171. data/lib/active_support/inflector/inflections.rb +20 -4
  172. data/lib/active_support/inflector/methods.rb +43 -24
  173. data/lib/active_support/inflector/transliterate.rb +17 -8
  174. data/lib/active_support/json.rb +2 -0
  175. data/lib/active_support/json/decoding.rb +2 -0
  176. data/lib/active_support/json/encoding.rb +2 -0
  177. data/lib/active_support/key_generator.rb +3 -1
  178. data/lib/active_support/lazy_load_hooks.rb +2 -0
  179. data/lib/active_support/log_subscriber.rb +3 -2
  180. data/lib/active_support/log_subscriber/test_helper.rb +2 -0
  181. data/lib/active_support/logger.rb +2 -0
  182. data/lib/active_support/logger_silence.rb +3 -2
  183. data/lib/active_support/logger_thread_safe_level.rb +4 -1
  184. data/lib/active_support/message_encryptor.rb +95 -22
  185. data/lib/active_support/message_verifier.rb +78 -7
  186. data/lib/active_support/messages/metadata.rb +71 -0
  187. data/lib/active_support/messages/rotation_configuration.rb +22 -0
  188. data/lib/active_support/messages/rotator.rb +56 -0
  189. data/lib/active_support/multibyte.rb +2 -0
  190. data/lib/active_support/multibyte/chars.rb +2 -0
  191. data/lib/active_support/multibyte/unicode.rb +4 -2
  192. data/lib/active_support/notifications.rb +2 -0
  193. data/lib/active_support/notifications/fanout.rb +4 -2
  194. data/lib/active_support/notifications/instrumenter.rb +2 -0
  195. data/lib/active_support/number_helper.rb +2 -0
  196. data/lib/active_support/number_helper/number_converter.rb +2 -0
  197. data/lib/active_support/number_helper/number_to_currency_converter.rb +2 -0
  198. data/lib/active_support/number_helper/number_to_delimited_converter.rb +2 -0
  199. data/lib/active_support/number_helper/number_to_human_converter.rb +2 -0
  200. data/lib/active_support/number_helper/number_to_human_size_converter.rb +2 -0
  201. data/lib/active_support/number_helper/number_to_percentage_converter.rb +2 -0
  202. data/lib/active_support/number_helper/number_to_phone_converter.rb +3 -1
  203. data/lib/active_support/number_helper/number_to_rounded_converter.rb +2 -20
  204. data/lib/active_support/number_helper/rounding_helper.rb +6 -4
  205. data/lib/active_support/option_merger.rb +2 -0
  206. data/lib/active_support/ordered_hash.rb +2 -0
  207. data/lib/active_support/ordered_options.rb +5 -3
  208. data/lib/active_support/per_thread_registry.rb +2 -0
  209. data/lib/active_support/proxy_object.rb +2 -0
  210. data/lib/active_support/rails.rb +2 -0
  211. data/lib/active_support/railtie.rb +37 -8
  212. data/lib/active_support/reloader.rb +7 -5
  213. data/lib/active_support/rescuable.rb +3 -2
  214. data/lib/active_support/security_utils.rb +15 -11
  215. data/lib/active_support/string_inquirer.rb +2 -0
  216. data/lib/active_support/subscriber.rb +8 -2
  217. data/lib/active_support/tagged_logging.rb +2 -0
  218. data/lib/active_support/test_case.rb +3 -2
  219. data/lib/active_support/testing/assertions.rb +31 -14
  220. data/lib/active_support/testing/autorun.rb +2 -0
  221. data/lib/active_support/testing/constant_lookup.rb +2 -0
  222. data/lib/active_support/testing/declarative.rb +2 -0
  223. data/lib/active_support/testing/deprecation.rb +2 -0
  224. data/lib/active_support/testing/file_fixtures.rb +2 -0
  225. data/lib/active_support/testing/isolation.rb +3 -1
  226. data/lib/active_support/testing/method_call_assertions.rb +2 -0
  227. data/lib/active_support/testing/setup_and_teardown.rb +12 -7
  228. data/lib/active_support/testing/stream.rb +2 -0
  229. data/lib/active_support/testing/tagged_logging.rb +2 -0
  230. data/lib/active_support/testing/time_helpers.rb +33 -3
  231. data/lib/active_support/time.rb +2 -0
  232. data/lib/active_support/time_with_zone.rb +38 -0
  233. data/lib/active_support/values/time_zone.rb +20 -8
  234. data/lib/active_support/version.rb +2 -0
  235. data/lib/active_support/xml_mini.rb +4 -2
  236. data/lib/active_support/xml_mini/jdom.rb +4 -2
  237. data/lib/active_support/xml_mini/libxml.rb +3 -1
  238. data/lib/active_support/xml_mini/libxmlsax.rb +4 -2
  239. data/lib/active_support/xml_mini/nokogiri.rb +3 -1
  240. data/lib/active_support/xml_mini/nokogirisax.rb +3 -1
  241. data/lib/active_support/xml_mini/rexml.rb +3 -1
  242. metadata +17 -5
@@ -1,6 +1,14 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveSupport::RangeWithFormat
2
4
  RANGE_FORMATS = {
3
- db: Proc.new { |start, stop| "BETWEEN '#{start.to_s(:db)}' AND '#{stop.to_s(:db)}'" }
5
+ db: -> (start, stop) do
6
+ case start
7
+ when String then "BETWEEN '#{start}' AND '#{stop}'"
8
+ else
9
+ "BETWEEN '#{start.to_s(:db)}' AND '#{stop.to_s(:db)}'"
10
+ end
11
+ end
4
12
  }
5
13
 
6
14
  # Convert range to a formatted string. See RANGE_FORMATS for predefined formats.
@@ -1,3 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/time_with_zone"
4
+
1
5
  module ActiveSupport
2
6
  module EachTimeWithZone #:nodoc:
3
7
  def each(&block)
@@ -13,7 +17,7 @@ module ActiveSupport
13
17
  private
14
18
 
15
19
  def ensure_iteration_allowed
16
- raise TypeError, "can't iterate from #{first.class}" if first.is_a?(Time)
20
+ raise TypeError, "can't iterate from #{first.class}" if first.is_a?(TimeWithZone)
17
21
  end
18
22
  end
19
23
  end
@@ -1,23 +1,3 @@
1
- module ActiveSupport
2
- module IncludeWithRange #:nodoc:
3
- # Extends the default Range#include? to support range comparisons.
4
- # (1..5).include?(1..5) # => true
5
- # (1..5).include?(2..3) # => true
6
- # (1..5).include?(2..6) # => false
7
- #
8
- # The native Range#include? behavior is untouched.
9
- # ('a'..'f').include?('c') # => true
10
- # (5..9).include?(11) # => false
11
- def include?(value)
12
- if value.is_a?(::Range)
13
- # 1...10 includes 1..9 but it does not include 1..10.
14
- operator = exclude_end? && !value.exclude_end? ? :< : :<=
15
- super(value.first) && value.last.send(operator, last)
16
- else
17
- super
18
- end
19
- end
20
- end
21
- end
1
+ # frozen_string_literal: true
22
2
 
23
- Range.prepend(ActiveSupport::IncludeWithRange)
3
+ require "active_support/core_ext/range/compare_range"
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/time_with_zone"
4
+
5
+ module ActiveSupport
6
+ module IncludeTimeWithZone #:nodoc:
7
+ # Extends the default Range#include? to support ActiveSupport::TimeWithZone.
8
+ #
9
+ # (1.hour.ago..1.hour.from_now).include?(Time.current) # => true
10
+ #
11
+ def include?(value)
12
+ if first.is_a?(TimeWithZone)
13
+ cover?(value)
14
+ elsif last.is_a?(TimeWithZone)
15
+ cover?(value)
16
+ else
17
+ super
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ Range.prepend(ActiveSupport::IncludeTimeWithZone)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Range
2
4
  # Compare two ranges and see if they overlap each other
3
5
  # (1..5).overlaps?(4..6) # => true
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Regexp #:nodoc:
2
4
  def multiline?
3
5
  options & MULTILINE == MULTILINE
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "securerandom"
2
4
 
3
5
  module SecureRandom
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/core_ext/string/conversions"
2
4
  require "active_support/core_ext/string/filters"
3
5
  require "active_support/core_ext/string/multibyte"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class String
2
4
  # If you pass a single integer, returns a substring of one character at that
3
5
  # position. The first character of the string is at position 0, the next at
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class String
2
4
  # Enables more predictable duck-typing on String-like classes. See <tt>Object#acts_like?</tt>.
3
5
  def acts_like_string?
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "date"
2
4
  require "active_support/core_ext/time/calculations"
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class String
2
4
  # The inverse of <tt>String#include?</tt>. Returns true if the string
3
5
  # does not include the other string.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class String
2
4
  # Returns the string, first removing all whitespace on both ends of
3
5
  # the string, and then changing remaining consecutive whitespace
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class String
2
4
  # Same as +indent+, except it indents the receiver in-place.
3
5
  #
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/inflector/methods"
2
4
  require "active_support/inflector/transliterate"
3
5
 
@@ -92,6 +94,8 @@ class String
92
94
  ActiveSupport::Inflector.camelize(self, true)
93
95
  when :lower
94
96
  ActiveSupport::Inflector.camelize(self, false)
97
+ else
98
+ raise ArgumentError, "Invalid option, use either :upper or :lower."
95
99
  end
96
100
  end
97
101
  alias_method :camelcase, :camelize
@@ -100,12 +104,17 @@ class String
100
104
  # a nicer looking title. +titleize+ is meant for creating pretty output. It is not
101
105
  # used in the Rails internals.
102
106
  #
107
+ # The trailing '_id','Id'.. can be kept and capitalized by setting the
108
+ # optional parameter +keep_id_suffix+ to true.
109
+ # By default, this parameter is false.
110
+ #
103
111
  # +titleize+ is also aliased as +titlecase+.
104
112
  #
105
- # 'man from the boondocks'.titleize # => "Man From The Boondocks"
106
- # 'x-men: the last stand'.titleize # => "X Men: The Last Stand"
107
- def titleize
108
- ActiveSupport::Inflector.titleize(self)
113
+ # 'man from the boondocks'.titleize # => "Man From The Boondocks"
114
+ # 'x-men: the last stand'.titleize # => "X Men: The Last Stand"
115
+ # 'string_ending_with_id'.titleize(keep_id_suffix: true) # => "String Ending With Id"
116
+ def titleize(keep_id_suffix: false)
117
+ ActiveSupport::Inflector.titleize(self, keep_id_suffix: keep_id_suffix)
109
118
  end
110
119
  alias_method :titlecase, :titleize
111
120
 
@@ -165,7 +174,7 @@ class String
165
174
  # <%= link_to(@person.name, person_path) %>
166
175
  # # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>
167
176
  #
168
- # To preserve the case of the characters in a string, use the `preserve_case` argument.
177
+ # To preserve the case of the characters in a string, use the +preserve_case+ argument.
169
178
  #
170
179
  # class Person
171
180
  # def to_param
@@ -202,7 +211,7 @@ class String
202
211
  ActiveSupport::Inflector.classify(self)
203
212
  end
204
213
 
205
- # Capitalizes the first word, turns underscores into spaces, and strips a
214
+ # Capitalizes the first word, turns underscores into spaces, and (by default)strips a
206
215
  # trailing '_id' if present.
207
216
  # Like +titleize+, this is meant for creating pretty output.
208
217
  #
@@ -210,12 +219,17 @@ class String
210
219
  # optional parameter +capitalize+ to false.
211
220
  # By default, this parameter is true.
212
221
  #
213
- # 'employee_salary'.humanize # => "Employee salary"
214
- # 'author_id'.humanize # => "Author"
215
- # 'author_id'.humanize(capitalize: false) # => "author"
216
- # '_id'.humanize # => "Id"
217
- def humanize(options = {})
218
- ActiveSupport::Inflector.humanize(self, options)
222
+ # The trailing '_id' can be kept and capitalized by setting the
223
+ # optional parameter +keep_id_suffix+ to true.
224
+ # By default, this parameter is false.
225
+ #
226
+ # 'employee_salary'.humanize # => "Employee salary"
227
+ # 'author_id'.humanize # => "Author"
228
+ # 'author_id'.humanize(capitalize: false) # => "author"
229
+ # '_id'.humanize # => "Id"
230
+ # 'author_id'.humanize(keep_id_suffix: true) # => "Author Id"
231
+ def humanize(capitalize: true, keep_id_suffix: false)
232
+ ActiveSupport::Inflector.humanize(self, capitalize: capitalize, keep_id_suffix: keep_id_suffix)
219
233
  end
220
234
 
221
235
  # Converts just the first character to uppercase.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/string_inquirer"
2
4
 
3
5
  class String
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/multibyte"
2
4
 
3
5
  class String
@@ -14,6 +16,8 @@ class String
14
16
  # >> "lj".mb_chars.upcase.to_s
15
17
  # => "LJ"
16
18
  #
19
+ # NOTE: An above example is useful for pre Ruby 2.4. Ruby 2.4 supports Unicode case mappings.
20
+ #
17
21
  # == Method chaining
18
22
  #
19
23
  # All the methods on the Chars proxy which normally return a string will return a Chars object. This allows
@@ -1,5 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "erb"
2
4
  require "active_support/core_ext/kernel/singleton_class"
5
+ require "active_support/core_ext/module/redefine_method"
3
6
  require "active_support/multibyte/unicode"
4
7
 
5
8
  class ERB
@@ -12,22 +15,18 @@ class ERB
12
15
  # A utility method for escaping HTML tag characters.
13
16
  # This method is also aliased as <tt>h</tt>.
14
17
  #
15
- # In your ERB templates, use this method to escape any unsafe content. For example:
16
- # <%= h @person.name %>
17
- #
18
18
  # puts html_escape('is a > 0 & a < 10?')
19
19
  # # => is a &gt; 0 &amp; a &lt; 10?
20
20
  def html_escape(s)
21
21
  unwrapped_html_escape(s).html_safe
22
22
  end
23
23
 
24
- # Aliasing twice issues a warning "discarding old...". Remove first to avoid it.
25
- remove_method(:h)
24
+ silence_redefinition_of_method :h
26
25
  alias h html_escape
27
26
 
28
27
  module_function :h
29
28
 
30
- singleton_class.send(:remove_method, :html_escape)
29
+ singleton_class.silence_redefinition_of_method :html_escape
31
30
  module_function :html_escape
32
31
 
33
32
  # HTML escapes strings but doesn't wrap them with an ActiveSupport::SafeBuffer.
@@ -251,7 +250,7 @@ class String
251
250
  # Marks a string as trusted safe. It will be inserted into HTML with no
252
251
  # additional escaping performed. It is your responsibility to ensure that the
253
252
  # string contains no malicious content. This method is equivalent to the
254
- # `raw` helper in views. It is recommended that you use `sanitize` instead of
253
+ # +raw+ helper in views. It is recommended that you use +sanitize+ instead of
255
254
  # this method. It should never be called on user input.
256
255
  def html_safe
257
256
  ActiveSupport::SafeBuffer.new(self)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class String
2
4
  alias_method :starts_with?, :start_with?
3
5
  alias_method :ends_with?, :end_with?
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class String
2
4
  # Strips indentation in heredocs.
3
5
  #
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/core_ext/string/conversions"
2
4
  require "active_support/core_ext/time/zones"
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/core_ext/time/acts_like"
2
4
  require "active_support/core_ext/time/calculations"
3
5
  require "active_support/core_ext/time/compatibility"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/core_ext/object/acts_like"
2
4
 
3
5
  class Time
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/duration"
2
4
  require "active_support/core_ext/time/conversions"
3
5
  require "active_support/time_with_zone"
@@ -107,21 +109,22 @@ class Time
107
109
  # to the +options+ parameter. The time options (<tt>:hour</tt>, <tt>:min</tt>,
108
110
  # <tt>:sec</tt>, <tt>:usec</tt>, <tt>:nsec</tt>) reset cascadingly, so if only
109
111
  # the hour is passed, then minute, sec, usec and nsec is set to 0. If the hour
110
- # and minute is passed, then sec, usec and nsec is set to 0. The +options+
111
- # parameter takes a hash with any of these keys: <tt>:year</tt>, <tt>:month</tt>,
112
- # <tt>:day</tt>, <tt>:hour</tt>, <tt>:min</tt>, <tt>:sec</tt>, <tt>:usec</tt>
113
- # <tt>:nsec</tt>. Pass either <tt>:usec</tt> or <tt>:nsec</tt>, not both.
112
+ # and minute is passed, then sec, usec and nsec is set to 0. The +options+ parameter
113
+ # takes a hash with any of these keys: <tt>:year</tt>, <tt>:month</tt>, <tt>:day</tt>,
114
+ # <tt>:hour</tt>, <tt>:min</tt>, <tt>:sec</tt>, <tt>:usec</tt>, <tt>:nsec</tt>,
115
+ # <tt>:offset</tt>. Pass either <tt>:usec</tt> or <tt>:nsec</tt>, not both.
114
116
  #
115
117
  # Time.new(2012, 8, 29, 22, 35, 0).change(day: 1) # => Time.new(2012, 8, 1, 22, 35, 0)
116
118
  # Time.new(2012, 8, 29, 22, 35, 0).change(year: 1981, day: 1) # => Time.new(1981, 8, 1, 22, 35, 0)
117
119
  # Time.new(2012, 8, 29, 22, 35, 0).change(year: 1981, hour: 0) # => Time.new(1981, 8, 29, 0, 0, 0)
118
120
  def change(options)
119
- new_year = options.fetch(:year, year)
120
- new_month = options.fetch(:month, month)
121
- new_day = options.fetch(:day, day)
122
- new_hour = options.fetch(:hour, hour)
123
- new_min = options.fetch(:min, options[:hour] ? 0 : min)
124
- new_sec = options.fetch(:sec, (options[:hour] || options[:min]) ? 0 : sec)
121
+ new_year = options.fetch(:year, year)
122
+ new_month = options.fetch(:month, month)
123
+ new_day = options.fetch(:day, day)
124
+ new_hour = options.fetch(:hour, hour)
125
+ new_min = options.fetch(:min, options[:hour] ? 0 : min)
126
+ new_sec = options.fetch(:sec, (options[:hour] || options[:min]) ? 0 : sec)
127
+ new_offset = options.fetch(:offset, nil)
125
128
 
126
129
  if new_nsec = options[:nsec]
127
130
  raise ArgumentError, "Can't change both :nsec and :usec at the same time: #{options.inspect}" if options[:usec]
@@ -130,13 +133,18 @@ class Time
130
133
  new_usec = options.fetch(:usec, (options[:hour] || options[:min] || options[:sec]) ? 0 : Rational(nsec, 1000))
131
134
  end
132
135
 
133
- if utc?
134
- ::Time.utc(new_year, new_month, new_day, new_hour, new_min, new_sec, new_usec)
136
+ raise ArgumentError, "argument out of range" if new_usec >= 1000000
137
+
138
+ new_sec += Rational(new_usec, 1000000)
139
+
140
+ if new_offset
141
+ ::Time.new(new_year, new_month, new_day, new_hour, new_min, new_sec, new_offset)
142
+ elsif utc?
143
+ ::Time.utc(new_year, new_month, new_day, new_hour, new_min, new_sec)
135
144
  elsif zone
136
- ::Time.local(new_year, new_month, new_day, new_hour, new_min, new_sec, new_usec)
145
+ ::Time.local(new_year, new_month, new_day, new_hour, new_min, new_sec)
137
146
  else
138
- raise ArgumentError, "argument out of range" if new_usec >= 1000000
139
- ::Time.new(new_year, new_month, new_day, new_hour, new_min, new_sec + (new_usec.to_r / 1000000), utc_offset)
147
+ ::Time.new(new_year, new_month, new_day, new_hour, new_min, new_sec, utc_offset)
140
148
  end
141
149
  end
142
150
 
@@ -1,10 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/core_ext/date_and_time/compatibility"
2
- require "active_support/core_ext/module/remove_method"
4
+ require "active_support/core_ext/module/redefine_method"
3
5
 
4
6
  class Time
5
7
  include DateAndTime::Compatibility
6
8
 
7
- remove_possible_method :to_time
9
+ silence_redefinition_of_method :to_time
8
10
 
9
11
  # Either return +self+ or the time in the local system timezone depending
10
12
  # on the setting of +ActiveSupport.to_time_preserves_timezone+.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/inflector/methods"
2
4
  require "active_support/values/time_zone"
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/time_with_zone"
2
4
  require "active_support/core_ext/time/acts_like"
3
5
  require "active_support/core_ext/date_and_time/zones"
@@ -53,10 +55,10 @@ class Time
53
55
  # end
54
56
  # end
55
57
  #
56
- # NOTE: This won't affect any <tt>ActiveSupport::TimeWithZone</tt>
57
- # objects that have already been created, e.g. any model timestamp
58
- # attributes that have been read before the block will remain in
59
- # the application's default timezone.
58
+ # NOTE: This won't affect any <tt>ActiveSupport::TimeWithZone</tt>
59
+ # objects that have already been created, e.g. any model timestamp
60
+ # attributes that have been read before the block will remain in
61
+ # the application's default timezone.
60
62
  def use_zone(time_zone)
61
63
  new_zone = find_zone!(time_zone)
62
64
  begin
@@ -1,16 +1,16 @@
1
- require "uri"
2
- str = "\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E" # Ni-ho-nn-go in UTF-8, means Japanese.
3
- parser = URI::Parser.new
1
+ # frozen_string_literal: true
4
2
 
5
- unless str == parser.unescape(parser.escape(str))
3
+ require "uri"
4
+ if RUBY_VERSION < "2.6.0"
5
+ require "active_support/core_ext/module/redefine_method"
6
6
  URI::Parser.class_eval do
7
- remove_method :unescape
7
+ silence_redefinition_of_method :unescape
8
8
  def unescape(str, escaped = /%[a-fA-F\d]{2}/)
9
9
  # TODO: Are we actually sure that ASCII == UTF-8?
10
10
  # YK: My initial experiments say yes, but let's be sure please
11
11
  enc = str.encoding
12
12
  enc = Encoding::UTF_8 if enc == Encoding::US_ASCII
13
- str.gsub(escaped) { |match| [match[1, 2].hex].pack("C") }.force_encoding(enc)
13
+ str.dup.force_encoding(Encoding::ASCII_8BIT).gsub(escaped) { |match| [match[1, 2].hex].pack("C") }.force_encoding(enc)
14
14
  end
15
15
  end
16
16
  end