activesupport 4.2.11.3 → 5.0.0.beta1

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 (174) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +309 -485
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +2 -3
  5. data/lib/active_support.rb +8 -15
  6. data/lib/active_support/array_inquirer.rb +44 -0
  7. data/lib/active_support/backtrace_cleaner.rb +1 -1
  8. data/lib/active_support/cache.rb +59 -72
  9. data/lib/active_support/cache/file_store.rb +27 -19
  10. data/lib/active_support/cache/mem_cache_store.rb +71 -60
  11. data/lib/active_support/cache/memory_store.rb +16 -21
  12. data/lib/active_support/cache/null_store.rb +1 -4
  13. data/lib/active_support/cache/strategy/local_cache.rb +31 -20
  14. data/lib/active_support/callbacks.rb +107 -111
  15. data/lib/active_support/concern.rb +1 -1
  16. data/lib/active_support/concurrency/latch.rb +7 -15
  17. data/lib/active_support/concurrency/share_lock.rb +142 -0
  18. data/lib/active_support/configurable.rb +1 -0
  19. data/lib/active_support/core_ext.rb +2 -1
  20. data/lib/active_support/core_ext/array.rb +1 -0
  21. data/lib/active_support/core_ext/array/access.rb +13 -1
  22. data/lib/active_support/core_ext/array/conversions.rb +6 -4
  23. data/lib/active_support/core_ext/array/inquiry.rb +17 -0
  24. data/lib/active_support/core_ext/array/wrap.rb +5 -4
  25. data/lib/active_support/core_ext/big_decimal/conversions.rb +8 -10
  26. data/lib/active_support/core_ext/class.rb +0 -1
  27. data/lib/active_support/core_ext/class/attribute.rb +10 -9
  28. data/lib/active_support/core_ext/class/subclasses.rb +5 -2
  29. data/lib/active_support/core_ext/date.rb +1 -1
  30. data/lib/active_support/core_ext/date/blank.rb +12 -0
  31. data/lib/active_support/core_ext/date/calculations.rb +1 -1
  32. data/lib/active_support/core_ext/date/conversions.rb +3 -3
  33. data/lib/active_support/core_ext/date_and_time/calculations.rb +93 -27
  34. data/lib/active_support/core_ext/date_and_time/zones.rb +1 -2
  35. data/lib/active_support/core_ext/date_time.rb +1 -1
  36. data/lib/active_support/core_ext/date_time/blank.rb +12 -0
  37. data/lib/active_support/core_ext/date_time/calculations.rb +7 -23
  38. data/lib/active_support/core_ext/date_time/conversions.rb +2 -0
  39. data/lib/active_support/core_ext/enumerable.rb +27 -17
  40. data/lib/active_support/core_ext/file/atomic.rb +30 -25
  41. data/lib/active_support/core_ext/hash/compact.rb +15 -19
  42. data/lib/active_support/core_ext/hash/conversions.rb +21 -2
  43. data/lib/active_support/core_ext/hash/deep_merge.rb +1 -1
  44. data/lib/active_support/core_ext/hash/except.rb +9 -8
  45. data/lib/active_support/core_ext/hash/indifferent_access.rb +1 -1
  46. data/lib/active_support/core_ext/hash/keys.rb +22 -18
  47. data/lib/active_support/core_ext/hash/slice.rb +1 -1
  48. data/lib/active_support/core_ext/hash/transform_values.rb +13 -7
  49. data/lib/active_support/core_ext/integer/time.rb +1 -1
  50. data/lib/active_support/core_ext/kernel.rb +0 -1
  51. data/lib/active_support/core_ext/kernel/debugger.rb +3 -10
  52. data/lib/active_support/core_ext/kernel/reporting.rb +0 -84
  53. data/lib/active_support/core_ext/load_error.rb +4 -2
  54. data/lib/active_support/core_ext/marshal.rb +8 -13
  55. data/lib/active_support/core_ext/module.rb +1 -0
  56. data/lib/active_support/core_ext/module/aliasing.rb +6 -1
  57. data/lib/active_support/core_ext/module/anonymous.rb +10 -1
  58. data/lib/active_support/core_ext/module/attr_internal.rb +2 -5
  59. data/lib/active_support/core_ext/module/attribute_accessors.rb +7 -7
  60. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +141 -0
  61. data/lib/active_support/core_ext/module/concerning.rb +4 -4
  62. data/lib/active_support/core_ext/module/delegation.rb +7 -14
  63. data/lib/active_support/core_ext/module/method_transplanting.rb +3 -13
  64. data/lib/active_support/core_ext/module/qualified_const.rb +30 -12
  65. data/lib/active_support/core_ext/module/remove_method.rb +23 -0
  66. data/lib/active_support/core_ext/name_error.rb +15 -2
  67. data/lib/active_support/core_ext/numeric.rb +1 -0
  68. data/lib/active_support/core_ext/numeric/bytes.rb +20 -0
  69. data/lib/active_support/core_ext/numeric/conversions.rb +12 -23
  70. data/lib/active_support/core_ext/numeric/inquiry.rb +26 -0
  71. data/lib/active_support/core_ext/numeric/time.rb +20 -0
  72. data/lib/active_support/core_ext/object.rb +0 -1
  73. data/lib/active_support/core_ext/object/blank.rb +11 -2
  74. data/lib/active_support/core_ext/object/deep_dup.rb +10 -3
  75. data/lib/active_support/core_ext/object/duplicable.rb +39 -70
  76. data/lib/active_support/core_ext/object/inclusion.rb +2 -2
  77. data/lib/active_support/core_ext/object/instance_variables.rb +1 -1
  78. data/lib/active_support/core_ext/object/json.rb +9 -7
  79. data/lib/active_support/core_ext/object/to_query.rb +1 -1
  80. data/lib/active_support/core_ext/object/try.rb +67 -21
  81. data/lib/active_support/core_ext/object/with_options.rb +1 -1
  82. data/lib/active_support/core_ext/range/conversions.rb +18 -6
  83. data/lib/active_support/core_ext/range/each.rb +16 -18
  84. data/lib/active_support/core_ext/range/include_range.rb +20 -20
  85. data/lib/active_support/core_ext/securerandom.rb +23 -0
  86. data/lib/active_support/core_ext/string/access.rb +1 -1
  87. data/lib/active_support/core_ext/string/behavior.rb +1 -1
  88. data/lib/active_support/core_ext/string/conversions.rb +2 -2
  89. data/lib/active_support/core_ext/string/filters.rb +1 -2
  90. data/lib/active_support/core_ext/string/inflections.rb +23 -5
  91. data/lib/active_support/core_ext/string/multibyte.rb +11 -7
  92. data/lib/active_support/core_ext/string/output_safety.rb +8 -9
  93. data/lib/active_support/core_ext/string/strip.rb +3 -6
  94. data/lib/active_support/core_ext/struct.rb +3 -6
  95. data/lib/active_support/core_ext/time.rb +0 -2
  96. data/lib/active_support/core_ext/time/calculations.rb +18 -16
  97. data/lib/active_support/core_ext/time/conversions.rb +4 -2
  98. data/lib/active_support/core_ext/time/marshal.rb +2 -29
  99. data/lib/active_support/core_ext/time/zones.rb +19 -3
  100. data/lib/active_support/core_ext/uri.rb +1 -3
  101. data/lib/active_support/dependencies.rb +79 -44
  102. data/lib/active_support/dependencies/interlock.rb +47 -0
  103. data/lib/active_support/deprecation/behaviors.rb +12 -0
  104. data/lib/active_support/deprecation/method_wrappers.rb +42 -16
  105. data/lib/active_support/deprecation/proxy_wrappers.rb +47 -24
  106. data/lib/active_support/deprecation/reporting.rb +13 -2
  107. data/lib/active_support/duration.rb +5 -8
  108. data/lib/active_support/evented_file_update_checker.rb +150 -0
  109. data/lib/active_support/file_update_checker.rb +1 -1
  110. data/lib/active_support/gem_version.rb +5 -5
  111. data/lib/active_support/hash_with_indifferent_access.rb +15 -17
  112. data/lib/active_support/i18n_railtie.rb +25 -4
  113. data/lib/active_support/inflector/inflections.rb +36 -5
  114. data/lib/active_support/inflector/methods.rb +87 -89
  115. data/lib/active_support/inflector/transliterate.rb +36 -21
  116. data/lib/active_support/json/decoding.rb +2 -8
  117. data/lib/active_support/json/encoding.rb +0 -50
  118. data/lib/active_support/key_generator.rb +4 -4
  119. data/lib/active_support/log_subscriber.rb +1 -1
  120. data/lib/active_support/log_subscriber/test_helper.rb +3 -3
  121. data/lib/active_support/logger.rb +4 -52
  122. data/lib/active_support/logger_silence.rb +3 -5
  123. data/lib/active_support/message_encryptor.rb +4 -11
  124. data/lib/active_support/message_verifier.rb +64 -8
  125. data/lib/active_support/multibyte/chars.rb +12 -3
  126. data/lib/active_support/multibyte/unicode.rb +6 -8
  127. data/lib/active_support/notifications.rb +2 -2
  128. data/lib/active_support/notifications/fanout.rb +5 -5
  129. data/lib/active_support/notifications/instrumenter.rb +19 -2
  130. data/lib/active_support/number_helper.rb +21 -15
  131. data/lib/active_support/number_helper/number_to_currency_converter.rb +4 -4
  132. data/lib/active_support/number_helper/number_to_delimited_converter.rb +7 -2
  133. data/lib/active_support/number_helper/number_to_human_converter.rb +6 -4
  134. data/lib/active_support/number_helper/number_to_human_size_converter.rb +5 -1
  135. data/lib/active_support/number_helper/number_to_percentage_converter.rb +1 -1
  136. data/lib/active_support/number_helper/number_to_rounded_converter.rb +28 -25
  137. data/lib/active_support/ordered_options.rb +15 -1
  138. data/lib/active_support/per_thread_registry.rb +3 -0
  139. data/lib/active_support/rails.rb +2 -2
  140. data/lib/active_support/railtie.rb +6 -1
  141. data/lib/active_support/rescuable.rb +4 -4
  142. data/lib/active_support/security_utils.rb +0 -7
  143. data/lib/active_support/string_inquirer.rb +1 -1
  144. data/lib/active_support/subscriber.rb +5 -10
  145. data/lib/active_support/tagged_logging.rb +3 -1
  146. data/lib/active_support/test_case.rb +13 -25
  147. data/lib/active_support/testing/assertions.rb +15 -13
  148. data/lib/active_support/testing/autorun.rb +8 -1
  149. data/lib/active_support/testing/composite_filter.rb +54 -0
  150. data/lib/active_support/testing/deprecation.rb +9 -8
  151. data/lib/active_support/testing/file_fixtures.rb +34 -0
  152. data/lib/active_support/testing/isolation.rb +22 -8
  153. data/lib/active_support/testing/method_call_assertions.rb +41 -0
  154. data/lib/active_support/testing/stream.rb +42 -0
  155. data/lib/active_support/testing/time_helpers.rb +6 -6
  156. data/lib/active_support/time_with_zone.rb +135 -53
  157. data/lib/active_support/values/time_zone.rb +80 -46
  158. data/lib/active_support/values/unicode_tables.dat +0 -0
  159. data/lib/active_support/xml_mini.rb +15 -30
  160. data/lib/active_support/xml_mini/jdom.rb +1 -1
  161. data/lib/active_support/xml_mini/libxml.rb +5 -3
  162. data/lib/active_support/xml_mini/libxmlsax.rb +4 -1
  163. data/lib/active_support/xml_mini/nokogiri.rb +5 -3
  164. data/lib/active_support/xml_mini/nokogirisax.rb +3 -1
  165. data/lib/active_support/xml_mini/rexml.rb +3 -1
  166. metadata +57 -21
  167. data/lib/active_support/core_ext/big_decimal/yaml_conversions.rb +0 -16
  168. data/lib/active_support/core_ext/class/delegating_attributes.rb +0 -45
  169. data/lib/active_support/core_ext/date_and_time/compatibility.rb +0 -15
  170. data/lib/active_support/core_ext/date_time/compatibility.rb +0 -16
  171. data/lib/active_support/core_ext/object/itself.rb +0 -15
  172. data/lib/active_support/core_ext/thread.rb +0 -86
  173. data/lib/active_support/core_ext/time/compatibility.rb +0 -14
  174. data/lib/active_support/logger_thread_safe_level.rb +0 -32
