Skip to content

Commit

Permalink
Document how to remove to_s deprecation warnings when defaul format i…
Browse files Browse the repository at this point in the history
…s 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.
  • Loading branch information
rafaelfranca committed Aug 23, 2023
1 parent e478dca commit f5fd433
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 18 deletions.
4 changes: 2 additions & 2 deletions actioncable/lib/action_cable/connection/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,15 @@ def started_request_message
request.filtered_path,
websocket.possible? ? " [WebSocket]" : "[non-WebSocket]",
request.ip,
Time.now.to_s ]
Time.now.to_default_s ]
end

def finished_request_message
'Finished "%s"%s for %s at %s' % [
request.filtered_path,
websocket.possible? ? " [WebSocket]" : "[non-WebSocket]",
request.ip,
Time.now.to_s ]
Time.now.to_default_s ]
end

def invalid_request_message
Expand Down
2 changes: 1 addition & 1 deletion activejob/lib/active_job/log_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def enqueue_at(event)
end
else
info do
"Enqueued #{job.class.name} (Job ID: #{job.job_id}) to #{queue_name(event)} at #{scheduled_at(event)}" + args_info(job)
"Enqueued #{job.class.name} (Job ID: #{job.job_id}) to #{queue_name(event)} at #{scheduled_at(event).to_default_s}" + args_info(job)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ def to_s(format = NOT_SET) # :nodoc:
end
elsif format == NOT_SET
if formatter = DATE_FORMATS[:default]
ActiveSupport::Deprecation.warn(
"Using a :default format for Date#to_s is deprecated. Please use Date#to_fs instead."
)
ActiveSupport::Deprecation.warn(<<-MSG.squish)
Using a :default format for Date#to_s is deprecated. Please use Date#to_fs instead. If you fixed all places
inside your application that you see this deprecation, you can set
`ENV['RAILS_DISABLE_DEPRECATED_TO_S_CONVERSION']` to `"true"` in the `config/application.rb` file before
the `Bundler.require` call to fix all the callers outside of your application.
MSG
if formatter.respond_to?(:call)
formatter.call(self).to_s
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ def to_s(format = NOT_SET) # :nodoc:
formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
elsif format == NOT_SET
if formatter = ::Time::DATE_FORMATS[:default]
ActiveSupport::Deprecation.warn(
"Using a :default format for DateTime#to_s is deprecated. Please use DateTime#to_fs instead."
)
ActiveSupport::Deprecation.warn(<<-MSG.squish)
Using a :default format for DateTime#to_s is deprecated. Please use DateTime#to_fs instead. If you fixed all
places inside your application that you see this deprecation, you can set
`ENV['RAILS_DISABLE_DEPRECATED_TO_S_CONVERSION']` to `"true"` in the `config/application.rb` file before
the `Bundler.require` call to fix all the callers outside of your application.
MSG
if formatter.respond_to?(:call)
formatter.call(self).to_s
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ def to_s(format = NOT_SET)
formatter.call(first, last)
elsif format == NOT_SET
if formatter = RangeWithFormat::RANGE_FORMATS[:default]
ActiveSupport::Deprecation.warn(
"Using a :default format for Range#to_s is deprecated. Please use Range#to_fs instead."
)
ActiveSupport::Deprecation.warn(<<-MSG.squish)
Using a :default format for Range#to_s is deprecated. Please use Range#to_fs instead. If you fixed all
places inside your application that you see this deprecation, you can set
`ENV['RAILS_DISABLE_DEPRECATED_TO_S_CONVERSION']` to `"true"` in the `config/application.rb` file before
the `Bundler.require` call to fix all the callers outside of your application.
MSG
formatter.call(first, last)
else
super()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ def to_s(format = NOT_SET) # :nodoc:
formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
elsif format == NOT_SET
if formatter = ::Time::DATE_FORMATS[:default]
ActiveSupport::Deprecation.warn(
"Using a :default format for Time#to_s is deprecated. Please use Time#to_fs instead."
)
ActiveSupport::Deprecation.warn(<<-MSG.squish)
Using a :default format for Time#to_s is deprecated. Please use Time#to_fs instead. If you fixed all places
inside your application that you see this deprecation, you can set
`ENV['RAILS_DISABLE_DEPRECATED_TO_S_CONVERSION']` to `"true"` in the `config/application.rb` file before
the `Bundler.require` call to fix all the callers outside of your application.
MSG
if formatter.respond_to?(:call)
formatter.call(self).to_s
else
Expand Down
9 changes: 6 additions & 3 deletions activesupport/lib/active_support/time_with_zone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,12 @@ def to_s(format = NOT_SET)
formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
elsif format == NOT_SET
if formatter = ::Time::DATE_FORMATS[:default]
ActiveSupport::Deprecation.warn(
"Using a :default format for TimeWithZone#to_s is deprecated. Please use TimeWithZone#to_fs instead."
)
ActiveSupport::Deprecation.warn(<<-MSG.squish)
Using a :default format for TimeWithZone#to_s is deprecated. Please use TimeWithZone#to_fs instead.
If you fixed all places inside your application that you see this deprecation, you can set
`ENV['RAILS_DISABLE_DEPRECATED_TO_S_CONVERSION']` to `"true"` in the `config/application.rb` file before
the `Bundler.require` call to fix all the callers outside of your application.
MSG
formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
else
"#{time.strftime("%Y-%m-%d %H:%M:%S")} #{formatted_offset(false, 'UTC')}" # mimicking Ruby Time#to_s format
Expand Down

0 comments on commit f5fd433

Please sign in to comment.