Avoid Time/Date::DATE_FORMATS deprecation on Rails 8.2#180
Open
kylekeesling wants to merge 1 commit into
Open
Conversation
Rails 8.2 deprecates the Time::DATE_FORMATS and Date::DATE_FORMATS constants in favor of ActiveSupport::TimeFormats / DateFormats. Reading those constants from LocalTimeHelper#ruby_time_or_date_format now emits a DEPRECATION WARNING on every render that uses a symbol format. Prefer the new lookup API when available, fall back to the existing DATE_FORMATS read on older Rails. Tests pass on Rails 7.1.
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Refactors time/date format lookup to prefer new ActiveSupport lookup APIs when available, with fallbacks to Time::DATE_FORMATS / Date::DATE_FORMATS.
Changes:
- Replaces direct
Time::DATE_FORMATS/Date::DATE_FORMATSaccess with helper methods. - Adds
lookup_time_format/lookup_date_formatto supportActiveSupport::*Formats.lookup. - Keeps fallback behavior for older Rails versions.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+113
to
125
| if defined?(ActiveSupport::TimeFormats) | ||
| ActiveSupport::TimeFormats.lookup(name.to_sym) | ||
| else | ||
| Time::DATE_FORMATS.with_indifferent_access[name] | ||
| end | ||
| end | ||
|
|
||
| def lookup_date_format(name) | ||
| if defined?(ActiveSupport::DateFormats) | ||
| ActiveSupport::DateFormats.lookup(name.to_sym) | ||
| else | ||
| Date::DATE_FORMATS.with_indifferent_access[name] | ||
| end |
| if defined?(ActiveSupport::TimeFormats) | ||
| ActiveSupport::TimeFormats.lookup(name.to_sym) | ||
| else | ||
| Time::DATE_FORMATS.with_indifferent_access[name] |
|
|
||
| def lookup_time_format(name) | ||
| if defined?(ActiveSupport::TimeFormats) | ||
| ActiveSupport::TimeFormats.lookup(name.to_sym) |
|
|
||
| def lookup_date_format(name) | ||
| if defined?(ActiveSupport::DateFormats) | ||
| ActiveSupport::DateFormats.lookup(name.to_sym) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Rails 8.2 (currently
main) deprecates theTime::DATE_FORMATSandDate::DATE_FORMATSconstants in favor of the newActiveSupport::TimeFormats/ActiveSupport::DateFormatsmodules. See rails/rails@b527067 for the deprecation.LocalTimeHelper#ruby_time_or_date_formatreads those constants on every render that uses a symbol format, which produces a noisy warning on apps tracking Rails edge:Fix
Prefer
ActiveSupport::TimeFormats.lookup/ActiveSupport::DateFormats.lookupwhen the modules are defined, falling back to the existingDATE_FORMATSread on older Rails so this stays compatible with the gem's current~> 7.0dev dependency.ActiveSupport::TimeFormats.lookupinternally checks both the new frozen@listand the legacyDEPRECATED_LIST(where writes viaTime::DATE_FORMATS[:foo] = ...still land), so existing call sites that register formats the old way continue to work transparently.Verification
bundle exec rake test:helpers→ 27 runs, 28 assertions, 0 failures (Rails 7.1.5.1, backward-compat path).:stamp-style formats resolve correctly.