@@ -38,7 +38,7 @@ class Array
38
38
  # Calls <tt>to_param</tt> on all its elements and joins the result with
39
39
  # slashes. This is used by <tt>url_for</tt> in Action Pack.
40
40
  def to_param
41
- collect { |e| e.to_param }.join '/'
41
+ collect(&:to_param).join '/'
42
42
  end
43
43
 
44
44
  # Converts an array into a string suitable for use as a URL query string,
@@ -1,4 +1,34 @@
1
+ require 'delegate'
2
+
3
+ module ActiveSupport
4
+ module Tryable #:nodoc:
5
+ def try(*a, &b)
6
+ try!(*a, &b) if a.empty? || respond_to?(a.first)
7
+ end
8
+
9
+ def try!(*a, &b)
10
+ if a.empty? && block_given?
11
+ if b.arity == 0
12
+ instance_eval(&b)
13
+ else
14
+ yield self
15
+ end
16
+ else
17
+ public_send(*a, &b)
18
+ end
19
+ end
20
+ end
21
+ end
22
+
1
23
  class Object
24
+ include ActiveSupport::Tryable
25
+
26
+ ##
27
+ # :method: try
28
+ #
29
+ # :call-seq:
30
+ # try(*a, &b)
31
+ #
2
32
  # Invokes the public method whose name goes as first argument just like
