Skip to content

Commit 141c2f1

Browse files
authored
Merge pull request #142 from daugaard/update-rails-deprecation-access
Update deprecation tracker to support newer Rails versions (7.1+)
2 parents 2b5ed88 + d83d1b7 commit 141c2f1

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
* Your changes/patches go here.
66

7+
- [FEATURE: Update deprecation tracker to support newer Rails versions (7.1+)](https://github.com/fastruby/next_rails/pull/142)
8+
79
# v1.4.3 / 2025-02-20 [(commits)](https://github.com/fastruby/next_rails/compare/v1.4.2...v1.4.3)
810

911
- [Add next_rails --init](https://github.com/fastruby/next_rails/pull/139)

lib/deprecation_tracker.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,17 @@ def self.init_tracker(opts = {})
7373
mode = opts[:mode]
7474
transform_message = opts[:transform_message]
7575
deprecation_tracker = DeprecationTracker.new(shitlist_path, transform_message, mode)
76-
if defined?(ActiveSupport)
76+
# Since Rails 7.1 the preferred way to track deprecations is to use the deprecation trackers via
77+
# `Rails.application.deprecators`.
78+
# We fallback to tracking deprecations via the ActiveSupport singleton object if Rails.application.deprecators is
79+
# not defined for older Rails versions.
80+
if defined?(Rails) && defined?(Rails.application) && defined?(Rails.application.deprecators)
81+
Rails.application.deprecators.each do |deprecator|
82+
deprecator.behavior << -> (message, _callstack = nil, _deprecation_horizon = nil, _gem_name = nil) {
83+
deprecation_tracker.add(message)
84+
}
85+
end
86+
elsif defined?(ActiveSupport)
7787
ActiveSupport::Deprecation.behavior << -> (message, _callstack = nil, _deprecation_horizon = nil, _gem_name = nil) {
7888
deprecation_tracker.add(message)
7989
}

spec/deprecation_tracker_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,48 @@
251251
end
252252
end
253253

254+
describe "#init_tracker" do
255+
it "returns a new instance of DeprecationTracker" do
256+
tracker = DeprecationTracker.init_tracker({mode: "save"})
257+
expect(tracker).to be_a(DeprecationTracker)
258+
end
259+
260+
it "subscribes to KernelWarnTracker deprecation events" do
261+
expect do
262+
DeprecationTracker.init_tracker({mode: "save"})
263+
end.to change(DeprecationTracker::KernelWarnTracker.callbacks, :size).by(1)
264+
end
265+
266+
context "when Rails.application.deprecation is not defined and ActiveSupport is defined" do
267+
it "sets the ActiveSupport::Deprecation behavior" do
268+
# Stub ActiveSupport::Deprecation with a simple behavior array
269+
stub_const("ActiveSupport::Deprecation", Class.new {
270+
def self.behavior
271+
@behavior ||= []
272+
end
273+
})
274+
275+
expect do
276+
DeprecationTracker.init_tracker({mode: "save"})
277+
end.to change(ActiveSupport::Deprecation.behavior, :size).by(1)
278+
end
279+
end
280+
281+
context "when Rails.application.deprecation is defined" do
282+
it "sets the behavior of each Rails.application.deprecators" do
283+
# Stub Rails.application.deprecators with an array of mock deprecators
284+
fake_deprecator_1 = double("Deprecator", behavior: [])
285+
fake_deprecator_2 = double("Deprecator", behavior: [])
286+
stub_const("Rails", Module.new)
287+
allow(Rails).to receive_message_chain(:application, :deprecators).and_return([fake_deprecator_1, fake_deprecator_2])
288+
289+
expect do
290+
DeprecationTracker.init_tracker({ mode: "save" })
291+
end.to change(fake_deprecator_1.behavior, :size).by(1).and change(fake_deprecator_2.behavior, :size).by(1)
292+
end
293+
end
294+
end
295+
254296
describe DeprecationTracker::KernelWarnTracker do
255297
it "captures Kernel#warn" do
256298
warn_messages = []

0 commit comments

Comments
 (0)