Skip to content

Commit f5fd433

Browse files
committed
Document how to remove to_s deprecation warnings when defaul format is changed
Closes #48995. When users change `Time::DATE_FORMATS[:default]` or `Date::DATE_FORMATS[:default]` we are now showing a deprecation message when calling `to_s`. Users can fix this deprecation message in their own code. But there are a few other implicit callers of `to_s` that they can't change like JSON serialization. The only way for users to disable this deprecation message, at this point is to mark their application as safe to use the default Ruby behavior for `to_s` with an environment variable. We can't use the config because some gems will likely require the core extensions before the config is loaded.
1 parent e478dca commit f5fd433

File tree

7 files changed

+33
-18
lines changed

7 files changed

+33
-18
lines changed

actioncable/lib/action_cable/connection/base.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,15 +237,15 @@ def started_request_message
237237
request.filtered_path,
238238
websocket.possible? ? " [WebSocket]" : "[non-WebSocket]",
239239
request.ip,
240-
Time.now.to_s ]
240+
Time.now.to_default_s ]
241241
end
242242

243243
def finished_request_message
244244
'Finished "%s"%s for %s at %s' % [
245245
request.filtered_path,
246246
websocket.possible? ? " [WebSocket]" : "[non-WebSocket]",
247247
request.ip,
248-
Time.now.to_s ]
248+
Time.now.to_default_s ]
249249
end
250250

251251
def invalid_request_message

activejob/lib/active_job/log_subscriber.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def enqueue_at(event)
3838
end
3939
else
4040
info do
41-
"Enqueued #{job.class.name} (Job ID: #{job.job_id}) to #{queue_name(event)} at #{scheduled_at(event)}" + args_info(job)
41+
"Enqueued #{job.class.name} (Job ID: #{job.job_id}) to #{queue_name(event)} at #{scheduled_at(event).to_default_s}" + args_info(job)
4242
end
4343
end
4444
end

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ def to_s(format = NOT_SET) # :nodoc:
1616
end
1717
elsif format == NOT_SET
1818
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-
)
19+
ActiveSupport::Deprecation.warn(<<-MSG.squish)
20+
Using a :default format for Date#to_s is deprecated. Please use Date#to_fs instead. If you fixed all places
21+
inside your application that you see this deprecation, you can set
22+
`ENV['RAILS_DISABLE_DEPRECATED_TO_S_CONVERSION']` to `"true"` in the `config/application.rb` file before
23+
the `Bundler.require` call to fix all the callers outside of your application.
24+
MSG
2225
if formatter.respond_to?(:call)
2326
formatter.call(self).to_s
2427
else

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ def to_s(format = NOT_SET) # :nodoc:
1212
formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
1313
elsif format == NOT_SET
1414
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-
)
15+
ActiveSupport::Deprecation.warn(<<-MSG.squish)
16+
Using a :default format for DateTime#to_s is deprecated. Please use DateTime#to_fs instead. If you fixed all
17+
places inside your application that you see this deprecation, you can set
18+
`ENV['RAILS_DISABLE_DEPRECATED_TO_S_CONVERSION']` to `"true"` in the `config/application.rb` file before
19+
the `Bundler.require` call to fix all the callers outside of your application.
20+
MSG
1821
if formatter.respond_to?(:call)
1922
formatter.call(self).to_s
2023
else

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ def to_s(format = NOT_SET)
1111
formatter.call(first, last)
1212
elsif format == NOT_SET
1313
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-
)
14+
ActiveSupport::Deprecation.warn(<<-MSG.squish)
15+
Using a :default format for Range#to_s is deprecated. Please use Range#to_fs instead. If you fixed all
16+
places inside your application that you see this deprecation, you can set
17+
`ENV['RAILS_DISABLE_DEPRECATED_TO_S_CONVERSION']` to `"true"` in the `config/application.rb` file before
18+
the `Bundler.require` call to fix all the callers outside of your application.
19+
MSG
1720
formatter.call(first, last)
1821
else
1922
super()

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ def to_s(format = NOT_SET) # :nodoc:
1212
formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
1313
elsif format == NOT_SET
1414
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-
)
15+
ActiveSupport::Deprecation.warn(<<-MSG.squish)
16+
Using a :default format for Time#to_s is deprecated. Please use Time#to_fs instead. If you fixed all places
17+
inside your application that you see this deprecation, you can set
18+
`ENV['RAILS_DISABLE_DEPRECATED_TO_S_CONVERSION']` to `"true"` in the `config/application.rb` file before
19+
the `Bundler.require` call to fix all the callers outside of your application.
20+
MSG
1821
if formatter.respond_to?(:call)
1922
formatter.call(self).to_s
2023
else

activesupport/lib/active_support/time_with_zone.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,12 @@ def to_s(format = NOT_SET)
222222
formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
223223
elsif format == NOT_SET
224224
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-
)
225+
ActiveSupport::Deprecation.warn(<<-MSG.squish)
226+
Using a :default format for TimeWithZone#to_s is deprecated. Please use TimeWithZone#to_fs instead.
227+
If you fixed all places inside your application that you see this deprecation, you can set
228+
`ENV['RAILS_DISABLE_DEPRECATED_TO_S_CONVERSION']` to `"true"` in the `config/application.rb` file before
229+
the `Bundler.require` call to fix all the callers outside of your application.
230+
MSG
228231
formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
229232
else
230233
"#{time.strftime("%Y-%m-%d %H:%M:%S")} #{formatted_offset(false, 'UTC')}" # mimicking Ruby Time#to_s format

0 commit comments

Comments
 (0)