3
33
  # +public_send+ does, except that if the receiver does not respond to it the
4
34
  # call returns +nil+ rather than raising an exception.
@@ -21,11 +51,11 @@ class Object
21
51
  #
22
52
  # +try+ will also return +nil+ if the receiver does not respond to the method:
23
53
  #
24
- # @person.try(:non_existing_method) #=> nil
54
+ # @person.try(:non_existing_method) # => nil
25
55
  #
26
56
  # instead of
27
57
  #
28
- # @person.non_existing_method if @person.respond_to?(:non_existing_method) #=> nil
58
+ # @person.non_existing_method if @person.respond_to?(:non_existing_method) # => nil
29
59
  #
30
60
  # +try+ returns +nil+ when called on +nil+ regardless of whether it responds
31
61
  # to the method:
@@ -56,27 +86,40 @@ class Object
56
86
  #
57
87
  # Please also note that +try+ is defined on +Object+. Therefore, it won't work
58
88
  # with instances of classes that do not have +Object+ among their ancestors,
59
- # like direct subclasses of +BasicObject+. For example, using +try+ with
60
- # +SimpleDelegator+ will delegate +try+ to the target instead of calling it on
61
- # the delegator itself.
62
- def try(*a, &b)
63
- try!(*a, &b) if a.empty? || respond_to?(a.first)
64
- end
89
+ # like direct subclasses of +BasicObject+.
90
+
91
+ ##
92
+ # :method: try!
93
+ #
94
+ # :call-seq:
95
+ # try!(*a, &b)
96
+ #
97
+ # Same as #try, but raises a +NoMethodError+ exception if the receiver is
98
+ # not +nil+ and does not implement the tried method.
99
+ #
100
+ # "a".try!(:upcase) # => "A"
101
+ # nil.try!(:upcase) # => nil
102
+ # 123.try!(:upcase) # => NoMethodError: undefined method `upcase' for 123:Fixnum
103
+ end
65
104
 
