Skip to content

Commit 1688ba0

Browse files
authored
Merge pull request rails#55296 from rails/fxn/da-notifications
Pass the user-facing reflection when notifying deprecated HABTMs
2 parents 58a2187 + 9496e00 commit 1688ba0

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

activerecord/lib/active_record/associations/deprecation.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ def guard(reflection)
3737
end
3838

3939
def report(reflection, context:)
40+
reflection = user_facing_reflection(reflection)
41+
4042
message = +"The association #{reflection.active_record}##{reflection.name} is deprecated, #{context}"
4143
message << " (#{backtrace_cleaner.first_clean_frame})"
4244

@@ -75,6 +77,15 @@ def clean_locations
7577
def set_backtrace_supports_array_of_locations?
7678
@backtrace_supports_array_of_locations ||= Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.4.0")
7779
end
80+
81+
def user_facing_reflection(reflection)
82+
case reflection.parent_reflection
83+
when ActiveRecord::Reflection::HasAndBelongsToManyReflection
84+
reflection.parent_reflection
85+
else
86+
reflection
87+
end
88+
end
7889
end
7990

8091
self.mode = :warn

activerecord/test/cases/associations/deprecation_test.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,18 @@ def teardown
212212
ActiveRecord::LogSubscriber.backtrace_cleaner = @original_backtrace_cleaner
213213
end
214214

215+
def assert_user_facing_reflection(model, association)
216+
payloads = []
217+
callback = ->(event) { payloads << event.payload }
218+
219+
ActiveSupport::Notifications.subscribed(callback, "deprecated_association.active_record") do
220+
model.new.send(association)
221+
end
222+
223+
assert_equal 1, payloads.size
224+
assert_equal model.reflect_on_association(association), payloads[0][:reflection]
225+
end
226+
215227
test "report publishes an Active Support notification in :notify mode" do
216228
payloads = []
217229
callback = ->(event) { payloads << event.payload }
@@ -234,5 +246,29 @@ def teardown
234246
assert_equal __FILE__, payload[:backtrace][-2].path
235247
assert_equal line, payload[:backtrace][-2].lineno
236248
end
249+
250+
test "has_many receives the user-facing reflection in the payload" do
251+
assert_user_facing_reflection(DATS::Author, :deprecated_posts)
252+
end
253+
254+
test "has_one receives the user-facing reflection in the payload" do
255+
assert_user_facing_reflection(DATS::Author, :deprecated_post)
256+
end
257+
258+
test "belongs_to receives the user-facing reflection in the payload" do
259+
assert_user_facing_reflection(DATS::Bulb, :deprecated_car)
260+
end
261+
262+
test "has_many :through receives the user-facing reflection in the payload" do
263+
assert_user_facing_reflection(DATS::Author, :deprecated_has_many_through)
264+
end
265+
266+
test "has_one :through receives the user-facing reflection in the payload" do
267+
assert_user_facing_reflection(DATS::Author, :deprecated_has_one_through)
268+
end
269+
270+
test "HABTM receives the user-facing reflection in the payload" do
271+
assert_user_facing_reflection(DATS::Category, :deprecated_posts)
272+
end
237273
end
238274
end

0 commit comments

Comments
 (0)