Skip to content

Commit 7e9ffc2

Browse files
committed
Fix to_s not using :default format with no args
When `to_s` was [deprecated][1] for `to_fs`, warnings were added when explicit formats were passed to `to_s`. To determine whether a format argument was passed, the method signature was changed from `to_s(format = :default)` to `to_s(format = EMPTY_OBJECT)`. While this enabled being able to warn on formats passed, it changed the behavior of not passing any format for applications that customized the `:default` format for one of these classes. This commit restores the previous behavior for `:default` formats while keeping the existing warnings. Additionally, it will continue to not warn for applications using `to_s` without arguments that haven't set a custom `:default` format. However, for applications that have set a custom `:default` format, it will now warn that those classes should be migrated to `to_fs`. [1]: f9fbfe0
1 parent f6c6534 commit 7e9ffc2

11 files changed

Lines changed: 116 additions & 5 deletions

File tree

activesupport/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Fix `to_s` with no arguments not respecting custom `:default` formats
2+
3+
*Hartley McGuire*
4+
15
* Fix `ActiveSupport::Inflector.humanize(nil)` raising ``NoMethodError: undefined method `end_with?' for nil:NilClass``.
26

37
*James Robinson*

activesupport/lib/active_support/core_ext/date/deprecated_conversions.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,18 @@ def to_s(format = NOT_SET) # :nodoc:
1515
strftime(formatter)
1616
end
1717
elsif format == NOT_SET
18-
to_default_s
18+
if formatter = DATE_FORMATS[:default]
19+
ActiveSupport::Deprecation.warn(
20+
"Using a :default format for Date#to_s is deprecated. Please use Date#to_fs instead."
21+
)
22+
if formatter.respond_to?(:call)
23+
formatter.call(self).to_s
24+
else
25+
strftime(formatter)
26+
end
27+
else
28+
to_default_s
29+
end
1930
else
2031
ActiveSupport::Deprecation.warn(
2132
"Date#to_s(#{format.inspect}) is deprecated. Please use Date#to_fs(#{format.inspect}) instead."

activesupport/lib/active_support/core_ext/date_time/deprecated_conversions.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,18 @@ def to_s(format = NOT_SET) # :nodoc:
1111
)
1212
formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
1313
elsif format == NOT_SET
14-
to_default_s
14+
if formatter = ::Time::DATE_FORMATS[:default]
15+
ActiveSupport::Deprecation.warn(
16+
"Using a :default format for DateTime#to_s is deprecated. Please use DateTime#to_fs instead."
17+
)
18+
if formatter.respond_to?(:call)
19+
formatter.call(self).to_s
20+
else
21+
strftime(formatter)
22+
end
23+
else
24+
to_default_s
25+
end
1526
else
1627
ActiveSupport::Deprecation.warn(
1728
"DateTime#to_s(#{format.inspect}) is deprecated. Please use DateTime#to_fs(#{format.inspect}) instead."

activesupport/lib/active_support/core_ext/range/deprecated_conversions.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ def to_s(format = NOT_SET)
1010
)
1111
formatter.call(first, last)
1212
elsif format == NOT_SET
13-
super()
13+
if formatter = RangeWithFormat::RANGE_FORMATS[:default]
14+
ActiveSupport::Deprecation.warn(
15+
"Using a :default format for Range#to_s is deprecated. Please use Range#to_fs instead."
16+
)
17+
formatter.call(first, last)
18+
else
19+
super()
20+
end
1421
else
1522
ActiveSupport::Deprecation.warn(
1623
"Range#to_s(#{format.inspect}) is deprecated. Please use Range#to_fs(#{format.inspect}) instead."

activesupport/lib/active_support/core_ext/time/deprecated_conversions.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,18 @@ def to_s(format = NOT_SET) # :nodoc:
1111
)
1212
formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
1313
elsif format == NOT_SET
14-
to_default_s
14+
if formatter = ::Time::DATE_FORMATS[:default]
15+
ActiveSupport::Deprecation.warn(
16+
"Using a :default format for Time#to_s is deprecated. Please use Time#to_fs instead."
17+
)
18+
if formatter.respond_to?(:call)
19+
formatter.call(self).to_s
20+
else
21+
strftime(formatter)
22+
end
23+
else
24+
to_default_s
25+
end
1526
else
1627
ActiveSupport::Deprecation.warn(
1728
"Time#to_s(#{format.inspect}) is deprecated. Please use Time#to_fs(#{format.inspect}) instead."

activesupport/lib/active_support/time_with_zone.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,14 @@ def to_s(format = NOT_SET)
221221
)
222222
formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
223223
elsif format == NOT_SET
224-
"#{time.strftime("%Y-%m-%d %H:%M:%S")} #{formatted_offset(false, 'UTC')}" # mimicking Ruby Time#to_s format
224+
if formatter = ::Time::DATE_FORMATS[:default]
225+
ActiveSupport::Deprecation.warn(
226+
"Using a :default format for TimeWithZone#to_s is deprecated. Please use TimeWithZone#to_fs instead."
227+
)
228+
formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
229+
else
230+
"#{time.strftime("%Y-%m-%d %H:%M:%S")} #{formatted_offset(false, 'UTC')}" # mimicking Ruby Time#to_s format
231+
end
225232
else
226233
ActiveSupport::Deprecation.warn(
227234
"TimeWithZone#to_s(#{format.inspect}) is deprecated. Please use TimeWithZone#to_fs(#{format.inspect}) instead."

activesupport/test/core_ext/date_ext_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ def test_to_s
5050
end
5151
end
5252

53+
def test_to_s_default_is_deprecated
54+
date = Date.new(2005, 2, 21)
55+
56+
Date::DATE_FORMATS[:default] = "%Y/%m/%d"
57+
58+
assert_deprecated do
59+
assert_equal "2005/02/21", date.to_s
60+
end
61+
ensure
62+
Date::DATE_FORMATS.delete(:default)
63+
end
64+
5365
def test_to_s_with_single_digit_day
5466
date = Date.new(2005, 2, 1)
5567
assert_equal "2005-02-01", date.to_s

activesupport/test/core_ext/date_time_ext_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ def test_to_s
5454
end
5555
end
5656

57+
def test_to_s_default_is_deprecated
58+
datetime = DateTime.new(2005, 2, 21, 14, 30, 0, 0)
59+
60+
Time::DATE_FORMATS[:default] = "%Y/%m/%d"
61+
62+
assert_deprecated do
63+
assert_equal "2005/02/21", datetime.to_s
64+
end
65+
ensure
66+
Time::DATE_FORMATS.delete(:default)
67+
end
68+
5769
def test_to_fs
5870
datetime = DateTime.new(2005, 2, 21, 14, 30, 0, 0)
5971
assert_equal "2005-02-21 14:30:00", datetime.to_fs(:db)

activesupport/test/core_ext/range_ext_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ def test_to_s_with_format
3535
end
3636
end
3737

38+
def test_to_s_default_is_deprecated
39+
number_range = (1..100)
40+
41+
ActiveSupport::RangeWithFormat::RANGE_FORMATS[:default] = -> (s, e) do
42+
"s: #{s}, e: #{e}"
43+
end
44+
45+
assert_deprecated do
46+
assert_equal "s: 1, e: 100", number_range.to_s
47+
end
48+
ensure
49+
ActiveSupport::RangeWithFormat::RANGE_FORMATS.delete(:default)
50+
end
51+
3852
def test_to_s_with_format_invalid_format
3953
number_range = (1..100)
4054

activesupport/test/core_ext/time_ext_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,18 @@ def test_to_s
642642
end
643643
end
644644

645+
def test_to_s_default_is_deprecated
646+
time = Time.utc(2005, 2, 21, 17, 44, 30.12345678901)
647+
648+
Time::DATE_FORMATS[:default] = "%Y/%m/%d"
649+
650+
assert_deprecated do
651+
assert_equal "2005/02/21", time.to_s
652+
end
653+
ensure
654+
Time::DATE_FORMATS.delete(:default)
655+
end
656+
645657
def test_to_fs
646658
time = Time.utc(2005, 2, 21, 17, 44, 30.12345678901)
647659
assert_equal time.to_s, time.to_fs(:doesnt_exist)

0 commit comments

Comments
 (0)