66
- # Same as #try, but will raise a NoMethodError exception if the receiver is not +nil+ and
67
- # does not implement the tried method.
105
+ class Delegator
106
+ include ActiveSupport::Tryable
68
107
 
69
- def try!(*a, &b)
70
- if a.empty? && block_given?
71
- if b.arity == 0
72
- instance_eval(&b)
73
- else
74
- yield self
75
- end
76
- else
77
- public_send(*a, &b)
78
- end
79
- end
108
+ ##
109
+ # :method: try
110
+ #
111
+ # :call-seq:
112
+ # try(a*, &b)
113
+ #
114
+ # See Object#try
115
+
116
+ ##
117
+ # :method: try!
118
+ #
119
+ # :call-seq:
120
+ # try!(a*, &b)
121
+ #
122
+ # See Object#try!
80
123
  end
81
124
 
82
125
  class NilClass
@@ -94,6 +137,9 @@ class NilClass
94
137
  nil
95
138
  end
96
139
 
140
+ # Calling +try!+ on +nil+ always returns +nil+.
141
+ #
142
+ # nil.try!(:name) # => nil
97
143
  def try!(*args)
98
144
  nil
99
145
  end
@@ -7,7 +7,7 @@ class Object
7
7
  # provided. Each method called on the block variable must take an options
8
8
  # hash as its final argument.
9
9
  #
10
- # Without <tt>with_options></tt>, this code contains duplication:
10
+ # Without <tt>with_options</tt>, this code contains duplication:
11
11
  #
12
12
  # class Account < ActiveRecord::Base
13
13
  # has_many :customers, dependent: :destroy
@@ -1,19 +1,31 @@
1
- class Range
1
+ module ActiveSupport::RangeWithFormat
2
2
  RANGE_FORMATS = {
3
3
  :db => Proc.new { |start, stop| "BETWEEN '#{start.to_s(:db)}' AND '#{stop.to_s(:db)}'" }
4
4
  }
5
5
 
6
- # Gives a human readable format of the range.
6
+ # Convert range to a formatted string. See RANGE_FORMATS for predefined formats.
7
7
  #
8
- # (1..100).to_formatted_s # => "1..100"
9
- def to_formatted_s(format = :default)
8
+ # range = (1..100) # => 1..100
9
+ #
10
+ # range.to_s # => "1..100"
11
+ # range.to_s(:db) # => "BETWEEN '1' AND '100'"
12
+ #
13
+ # == Adding your own range formats to to_s
14
+ # You can add your own formats to the Range::RANGE_FORMATS hash.
15
+ # Use the format name as the hash key and a Proc instance.
16
+ #
17
+ # # config/initializers/range_formats.rb
18
+ # Range::RANGE_FORMATS[:short] = ->(start, stop) { "Between #{start.to_s(:db)} and #{stop.to_s(:db)}" }
19
+ def to_s(format = :default)
10
20
  if formatter = RANGE_FORMATS[format]
11
21
  formatter.call(first, last)
12
22
  else
13
- to_default_s
23
+ super()
14
24
  end
15
25
  end
16
26
 
17
27
  alias_method :to_default_s, :to_s
18
- alias_method :to_s, :to_formatted_s
28
+ alias_method :to_formatted_s, :to_s
19
29
  end
30
+
31
+ Range.prepend(ActiveSupport::RangeWithFormat)
@@ -1,23 +1,21 @@
1
- require 'active_support/core_ext/module/aliasing'
1
+ module ActiveSupport
2
+ module EachTimeWithZone #:nodoc:
3
+ def each(&block)
4
+ ensure_iteration_allowed
5
+ super
6
+ end
2
7
 
3
- class Range #:nodoc:
8
+ def step(n = 1, &block)
9
+ ensure_iteration_allowed
10
+ super
11
+ end
4
12
 
5
- def each_with_time_with_zone(&block)
6
- ensure_iteration_allowed
7
- each_without_time_with_zone(&block)
8
- end
9
- alias_method_chain :each, :time_with_zone
13
+ private
10
14
 
11
- def step_with_time_with_zone(n = 1, &block)
12
- ensure_iteration_allowed
13
- step_without_time_with_zone(n, &block)
14
- end
15
- alias_method_chain :step, :time_with_zone
16
-
17
- private
18
- def ensure_iteration_allowed
19
- if first.is_a?(Time)
20
- raise TypeError, "can't iterate from #{first.class}"
21
- end
15
+ def ensure_iteration_allowed
16
+ raise TypeError, "can't iterate from #{first.class}" if first.is_a?(Time)
17
+ end
22
18
  end
23
19
  end
20
+
21
+ Range.prepend(ActiveSupport::EachTimeWithZone)
@@ -1,23 +1,23 @@
1
- require 'active_support/core_ext/module/aliasing'
2
-
3
- class Range
4
- # Extends the default Range#include? to support range comparisons.
5
- # (1..5).include?(1..5) # => true
6
- # (1..5).include?(2..3) # => true
7
- # (1..5).include?(2..6) # => false
8
- #
9
- # The native Range#include? behavior is untouched.
10
- # ('a'..'f').include?('c') # => true
11
- # (5..9).include?(11) # => false
12
- def include_with_range?(value)
13
- if value.is_a?(::Range)
14
- # 1...10 includes 1..9 but it does not include 1..10.
15
- operator = exclude_end? && !value.exclude_end? ? :< : :<=
16
- include_without_range?(value.first) && value.last.send(operator, last)
17
- else
18
- include_without_range?(value)
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
19
  end
20
20
  end
21
-
22
- alias_method_chain :include?, :range
23
21
  end
22
+
23
+ Range.prepend(ActiveSupport::IncludeWithRange)
@@ -0,0 +1,23 @@
1
+ require 'securerandom'
2
+
3
+ module SecureRandom
4
+ BASE58_ALPHABET = ('0'..'9').to_a + ('A'..'Z').to_a + ('a'..'z').to_a - ['0', 'O', 'I', 'l']
5
+ # SecureRandom.base58 generates a random base58 string.
6
+ #
7
+ # The argument _n_ specifies the length, of the random string to be generated.
8
+ #
9
+ # If _n_ is not specified or is nil, 16 is assumed. It may be larger in the future.
10
+ #
11
+ # The result may contain alphanumeric characters except 0, O, I and l
12
+ #
13
+ # p SecureRandom.base58 # => "4kUgL2pdQMSCQtjE"
14
+ # p SecureRandom.base58(24) # => "77TMHrHJFvFDwodq8w7Ev2m7"
15
+ #
16
+ def self.base58(n = 16)
17
+ SecureRandom.random_bytes(n).unpack("C*").map do |byte|
18
+ idx = byte % 64
19
+ idx = SecureRandom.random_number(58) if idx >= 58
20
+ BASE58_ALPHABET[idx]
21
+ end.join
22
+ end
23
+ end
@@ -1,5 +1,5 @@
1
1
  class String
2
- # If you pass a single integer, returns a substring of one character at that
2
+ # If you pass a single Fixnum, returns a substring of one character at that
3
3
  # position. The first character of the string is at position 0, the next at
4
4
  # position 1, and so on. If a range is supplied, a substring containing
5
5
  # characters at offsets given by the range is returned. In both cases, if an
@@ -1,5 +1,5 @@
1
1
  class String
2
- # Enable more predictable duck-typing on String-like classes. See <tt>Object#acts_like?</tt>.
2
+ # Enables more predictable duck-typing on String-like classes. See <tt>Object#acts_like?</tt>.
3
3
  def acts_like_string?
4
4
  true
5
5
  end
@@ -14,7 +14,7 @@ class String
14
14
  # "06:12".to_time # => 2012-12-13 06:12:00 +0100
15
15
  # "2012-12-13 06:12".to_time # => 2012-12-13 06:12:00 +0100
16
16
  # "2012-12-13T06:12".to_time # => 2012-12-13 06:12:00 +0100
17
- # "2012-12-13T06:12".to_time(:utc) # => 2012-12-13 05:12:00 UTC
17
+ # "2012-12-13T06:12".to_time(:utc) # => 2012-12-13 06:12:00 UTC
18
18
  # "12/13/2012".to_time # => ArgumentError: argument out of range
19
19
  def to_time(form = :local)
20
20
  parts = Date._parse(self, false)
@@ -31,7 +31,7 @@ class String
31
31
  parts.fetch(:offset, form == :utc ? 0 : nil)
32
32
  )
33
33
 
34
- form == :utc ? time.utc : time.to_time
34
+ form == :utc ? time.utc : time.getlocal
35
35
  end
36
36
 
37
37
  # Converts a string to a Date value.
@@ -17,9 +17,8 @@ class String
17
17
  # str.squish! # => "foo bar boo"
18
18
  # str # => "foo bar boo"
19
19
  def squish!
20
- gsub!(/\A[[:space:]]+/, '')
21
- gsub!(/[[:space:]]+\z/, '')
22
20
  gsub!(/[[:space:]]+/, ' ')
21
+ strip!
23
22
  self
24
23
  end
25
24
 
@@ -164,25 +164,43 @@ class String
164
164
  #
165
165
  # <%= link_to(@person.name, person_path) %>
166
166
  # # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>
167
- def parameterize(sep = '-')
168
- ActiveSupport::Inflector.parameterize(self, sep)
167
+ #
168
+ # To preserve the case of the characters in a string, use the `preserve_case` argument.
169
+ #
170
+ # class Person
171
+ # def to_param
172
+ # "#{id}-#{name.parameterize(preserve_case: true)}"
173
+ # end
174
+ # end
175
+ #
176
+ # @person = Person.find(1)
177
+ # # => #<Person id: 1, name: "Donald E. Knuth">
178
+ #
179
+ # <%= link_to(@person.name, person_path) %>
180
+ # # => <a href="/person/1-Donald-E-Knuth">Donald E. Knuth</a>
181
+ def parameterize(sep = :unused, separator: '-', preserve_case: false)
182
+ unless sep == :unused
183
+ ActiveSupport::Deprecation.warn("Passing the separator argument as a positional parameter is deprecated and will soon be removed. Use `separator: '#{sep}'` instead.")
184
+ separator = sep
185
+ end
186
+ ActiveSupport::Inflector.parameterize(self, separator: separator, preserve_case: preserve_case)
169
187
  end
170
188
 
171
189
  # Creates the name of a table like Rails does for models to table names. This method
172
190
  # uses the +pluralize+ method on the last word in the string.
173
191
  #
174
192
  # 'RawScaledScorer'.tableize # => "raw_scaled_scorers"
175
- # 'egg_and_ham'.tableize # => "egg_and_hams"
193
+ # 'ham_and_egg'.tableize # => "ham_and_eggs"
176
194
  # 'fancyCategory'.tableize # => "fancy_categories"
177
195
  def tableize
178
196
  ActiveSupport::Inflector.tableize(self)
179
197
  end
180
198
 
181
- # Create a class name from a plural table name like Rails does for table names to models.
199
+ # Creates a class name from a plural table name like Rails does for table names to models.
182
200
  # Note that this returns a string and not a class. (To convert to an actual class
183
201
  # follow +classify+ with +constantize+.)
184
202
  #
185
- # 'egg_and_hams'.classify # => "EggAndHam"
203
+ # 'ham_and_eggs'.classify # => "HamAndEgg"
186
204
  # 'posts'.classify # => "Post"
187
205
  def classify
188
206
  ActiveSupport::Inflector.classify(self)
@@ -1,4 +1,3 @@
1
- # encoding: utf-8
2
1
  require 'active_support/multibyte'
3
2
 
4
3
  class String
@@ -10,12 +9,10 @@ class String
10
9
  # encapsulates the original string. A Unicode safe version of all the String methods are defined on this proxy
11
10
  # class. If the proxy class doesn't respond to a certain method, it's forwarded to the encapsulated string.
12
11
  #
13
- # name = 'Claus Müller'
14
- # name.reverse # => "rell??M sualC"
15
- # name.length # => 13
16
- #
17
- # name.mb_chars.reverse.to_s # => "rellüM sualC"
18
- # name.mb_chars.length # => 12
12
+ # >> "lj".upcase
13
+ # => "lj"
14
+ # >> "lj".mb_chars.upcase.to_s
15
+ # => "LJ"
19
16
  #
20
17
  # == Method chaining
21
18
  #
@@ -36,6 +33,13 @@ class String
36
33
  ActiveSupport::Multibyte.proxy_class.new(self)
37
34
  end
38
35
 
36
+ # Returns +true+ if string has utf_8 encoding.
37
+ #
38
+ # utf_8_str = "some string".encode "UTF-8"
39
+ # iso_str = "some string".encode "ISO-8859-1"
40
+ #
41
+ # utf_8_str.is_utf8? # => true
42
+ # iso_str.is_utf8? # => false
39
43
  def is_utf8?
40
44
  case encoding
41
45
  when Encoding::UTF_8
@@ -1,6 +1,5 @@
1
1
  require 'erb'
2
2
  require 'active_support/core_ext/kernel/singleton_class'
3
- require 'active_support/deprecation'
4
3
 
5
4
  class ERB
6
5
  module Util
@@ -14,7 +13,7 @@ class ERB
14
13
  # This method is also aliased as <tt>h</tt>.
15
14
  #
16
15
  # In your ERB templates, use this method to escape any unsafe content. For example:
17
- # <%=h @person.name %>
16
+ # <%= h @person.name %>
18
17
  #
19
18
  # puts html_escape('is a > 0 & a < 10?')
20
19
  # # => is a &gt; 0 &amp; a &lt; 10?
@@ -38,7 +37,7 @@ class ERB
38
37
  if s.html_safe?
39
38
  s
40
39
  else
41
- s.gsub(HTML_ESCAPE_REGEXP, HTML_ESCAPE)
40
+ ActiveSupport::Multibyte::Unicode.tidy_bytes(s).gsub(HTML_ESCAPE_REGEXP, HTML_ESCAPE)
42
41
  end
43
42
  end
44
43
  module_function :unwrapped_html_escape
@@ -51,7 +50,7 @@ class ERB
51
50
  # html_escape_once('&lt;&lt; Accept & Checkout')
52
51
  # # => "&lt;&lt; Accept &amp; Checkout"
53
52
  def html_escape_once(s)
54
- result = s.to_s.gsub(HTML_ESCAPE_ONCE_REGEXP, HTML_ESCAPE)
53
+ result = ActiveSupport::Multibyte::Unicode.tidy_bytes(s.to_s).gsub(HTML_ESCAPE_ONCE_REGEXP, HTML_ESCAPE)
55
54
  s.html_safe? ? result.html_safe : result
56
55
  end
57
56
 
@@ -86,6 +85,11 @@ class ERB
86
85
  # automatically flag the result as HTML safe, since the raw value is unsafe to
87
86
  # use inside HTML attributes.
88
87
  #
88
+ # If your JSON is being used downstream for insertion into the DOM, be aware of
89
+ # whether or not it is being inserted via +html()+. Most jQuery plugins do this.
90
+ # If that is the case, be sure to +html_escape+ or +sanitize+ any user-generated
91
+ # content returned by your JSON.
92
+ #
89
93
  # If you need to output JSON elsewhere in your HTML, you can just do something
90
94
  # like this, as any unsafe characters (including quotation marks) will be
91
95
  # automatically escaped for you:
@@ -190,11 +194,6 @@ module ActiveSupport #:nodoc:
190
194
  super(html_escape_interpolated_argument(value))
191
195
  end
192
196
 
193
- def prepend!(value)
194
- ActiveSupport::Deprecation.deprecation_warning "ActiveSupport::SafeBuffer#prepend!", :prepend
195
- prepend value
196
- end
197
-
198
197
  def +(other)
199
198
  dup.concat(other)
200
199
  end